Skip to content

Gather writev iovecs into a single FS.write - #27286

Merged
sbc100 merged 3 commits into
emscripten-core:mainfrom
guybedford:iov-writev-gather
Jul 9, 2026
Merged

sbc100 merged 3 commits into
emscripten-core:mainfrom
guybedford:iov-writev-gather

Conversation

@guybedford

Copy link
Copy Markdown
Collaborator

doWritev currently issues one FS.write per iovec. For a stream socket that fragments a single writev into one segment per iovec, which breaks stream byte semantics — e.g. mio's TCP writes came through split rather than as one contiguous send.

POSIX writev is a single gather-write, and __syscall_sendmsg already gathers all iovecs into one contiguous buffer before writing. This makes doWritev do the same:

  • sum the iovec lengths
  • copy each iovec into one contiguous buffer
  • issue a single FS.write

Adds test/fs/test_writev_gather.c, which registers a device whose write hook asserts it receives the whole payload in exactly one call.

Made with AI assistance under my review

@guybedford
guybedford force-pushed the iov-writev-gather branch 3 times, most recently from 7ee04cc to cc0503b Compare July 8, 2026 23:10
Per-iovec writes fragment a stream socket send into one segment per
iovec, breaking stream byte semantics (e.g. mio's TCP writes). POSIX
writev is a single gather-write, and __syscall_sendmsg already gathers
into one buffer before writing. Do the same in doWritev: sum the iovec
lengths, copy each into one contiguous buffer, and issue a single
FS.write.
@guybedford
guybedford force-pushed the iov-writev-gather branch from cc0503b to 5bfccda Compare July 9, 2026 00:06
@sbc100
sbc100 enabled auto-merge (squash) July 9, 2026 00:14
@guybedford

Copy link
Copy Markdown
Collaborator Author

I pushed the change to update the failing fixture here only, where the change is a correct behavioural change of the test under the PR.

@guybedford
guybedford force-pushed the iov-writev-gather branch from 014d270 to 4d35bba Compare July 9, 2026 01:08
@sbc100
sbc100 merged commit ca783f4 into emscripten-core:main Jul 9, 2026
39 checks passed
@guybedford
guybedford deleted the iov-writev-gather branch July 9, 2026 05:32
juj added a commit that referenced this pull request Nov 19, 2026
These two tests were added in #27284 and #27286 and assume that `writev`
gathers all its iovecs into a single write on the underlying device,
which is how the JS filesystem behaves. WasmFS instead writes each iovec
separately (returning a short write if a later one fails), so both tests
fail there:

  wasmfs.test_fs_writev_gather:
    Aborted(expected a single 8-byte gather write)
Aborted(Assertion failed: nwritten == 8, at:
test/fs/test_writev_gather.c,42,main)

  wasmfs.test_fs_readv_eagain:
Aborted(Assertion failed: nwrite == 8, at:
test/fs/test_readv_eagain.c,72,main)

- test_fs_writev_gather only tests the gathering behaviour, so disable
it under WasmFS.
- In test_fs_readv_eagain, expect the write of the first iovec to
succeed and the second one to would-block under WasmFS, which results in
a partial write of 4 bytes rather than EAGAIN.

---------

Co-authored-by: Claude Fable 5.2 <[email protected]>
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