Repository navigation
Description
From the next ioxide release, 0.24.444 (MDA2AV/ioxide#444, carrying MDA2AV/ioxide#442), the reactor thread no longer installs a SynchronizationContext. Up to 0.23.440, a plain await on something another thread completes (Npgsql, the BCL HttpClient, Task.Delay, a FileStream) was posted back to the reactor. From 0.24 it resumes on the thread that completed it, and the code after it stays there until an ioxide call completes on the reactor.
ioxide's own APIs take calls from any thread, so the engine keeps working. What doesn't is the state it keeps per thread.
Where
The drivers await the handler chain with plain awaits, then write the response:
Http1Driver.HandleRequestAsync(Engine/Ioxide/Protocol/Drivers/Tcp/Http1Driver.cs:198)Http2Driver.DispatchAsync(Engine/Ioxide/Protocol/Drivers/Tcp/Http2Driver.cs:57) andHttp3Driver.DispatchAsync(Engine/Ioxide/Protocol/Drivers/Quic/Http3Driver.cs:55);StreamedDriver.DispatchAsyncin Update ioxide to 0.27.637, serialize websocket writes by default, and refuse them once the handler has ended #953.
After a handler or concern awaits something that isn't ioxide, the whole response is written from that thread, and four thread-statics are read there:
| Thread-static | Off the reactor |
|---|---|
IoxideReactor._current |
IoxideReactor.Current throws InvalidOperationException. IoxideAssetContent.RentPool (Modules/Files/Multi/Ioxide/IoxideAssetContent.cs:102) reads it before the body, so the request fails and the connection closes without a response. |
Http1Driver._requestPool |
A request returned there lands in a pool on a thread-pool thread and is never reused. |
StreamedRequest._pool |
The same. |
Http1DateHeader._buffer, _second |
Each thread builds its own copy. Harmless. |
For example, an authentication concern that looks the user up with Npgsql, in front of an ioxide-backed static file folder: the files behind it fail. WarnIfThreadHopped(..., "after-handle") logs the hop once.
Fix
One await new ReactorSwitch(reactor) right after HandleAsync in each driver. ioxide 0.24 adds ReactorSwitch and Reactor.Current to the core. Everything that reads a thread-static after the handler then runs on the reactor again. The other reads (IoxideReactor.Current in TcpDriver, RentRequest and StreamedRequest.Rent) happen when the connection or stream starts, which is on the reactor.
// Http2Driver / Http3Driver (StreamedDriver in #953): DispatchAsync starts on the reactor
var reactor = Reactor.Current;
...
var response = await server.Handler.HandleAsync(mapped) ?? throw ...;
await new ReactorSwitch(reactor);
// Http1Driver: take the reactor in RunAsync, next to reactorThreadId, and pass it to HandleRequestAsyncOn the reactor, the usual case, the switch only checks the thread. Otherwise it costs one post to the reactor, which is what the context did after every such await. ioxide's own HTTP/2 and HTTP/3 stacks switch back the same way after each handler.
Reactor.Current is the same thing as IoxideReactor.Current, a thread-static set on the reactor thread, so IoxideReactor could go. That alone does not fix this: it is null off the reactor too.
Related: #953.
Activity
Update ioxide to 0.26.563, return to the reactor after the handler, a…
Update ioxide to 0.26.563, return to the reactor after the handler, a…