Skip to content

Never cancel a Snoop pipe frame write partway - #10

Merged
oysteinkrog merged 1 commit into
developfrom
fix/no-torn-frames
Oct 26, 2026
Merged

oysteinkrog merged 1 commit into
developfrom
fix/no-torn-frames

Conversation

@oysteinkrog

Copy link
Copy Markdown
Member

Problem

The broker could lose its Snoop session while the target app kept running. Every later call then failed with SESSION_NOT_FOUND.

FramedJsonTransport.SendAsync sent each frame as two writes: the 4-byte length prefix, then the JSON body. Both writes used the caller's token, which in PipeSnoopInspectorProxy is the 10 s per-call timeout. A pipe write stays pending until the reader takes the bytes. .NET creates the pipe with a zero-size buffer, so this applies to every write. When the agent read slowly, the timeout could cancel the body write after the agent had already read the prefix. The unread body bytes were lost. The agent then read the next frame's bytes as the rest of the old one, failed to parse them and closed the pipe. ResponsePumpAsync saw the end of the stream and marked the session disconnected.

Windows and Wine handle pending pipe writes the same way. Wine is slower, so the timeout fires more often there.

Fix

  • FramedJsonTransport.SendAsync builds one buffer holding the prefix and the body, and writes it in a single write with CancellationToken.None. The write lock is held until that write finishes. The caller's token now only limits how long the caller waits.
  • PipeSnoopInspectorProxy.InvokeAsync sends inside the try, so a send that times out reports OperationTimedOut and sends a cancel frame, as a timed-out response already did.
  • New test SendAsync_CancelledWhileWritePending_StillWritesWholeFrame uses a stream that stalls the write. It fails on the old transport (JsonException at byte 1) and passes with the fix. All 12 transport tests pass.

Testing

All runs were under Wine on Linux. Nothing was tested on Windows.

Real MotionCatalyst, headless, with the same build, hive and Wine. Only the broker's SnoopWPF.Agent.Remote.dll changed between runs. Each freeze round stopped the MC process (SIGSTOP) for 13 s during a call, resumed it, then made 5 more calls.

Broker DLL Freeze rounds the session survived Soak afterwards Drops
Before this change 0 of 2 not run session lost in both rounds
With this change 3 of 3 9 minutes, 12,698 calls 0

With the change, each frozen call returned OPERATION_TIMED_OUT, then all 5 later calls worked.

A stress harness running the real transport under Wine broke the frame stream in 3 of 3 runs before the change, and in 0 of 3 runs with it.

The unit test proves the frame tear itself. In real MotionCatalyst the test shows the session drop before the change and no drop after it.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Nd32EDa2xhc9FzL1Vp3NJU

Agent: wine-snoop (subagent of linux-wine) on oystein-office-primary

A pipe write that does not fit the pipe's buffer stays pending until the
reader takes the bytes. Under Wine that is every write, because .NET
creates the pipe with a zero-size buffer. When the per-call timeout or a
caller's token cancelled a pending write, the reader had already taken
the length prefix but never got the body. It then read the next frame's
bytes as the rest of this one, failed to parse them, and closed the
pipe, so the broker lost the session while the target kept running.

SendAsync now writes the prefix and body in one write that is never
cancelled, and holds the write lock until it finishes. The token still
bounds the caller's wait. A timed-out send in the proxy now reports
OperationTimedOut and sends a cancel frame, like a timed-out response.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Nd32EDa2xhc9FzL1Vp3NJU
@oysteinkrog
oysteinkrog merged commit 05f0c24 into develop Oct 26, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant