Skip to content

[wasi] Fix WasiHttpHandler request failures, disposal and header handling - #136261

Merged
lewing merged 2 commits into
dotnet:mainfrom
lewing:lewing-wasi-http-handler-fixes
Oct 30, 2026
Merged

lewing merged 2 commits into
dotnet:mainfrom
lewing:lewing-wasi-http-handler-fixes

Conversation

@lewing

@lewing lewing commented Oct 22, 2026

Copy link
Copy Markdown
Member

Fixes several WasiHttpHandler (the wasi:http handler used by HttpClientHandler on WASI) behaviors that made failures hang or surface the wrong exception.

Request content failures hang until timeout. SendContent was started fire-and-forget, so if serializing the request content threw or was canceled, nothing observed it and the request waited for a response until HttpClient.Timeout (100s). The handler now watches the send task while polling for the response and fails the request with the content's exception.

Dispose hides the real exception. Stream.Dispose() called the WasiOutputStream.Close() override, which finished the outgoing body. After a content failure, that threw a WitException from OutgoingBody.Finish, which replaced the original exception. Completing the body is now an explicit Finish(), and disposing an unfinished stream aborts the body.

Pending-pollable disposal trap. Dropping a request or response stream while a read or write pollable (a child resource) was still registered trapped wasmtime with resource has children and killed the process. The streams now cancel their pending wait before dropping the stream.

Chunked / Content-Length. wasi:http forbids the Transfer-Encoding header, so TransferEncodingChunked = true failed header validation. It now omits the header (and Content-Length) and lets the host chunk the body, and chunked encoding without content throws InvalidOperationException like SocketsHttpHandler. For content with a known length, Content-Length is now computed and sent.

Exception types aligned with SocketsHttpHandler.

  • Null or relative request URIs now throw InvalidOperationException, and non-http(s) schemes throw NotSupportedException; both used to throw ArgumentException.
  • wasi:io stream errors while reading the response body now throw HttpIOException(HttpRequestError.ResponseEnded) instead of a raw WitException.
  • CanRead is false after the response stream is disposed.

Validation

CoreCLR WASI, Release, wasmtime 49.0.1. Each run was done against a baseline build of the same main commit without these changes.

Run Before (pass / fail / skip) After (pass / fail / skip)
System.Net.Http.Functional.Tests innerloop 768 / 0 / 23 (791 run) 768 / 0 / 23 (791 run)
OuterLoop HttpClientHandler_RemoteServerTest, SocketsHttpHandler_PostScenarioTest, SocketsHttpHandler_ResponseStreamTest 35 / 338 / 3 37 / 336 / 3
  • No test regressed from pass to fail.
  • SendAsync_SendSameRequestMultipleTimesDirectlyOnHandler_Success now passes (2 cases) because Content-Length is sent.
  • The remaining outerloop failures are pre-existing on WASI: MD5 is unavailable, TLS to the dev-cert echo server fails, and the handler has unsupported features.
  • On main, the shared HttpClientHandlerTest bodies don't run against this handler on WASI. They're exercised by a follow-up WASI-only WasiHttpHandler test project, where the request-content-failure, cancellation and response-stream tests pass with these changes.

Note

This PR description and the changes were generated with GitHub Copilot.

lewing and others added 2 commits October 21, 2026 20:07
Throw InvalidOperationException/NotSupportedException for missing, relative
or unsupported request URIs, translate wasi:io stream errors on the response
body into HttpIOException(ResponseEnded), and report CanRead=false after the
response stream is disposed.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Observe the request-content send task while waiting for the response, so a
failure in the content (or its cancellation) fails the request with the
original exception instead of hanging until HttpClient.Timeout. Also:
- abort, rather than finish, the outgoing body when the request wrapper is
  disposed before the content completes;
- release pending wasi:io pollables before dropping request/response streams,
  avoiding the 'resource has children' trap;
- translate TransferEncodingChunked into a body without Content-Length, since
  wasi:http forbids the Transfer-Encoding header, and send Content-Length for
  content of known length.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 4 pipeline(s).
12 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

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

@lewing lewing added arch-wasm WebAssembly architecture os-wasi Related to WASI variant of arch-wasm labels Oct 22, 2026
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

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

@pavelsavara
pavelsavara self-requested a review October 22, 2026 09:50
@lewing
lewing marked this pull request as ready for review October 24, 2026 02:56
@lewing
lewing requested a review from radekdoulik October 26, 2026 16:08
@lewing

lewing commented Oct 27, 2026

Copy link
Copy Markdown
Member Author

I have a full test standup for this that needs to stack on the socketshttphandler/websocket pr that demonstrated the need for this.

@lewing

lewing commented Oct 30, 2026

Copy link
Copy Markdown
Member Author

#136684 has the test support for WasiHttpHandler

@lewing
lewing merged commit a932cb6 into dotnet:main Oct 30, 2026
103 of 106 checks passed
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Nov 4, 2026
lewing added a commit that referenced this pull request Nov 13, 2026
Since #136234 (and with the handler fixes from #136261),
`System.Net.Http.Functional.Tests` sets `UseNativeHttpHandler=false` on
WASI, so it covers SocketsHttpHandler. The switch is process-wide and
trimmed, so one assembly can't also test the `wasi:http` handler
(`WasiHttpHandler`). This adds a WASI-only test project,
`System.Net.Http.WasiHttpHandler.Functional.Tests`, that leaves the
switch at its WASI default (true).

### Test project
The project adds `WasiHttpHandler_*` classes for these shared test
bases:
- HttpClientHandlerTest
- Asynchrony
- Cancellation
- IdnaProtocolTests
- PostScenarioTest
- ResponseStreamTest

It also includes `HttpClientHandler_RemoteServerTest`. PostScenarioTest
and RemoteServerTest are OuterLoop on WASI.

The existing `tests.proj` glob picks the project up, which adds one
Helix work item.

### Skips
- **Capability gates:**
- The AutoRedirect, Decompression, Proxy and DefaultProxyCredentials
classes use `ConditionalClass` on `SupportsRedirectConfiguration`,
`SupportsAutomaticDecompression` and `SupportsProxy`. Those probes now
live in the shared `HttpClientHandlerTestBase`.
  - In-body checks cover the same capabilities.
- **`IsWasiHttpHandler`** (alongside `IsWinHttpHandler`) adds early
returns for handler gaps with no public capability flag:
  - Credentials and PreAuthenticate
  - CookieContainer
  - MaxConnectionsPerServer
  - redirect following
  - synchronous stream I/O
- **Host behaviour:** wasmtime's `wasi:http` behaviour is also gated:
  - It forbids hop-by-hop request headers and control characters.
  - It strips hop-by-hop response headers and rejects obs-fold.
- It doesn't expose the reason phrase or let the client choose the HTTP
version.
- **Outerloop helpers:**
- MD5 checks are gated on `PlatformDetection.IsMD5Supported`, using the
echo server's existing `Content-MD5-Skip` path.
- The HTTPS/HTTP2 local echo endpoints are dropped on WASI, which
doesn't trust their dev certificate.

HttpProtocolTests isn't included: it checks status-line parsing that the
`wasi:http` host performs, and Browser excludes it too.

### Validation
CoreCLR WASI, Release, wasmtime 49.0.1, on main `a932cb67bf6`:

| Suite | Run | Passed | Failed | Skipped |
|---|---|---|---|---|
| `System.Net.Http.WasiHttpHandler.Functional.Tests` | 195 | 184 | 0 |
11 |
| `System.Net.Http.Functional.Tests` (shared test changes) | 1994 | 1810
| 0 | 184 |

Outerloop was validated before the rebase onto current main: 311 of 325
tests passed, with 0 failures, with every class except the two
`SupportsProxy`-gated proxy classes.

Running the full outerloop scope still runs those proxy classes.
WasmTestRunner lets `-trait category=OuterLoop` override `-notrait
category=failing`, so `ConditionalClass`-gated classes run anyway.
That's a separate runner issue affecting all WASI/browser projects. The
WASI outerloop CI leg currently runs innerloop scope only.

> [!NOTE]
> This PR description and the changes were generated with GitHub
Copilot.

---------

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

arch-wasm WebAssembly architecture area-System.Net.Http os-wasi Related to WASI variant of arch-wasm

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants