Skip to content

Websockets: serialize writes by default, and refuse writes once the handler has ended #959

Open

Description

@MDA2AV

Follow-up to #914 and #953.

Serialize writes by default

#953 adds SynchronizeWrites() to the websocket builders, off by default. Without it, a connection written from several threads - what the reactive flavour exists for - races the pong and close frames the default OnPing and OnClose handlers write from the message pump, which an application's own lock never covers.

On the ioxide engine that is memory corruption, not garbled frames: TcpConnection refuses a second flush but not a second writer (MDA2AV/ioxide#397). With four unsynchronized writers the test host crashed in 2 of 16 runs; with SynchronizeWrites(), in none of 16.

An uncontended SemaphoreSlim.WaitAsync completes synchronously without allocating, so serializing could be the default, with an opt-out for a connection that only one thread ever writes.

Refuse writes once the handler has ended

The connection lives in an await using in the content's WriteAsync (Modules/Websockets/Reactive/ReactiveWebsocketContent.cs:23, Modules/Websockets/Imperative/ImperativeWebsocketContent.cs:22), and WebsocketConnection.DisposeAsync only completes the pipe reader. A task started from OnConnected - a broadcaster, a timer - still holds the connection after the client has left and the pump has returned, and its writes still go to the response sink of a connection the engine is done with. On ioxide, a pipe writer that outlives its connection can write into the connection that reuses it (MDA2AV/ioxide#398; found by reading the code, not reproduced).

Suggestions

  • Make SynchronizeWrites() the default, with an opt-out such as SynchronizeWrites(false).
  • Mark the connection closed in DisposeAsync, under the write lock so a write in flight finishes first, and make every later write throw ConnectionClosedException instead of reaching the sink.
  • Say in the docs that background writers should stop on that exception.

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions