Skip to content

[wasi] Disposing a TCP socket with an in-flight send blocks the guest (wasmtime waits on output-stream drop) #137519

Open

Description

@lewing

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 the MemoryNativeTask variant; the Task variant 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:

  1. Repeated output-stream.check-write and write calls, until check-write returns 0.
  2. One poll.
  3. tcp-socket.shutdown(both).
  4. 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.
  • main as 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

  1. added
    arch-wasm
    os-wasi
    on Nov 13, 2026
  2. added
    untriaged
    on Nov 13, 2026
  3. dotnet-policy-service commented on Nov 13, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to 'arch-wasm': @lewing, @pavelsavara
    See info in area-owners.md if you want to be subscribed.

  4. dotnet-policy-service commented on Nov 13, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @karelz, @dotnet/ncl
    See info in area-owners.md if you want to be subscribed.

  5. added this to the 12.0.0 milestone on Nov 13, 2026
  6. removed
    untriaged
    on Nov 13, 2026
  7. added a commit that references this issue on Nov 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    arch-wasmarea-System.Net.Socketsos-wasi

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions