Skip to content

fix(bridge): enforce one receive deadline per frame - #121

Merged
blwfish merged 3 commits into
blwfish:devfrom
cw-jlu:fix/bridge-frame-deadline
Nov 10, 2026
Merged

blwfish merged 3 commits into
blwfish:devfrom
cw-jlu:fix/bridge-frame-deadline

Conversation

@cw-jlu

@cw-jlu cw-jlu commented Nov 9, 2026 •

Copy link
Copy Markdown

What does this PR do?

Fixes #120. A peer that sends one byte every 80 ms currently completes a message after about 640 ms even with receive_message(timeout=0.2): each recv() gets a fresh timeout. This affects both the MCP bridge's response wait and FreeCAD's per-connection request wait (now configurable through FREECAD_MCP_RECV_TIMEOUT).

Use one monotonic deadline for the prefix and body, passing only the remaining budget to each exact read. Keep the wire format, size limits, failure return value, caller-timeout restoration, and None/zero blocking modes. This is independent of the dependency migration proposed in #118.

Follow-up from review: rebased onto dev; the deadline unit tests no longer assert elapsed wall-clock time (the slow-trickle test checks that the receive gives up before the peer has delivered the full frame, and the shared header/body deadline test uses a fake monotonic clock); the strict xfail on test_slow_trickle_is_dropped_at_the_single_deadline in tests/windows/test_windows_headless.py is removed.

Validation (Windows, CPython 3.14, uv sync --frozen --group dev, on dev 51e6502)

  • Focused framing tests (-k 'timeout_bounds_entire_frame or fragmented_frame_finishes or share_receive_deadline or preserves_blocking_modes'): 12 passed with this change; on unpatched dev the same tests give 6 failed, 6 passed (the four slow header/body cases and the two shared-deadline cases fail).
  • python -m pytest tests/unit --ignore=tests/unit/test_headless_server.py -q: 157 failed, 2709 passed, 39 errors, 10 skipped with this change; unpatched dev in the same environment gives 157 failed, 2697 passed, 39 errors, 10 skipped. The failing/error node-ID sets are identical, so this adds no failures; the Windows run is not green and no full-suite pass is claimed. test_headless_server.py is excluded because one of its tests sends SIGTERM to the pytest process on Windows (exit code 15).
  • ruff check on the changed test files and git diff --check pass.
  • Not run locally: tests/windows (no FreeCAD installed on this machine), and the Linux/macOS matrices. The FreeCAD import stubs used by the unit tests do not stand in for FreeCAD-native validation.

Checklist

  • This PR targets the dev branch (not main, which is release-only)
  • Tests pass: python3 -m pytest tests/unit/ (see the Windows baseline note above)
  • New behaviour is tested (real connected sockets plus a deterministic fake-clock test; the FreeCAD-dependent case is covered by the existing tests/windows slow-trickle test, not run locally)
  • Commit messages follow the project style (fix:, feat:, docs:, etc.)
  • This PR addresses one concern (bug fix and unrelated cleanup are separate PRs)

Platform tested on
Windows only.

@blwfish

blwfish commented Nov 9, 2026

Copy link
Copy Markdown
Owner

I'll take a look shortly. I have no way to test Windows end-to-end, but I think we can do better than what's there now.

blwfish added a commit that referenced this pull request Nov 9, 2026
The handler waited a fixed 30 s for each client's request frame, so the
single-deadline receive behavior (PR #121 / issue #120) could not be exercised
end to end through the real server in any reasonable time.

Add FREECAD_MCP_RECV_TIMEOUT: seconds to wait for one complete request frame
per connection (header and body share one deadline). Unset, empty or
whitespace-only keeps the 30 s default. Otherwise it must be a plain ASCII
decimal greater than 0 and at most 3600. Zero (which socket code treats as
non-blocking, not "no timeout"), negatives, NaN/inf, exponents, underscores and
non-ASCII digits are rejected. A bad value never silently becomes the default:
the module still imports (so a typo cannot take the whole addon down), and
start_server() reports the error on every platform and refuses to start.

The value is passed to the handler's only receive_message() call site. Documented
in AGENT-INSTALL.md.

Tests pin both sides of the 0 and 3600 boundaries, the ambiguous number forms,
import safety, that the default matches receive_message()'s own default, that the
configured value actually reaches receive_message, and that a bad value refuses
to start with Windows both on and off.

Verified against a real FreeCAD handler: with PR #121's change applied and the
timeout at 1.0, a request trickled over ~10 s is dropped at ~1.0 s and the next
request is served; without it the same request completes.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
blwfish added a commit that referenced this pull request Nov 9, 2026
…ws workflow

The handler has a distinct Windows code path (loopback TCP listener, per-launch auth
token with an icacls ACL, SO_EXCLUSIVEADDRUSE, pid liveness, instance stop) that had
never run on a real Windows host in CI. Add:

- tests/windows/: a harness that launches the real headless_server.py under a real
  FreeCADCmd, fully isolated, and drives it over TCP with the project's own framing.
  Each server gets its OWN temp tree (and so its own token file); stdout/stderr go to
  files, not pipes (a Windows pipe holds 4096 bytes); an autouse session gate aborts the
  run before any test if FreeCAD ignored the profile redirect (it would otherwise use the
  real profile, or a cwd-relative dir inside the checkout).
- Tests: token file, ACL signature (real Windows), a 9-way auth matrix incl. non-ASCII and
  lone-surrogate tokens, sync/async/poll round trip, exclusive bind (real Windows), token
  rotation, pid liveness boundaries (real Windows; W-7 and W-9b as strict xfails),
  framing at MAX-1/MAX/MAX+1 plus zero-length, truncated and short frames, the
  FREECAD_MCP_RECV_TIMEOUT receive deadline, and a stop/orphan check.
- Fake mode (FREECAD_MCP_WINDOWS_TESTS_FAKE=1): tests/windows/fakewin_headless.py forces
  the handler's Windows branch on Linux/macOS so the harness and the platform-independent
  tests can be developed locally. It proves logic only; Windows-only tests are skipped
  and fake results never count as Windows coverage.
- .github/workflows/windows-tests.yml: advisory (not in all-tests-pass). tier1 runs
  `-m windows_safe`; tier2 downloads the pinned FreeCAD, verifies the release-API digest
  (weekly builds publish no -SHA256.txt), and always runs bin\FreeCADCmd.exe, never the
  1.1.x root-level launcher shim. assert_tests_ran.py fails an all-skipped run.

Known-failing-by-design: test_slow_trickle_is_dropped_at_the_single_deadline is a strict
xfail until PR #121 lands (verified: applying #121 flips it to XPASS(strict)). Mutation
check: replacing the token check with `return True` fails all 9 bad-token tests.

Local fake-mode run: 23 passed, 13 Windows-only skipped, 1 xfailed.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@blwfish

blwfish commented Nov 9, 2026

Copy link
Copy Markdown
Owner

Thanks for this, and for the report in #120. The single-deadline approach looks right to me.

Could you retarget the PR to dev? main is release-only (it only moves via a dev → main release PR), so I can't merge a PR against it as-is. On GitHub that is Edit next to the PR title, then change the base branch. I've just added this to CONTRIBUTING.md and the PR template, since it is an easy thing to miss. A plain rebase onto dev is enough. I trial-merged your branch into current dev and it applies without conflicts, and the unit suite passes on the result.

Two things have changed on dev that are relevant here:

  • The receive timeout is now configurable (FREECAD_MCP_RECV_TIMEOUT, default 30 s), so your deadline behavior can be exercised end to end without waiting 30 s. There is also a new Windows test suite (tests/windows) that drives a real headless FreeCAD. It already contains a test for exactly the slow-trickle case from Bridge receive timeout resets on every fragment instead of bounding a complete frame #120, marked xfail(strict=True) and waiting for this change. Once your change is on dev that test will start passing, which makes the strict xfail fail. Please remove the marker in the same PR (look for the slow-trickle test in tests/windows/test_windows_headless.py).
  • Timing margin in test_receive_timeout_bounds_entire_frame: it asserts time.monotonic() - started < 0.6 after a short timeout. That is a wall-clock bound on a shared CI runner, so it can flake under load even when the code is correct. If you can restructure it so correctness doesn't depend on elapsed time (for example, assert that the call raises and that the total bytes delivered were less than a full frame, or use a generous margin), that would be great.

Happy to help if anything is unclear once you've retargeted.

@cw-jlu

cw-jlu commented Nov 10, 2026

Copy link
Copy Markdown
Author

Thanks for the detailed review, and for trial-merging this onto dev and adding the configurable receive timeout and the Windows headless suite. That makes the slow-trickle case much easier to exercise end to end.

I've noted the three requested changes: retarget the PR to dev, remove the strict xfail from the slow-trickle test in tests/windows/test_windows_headless.py, and make test_receive_timeout_bounds_entire_frame independent of elapsed wall-clock time (for example, by asserting that the receive fails before the full frame has been delivered, as you suggested).

In a local check, the commit applies cleanly onto current dev (ae8f767), and the focused framing tests pass there while failing on unpatched dev. Nothing has been pushed yet and the base branch is still main. I'll follow up with validation results once the branch has been updated against dev.

Replace the elapsed-time bound in test_receive_timeout_bounds_entire_frame
with an assertion that the receive gives up before the peer has delivered
the full frame, and drive the shared header/body deadline test with a fake
monotonic clock. Remove the strict xfail on the Windows slow-trickle test,
which this change makes pass.
@cw-jlu
cw-jlu force-pushed the fix/bridge-frame-deadline branch from 2b952f5 to 09e1fb0 Compare November 10, 2026 11:45
@cw-jlu
cw-jlu changed the base branch from main to dev November 10, 2026 12:10
…kle oracle

Add test_receive_deadline_boundary (fake clock; just-below/exactly-at/
just-above the deadline, both framing implementations). Exactly-at must give
up and must never hand the socket a non-positive timeout. Mutating
'remaining_time <= 0' to '< 0' in either file now fails it.

test_receive_timeout_bounds_entire_frame now also asserts the receiver held
on for ~the whole budget (lower bound only, so it cannot flake under load),
so a None caused by a disconnect/decode error no longer passes vacuously.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@blwfish

blwfish commented Nov 10, 2026

Copy link
Copy Markdown
Owner

Thanks for the quick turnaround, the single-deadline fix and the follow-up look good. I reviewed the diff and pushed one small test-only commit to your branch (4768032, `maintainerCanModify` was on). It does not touch the fix itself.

What I added

  • `test_receive_deadline_boundary`: fake clock, just below / exactly at / just above the deadline, for both framing implementations. Exactly-at must give up and must never hand the socket a non-positive timeout (`settimeout(0.0)` would silently make it non-blocking). Mutating `remaining_time <= 0` to `< 0` in either file now fails it.
  • `test_receive_timeout_bounds_entire_frame` now also asserts the receiver waited at least 90% of its budget. `None` is also what a disconnect or decode error returns, so without this the test could pass without exercising the deadline. It is a lower bound only, so it can't flake on a slow runner.

What I checked

  • Full unit suite on the branch: 2909 passed, ruff clean.
  • The 6 original deadline tests fail without the source change and pass with it.
  • `tests/windows` in fake-Windows mode against a real headless FreeCADCmd (macOS): 24 passed, 13 skipped. On plain `dev` the slow-trickle test is xfailed (as expected); with this PR it passes, so removing the strict xfail is correct. The 13 skips are the real-Windows-only tests, so the Windows CI job is still the real check.

Optional, not blocking: the `receive_message` docstrings in both files still describe `timeout` as a socket timeout. It now bounds the whole frame from the call onward, so it may be worth a line saying so.

Once the Windows job is green I'm happy to take it out of draft and merge.

@blwfish
blwfish marked this pull request as ready for review November 10, 2026 18:48
@blwfish
blwfish merged commit 4091704 into blwfish:dev Nov 10, 2026
19 checks passed
@blwfish blwfish mentioned this pull request Nov 10, 2026
blwfish added a commit that referenced this pull request Nov 10, 2026
The handler waited a fixed 30 s for each client's request frame, so the
single-deadline receive behavior (PR #121 / issue #120) could not be exercised
end to end through the real server in any reasonable time.

Add FREECAD_MCP_RECV_TIMEOUT: seconds to wait for one complete request frame
per connection (header and body share one deadline). Unset, empty or
whitespace-only keeps the 30 s default. Otherwise it must be a plain ASCII
decimal greater than 0 and at most 3600. Zero (which socket code treats as
non-blocking, not "no timeout"), negatives, NaN/inf, exponents, underscores and
non-ASCII digits are rejected. A bad value never silently becomes the default:
the module still imports (so a typo cannot take the whole addon down), and
start_server() reports the error on every platform and refuses to start.

The value is passed to the handler's only receive_message() call site. Documented
in AGENT-INSTALL.md.

Tests pin both sides of the 0 and 3600 boundaries, the ambiguous number forms,
import safety, that the default matches receive_message()'s own default, that the
configured value actually reaches receive_message, and that a bad value refuses
to start with Windows both on and off.

Verified against a real FreeCAD handler: with PR #121's change applied and the
timeout at 1.0, a request trickled over ~10 s is dropped at ~1.0 s and the next
request is served; without it the same request completes.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
blwfish added a commit that referenced this pull request Nov 10, 2026
…ws workflow

The handler has a distinct Windows code path (loopback TCP listener, per-launch auth
token with an icacls ACL, SO_EXCLUSIVEADDRUSE, pid liveness, instance stop) that had
never run on a real Windows host in CI. Add:

- tests/windows/: a harness that launches the real headless_server.py under a real
  FreeCADCmd, fully isolated, and drives it over TCP with the project's own framing.
  Each server gets its OWN temp tree (and so its own token file); stdout/stderr go to
  files, not pipes (a Windows pipe holds 4096 bytes); an autouse session gate aborts the
  run before any test if FreeCAD ignored the profile redirect (it would otherwise use the
  real profile, or a cwd-relative dir inside the checkout).
- Tests: token file, ACL signature (real Windows), a 9-way auth matrix incl. non-ASCII and
  lone-surrogate tokens, sync/async/poll round trip, exclusive bind (real Windows), token
  rotation, pid liveness boundaries (real Windows; W-7 and W-9b as strict xfails),
  framing at MAX-1/MAX/MAX+1 plus zero-length, truncated and short frames, the
  FREECAD_MCP_RECV_TIMEOUT receive deadline, and a stop/orphan check.
- Fake mode (FREECAD_MCP_WINDOWS_TESTS_FAKE=1): tests/windows/fakewin_headless.py forces
  the handler's Windows branch on Linux/macOS so the harness and the platform-independent
  tests can be developed locally. It proves logic only; Windows-only tests are skipped
  and fake results never count as Windows coverage.
- .github/workflows/windows-tests.yml: advisory (not in all-tests-pass). tier1 runs
  `-m windows_safe`; tier2 downloads the pinned FreeCAD, verifies the release-API digest
  (weekly builds publish no -SHA256.txt), and always runs bin\FreeCADCmd.exe, never the
  1.1.x root-level launcher shim. assert_tests_ran.py fails an all-skipped run.

Known-failing-by-design: test_slow_trickle_is_dropped_at_the_single_deadline is a strict
xfail until PR #121 lands (verified: applying #121 flips it to XPASS(strict)). Mutation
check: replacing the token check with `return True` fails all 9 bad-token tests.

Local fake-mode run: 23 passed, 13 Windows-only skipped, 1 xfailed.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
blwfish added a commit that referenced this pull request Nov 10, 2026
The handler waited a fixed 30 s for each client's request frame, so the
single-deadline receive behavior (PR #121 / issue #120) could not be exercised
end to end through the real server in any reasonable time.

Add FREECAD_MCP_RECV_TIMEOUT: seconds to wait for one complete request frame
per connection (header and body share one deadline). Unset, empty or
whitespace-only keeps the 30 s default. Otherwise it must be a plain ASCII
decimal greater than 0 and at most 3600. Zero (which socket code treats as
non-blocking, not "no timeout"), negatives, NaN/inf, exponents, underscores and
non-ASCII digits are rejected. A bad value never silently becomes the default:
the module still imports (so a typo cannot take the whole addon down), and
start_server() reports the error on every platform and refuses to start.

The value is passed to the handler's only receive_message() call site. Documented
in AGENT-INSTALL.md.

Tests pin both sides of the 0 and 3600 boundaries, the ambiguous number forms,
import safety, that the default matches receive_message()'s own default, that the
configured value actually reaches receive_message, and that a bad value refuses
to start with Windows both on and off.

Verified against a real FreeCAD handler: with PR #121's change applied and the
timeout at 1.0, a request trickled over ~10 s is dropped at ~1.0 s and the next
request is served; without it the same request completes.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
blwfish added a commit that referenced this pull request Nov 10, 2026
…ws workflow

The handler has a distinct Windows code path (loopback TCP listener, per-launch auth
token with an icacls ACL, SO_EXCLUSIVEADDRUSE, pid liveness, instance stop) that had
never run on a real Windows host in CI. Add:

- tests/windows/: a harness that launches the real headless_server.py under a real
  FreeCADCmd, fully isolated, and drives it over TCP with the project's own framing.
  Each server gets its OWN temp tree (and so its own token file); stdout/stderr go to
  files, not pipes (a Windows pipe holds 4096 bytes); an autouse session gate aborts the
  run before any test if FreeCAD ignored the profile redirect (it would otherwise use the
  real profile, or a cwd-relative dir inside the checkout).
- Tests: token file, ACL signature (real Windows), a 9-way auth matrix incl. non-ASCII and
  lone-surrogate tokens, sync/async/poll round trip, exclusive bind (real Windows), token
  rotation, pid liveness boundaries (real Windows; W-7 and W-9b as strict xfails),
  framing at MAX-1/MAX/MAX+1 plus zero-length, truncated and short frames, the
  FREECAD_MCP_RECV_TIMEOUT receive deadline, and a stop/orphan check.
- Fake mode (FREECAD_MCP_WINDOWS_TESTS_FAKE=1): tests/windows/fakewin_headless.py forces
  the handler's Windows branch on Linux/macOS so the harness and the platform-independent
  tests can be developed locally. It proves logic only; Windows-only tests are skipped
  and fake results never count as Windows coverage.
- .github/workflows/windows-tests.yml: advisory (not in all-tests-pass). tier1 runs
  `-m windows_safe`; tier2 downloads the pinned FreeCAD, verifies the release-API digest
  (weekly builds publish no -SHA256.txt), and always runs bin\FreeCADCmd.exe, never the
  1.1.x root-level launcher shim. assert_tests_ran.py fails an all-skipped run.

Known-failing-by-design: test_slow_trickle_is_dropped_at_the_single_deadline is a strict
xfail until PR #121 lands (verified: applying #121 flips it to XPASS(strict)). Mutation
check: replacing the token check with `return True` fails all 9 bad-token tests.

Local fake-mode run: 23 passed, 13 Windows-only skipped, 1 xfailed.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
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.

Bridge receive timeout resets on every fragment instead of bounding a complete frame

3 participants