Repository navigation
Description
Description
On CoreCLR WASI, closing a connected TCP Socket while a send is still in flight blocks the entire single-threaded guest inside the close call. If the peer that has to read the data is in the same process (as in our loopback tests), it never gets to run, so the process deadlocks at 0% CPU. Timers stop firing too, so test timeouts such as WaitAsync never trigger.
Root cause is in the host. In wasmtime 48 and later, dropping a WASIp2 TCP output-stream waits for any in-flight background write to finish. TcpWriter::cancel() in crates/wasi/src/p2/tcp.rs waits "to prevent silently dropping data". tcp-socket.shutdown doesn't help: while a write is in flight it only schedules the close for after that write. On WASI, SafeSocketHandle calls close(fd) directly, and wasi-libc then drops the output stream, so Socket.Dispose blocks until the peer reads the pending data.
This is stricter than the WASI spec, which allows a lossy drop. wasi-io streams.wit says: "Dropping an output-stream while there's still an active write in progress may result in the data being lost." wasmtime 47 and earlier aborted the in-flight write on drop. The behavior changed in bytecodealliance/wasmtime#13934 ("wasi-sockets: Unify the p2 & p3 implementations"). It's also stricter than POSIX close() without SO_LINGER, which returns immediately.
Reproduction Steps
Run either test on CoreCLR WASI with wasmtime 49.0.1:
System.Net.Sockets.Tests.SendReceive_MemoryArrayTask.TcpPeerReceivesFinOnShutdownWithPendingData(also theMemoryNativeTaskvariant; theTaskvariant hangs intermittently)System.Net.Sockets.Tests.SendFile_CancellableTask.SendFileAsync_CanceledDuringOperation_Throws
Both fill a loopback TCP connection's send buffer, then call Shutdown and Dispose (or cancel the sends) before the peer reads.
Expected behavior
Socket.Dispose returns without blocking the guest, the peer then reads the remaining data until FIN, and the test completes.
Actual behavior
The guest hangs forever at 0% CPU.
A wasmtime host-call trace (WASMTIME_LOG=wasmtime_wasi=trace,wasmtime_wasi_io=trace) shows this sequence:
- Repeated
output-stream.check-writeandwritecalls, untilcheck-writereturns 0. - One
poll. tcp-socket.shutdown(both).- No further host calls.
A macOS sample of the hung process shows the main thread parked in block_on, with every tokio worker idle.
Regression?
Yes, relative to wasmtime 47 and earlier, where dropping an output stream aborted the in-flight write.
Known Workarounds
On WASI, defer the actual close(fd) until the connected TCP socket's output-stream pollable is ready, and wait for it through the WASI event loop so other work keeps running. A prototype passed both tests and the related socket and HTTP test classes locally (565 socket tests, 140 SocketsHttpHandler_HttpClientHandlerTest tests, 0 failures). It was left out of #136683 to keep that PR test-focused. Separately, the wasmtime behavior is probably worth reporting upstream.
Configuration
- CoreCLR WASI (
wasi-wasm), Release, single-threaded. - wasmtime 49.0.1 on macOS arm64.
mainas of 2026-11-13.
Other information
The tests above are disabled on WASI against this issue in #136683.
Note
This issue was drafted with GitHub Copilot.
Activity
dotnet-policy-service commentedon Nov 13, 2026
Tagging subscribers to 'arch-wasm': @lewing, @pavelsavara
See info in area-owners.md if you want to be subscribed.
dotnet-policy-service commentedon Nov 13, 2026
Tagging subscribers to this area: @karelz, @dotnet/ncl
See info in area-owners.md if you want to be subscribed.
Track WASI socket close hang as dotnet#137519 instead of working arou…