Skip to content

Fix use-after-free in async write causing corruption with large payloads#392

Merged
haata merged 1 commit intocapnproto:masterfrom
edevil:fix-async-write-use-after-free
Jan 16, 2026
Merged

Fix use-after-free in async write causing corruption with large payloads#392
haata merged 1 commit intocapnproto:masterfrom
edevil:fix-async-write-use-after-free

Conversation

@edevil
Copy link
Contributor

@edevil edevil commented Jan 8, 2026

In _PyAsyncIoStreamProtocol.write_loop(), memoryview objects pointing to C++ message memory were passed directly to transport.write(). Since transport.write() is non-blocking and only queues data for later transmission, the memoryview could reference freed memory after fulfill() was called.

This caused message corruption when pipelining RPC calls with payloads larger than ~4000 bytes, as the C++ message memory would be freed before asyncio had a chance to transmit the data.

The fix copies the data to Python bytes objects before passing to transport.write(), ensuring the data remains valid until asyncio transmits it.

Includes regression test that verifies large payload integrity with both sequential and pipelined RPC calls.

@edevil edevil force-pushed the fix-async-write-use-after-free branch from ffc88da to 24af772 Compare January 8, 2026 15:39
In _PyAsyncIoStreamProtocol.write_loop(), memoryview objects pointing to
C++ message memory were passed directly to transport.write(). Since
transport.write() is non-blocking and only queues data for later
transmission, the memoryview could reference freed memory after
fulfill() was called.

This caused message corruption when pipelining RPC calls with payloads
larger than ~4000 bytes, as the C++ message memory would be freed before
asyncio had a chance to transmit the data.

The fix copies the data to Python bytes objects before passing to
transport.write(), ensuring the data remains valid until asyncio
transmits it.

Includes regression test that verifies large payload integrity with both
sequential and pipelined RPC calls.
@edevil edevil force-pushed the fix-async-write-use-after-free branch from 24af772 to 4e015f4 Compare January 8, 2026 15:53
@haata
Copy link
Collaborator

haata commented Jan 16, 2026

Thanks @edevil !

@haata haata merged commit 9754258 into capnproto:master Jan 16, 2026
11 of 12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants