Skip to content

fix(messaging): report an expired deadline as operation_timeout when a stage timer fires first - #8850

Merged
lidge-jun merged 1 commit into
devfrom
fix/messaging-deadline-timer-race
Nov 16, 2026
Merged

lidge-jun merged 1 commit into
devfrom
fix/messaging-deadline-timer-race

Conversation

@lidge-jun

@lidge-jun lidge-jun commented Nov 16, 2026 •

Copy link
Copy Markdown
Owner

Summary

Fixes a dev-red flake from the lane=all run 43912748460, job test 5/5 (self-hosted Linux, dev 1d5c5c1c2b):

tests/codex-integration/messaging-local-lifecycle.test.ts:100
local RPC lifecycle > whole-operation deadline applies across requests and initialization
Expected substring: "deadline"
Received message: "Local Codex request timed out." (151.77 ms)

Cause. MessageBudget arms one timer for the whole operation deadline (operation_timeout). LocalMessageRpc arms a second timer per request (rpc_timeout) and for connect (daemon_unavailable), using budget.remainingMs(limit). When the remaining budget is shorter than the RPC limit, both timers end at about the same moment. On a loaded runner the stage timer can run first, so an expired deadline was reported as an RPC timeout or a connection timeout.

Change. MessageBudget.stageTimer(limit, onStageTimeout) arms the stage timer. If the deadline capped the timer, or the timer fires at or after the deadline, it ends the operation through the budget's existing abort path (operation_timeout, same message). Otherwise it calls the stage's own timeout. Both rpc.ts timers (connect and request) use it. A real per-request timeout shorter than the remaining budget still reports rpc_timeout. structure/local-messaging.md records the rule.

Verification

All test runs were single files under /usr/bin/lockf -k /tmp/ocx-test.lock.

  • Regression tests in tests/codex-integration/messaging-local-lifecycle.test.ts. They replace setTimeout/clearTimeout and performance.now with hand-driven versions, move the clock to the deadline, and fire the connect timer or the request timer before the budget's own timer. A third case pins a 100 ms RPC limit against a 30 s budget to rpc_timeout. The existing toThrow("deadline") assertion is unchanged.
    • Red on origin/dev source (5b864ad4c8): 11 pass / 2 fail. The connect case received Local Codex connection timed out; no daemon was started. The request case received Local Codex request timed out., the same message as in CI.
    • Green with the fix: 13 pass / 0 fail.
  • Every test file importing src/messaging or the messaging helpers (27 files), run one at a time. 25 passed, two of them with expected platform skips. Two remote files hit their 5 s per-test timeout while the host load average was about 32 to 34:
    • messaging-remote-replies.test.ts ("reply routes have fixed capacity…") times out the same way with src/messaging stashed back to origin/dev, so this change did not cause it.
    • messaging-remote-admission.test.ts ("outbound enrollment permits one correlated return reply…") timed out twice with the fix, then passed on rerun (4647 ms against its 5000 ms limit). Its success path never fires a connect or request timer, so it is sensitive to load.
  • bun run typecheck: no type errors.
  • bun run structure:check: passed.
  • bun run privacy:scan: passed.

NOT RUN: full bun run test (shared host, excluded by the task); macOS/Windows CI.

Follow-up outside this PR: src/messaging/remote-auth.ts:28 uses the same capped setTimeout(fail, budget.remainingMs(5000)) shape and could adopt stageTimer in a later change.

Checklist

  • Scope stays focused and avoids unrelated cleanup.
  • Docs or release notes were updated when needed.
  • Security-sensitive changes were reviewed for secrets, auth, and unsafe defaults.

Summary by CodeRabbit

  • Bug Fixes
    • Operations that reach their overall deadline now consistently report an operation timeout, even when a connection or request timer fires first.
    • Shorter connection or request limits continue to report their specific timeout.
  • Documentation
    • Clarified how operation deadlines and connection or request timeouts interact.

…a stage timer fires first

A connect or request timer capped by the remaining operation budget could
run before the budget's own timer on a loaded event loop, relabelling a
deadline expiry as rpc_timeout or daemon_unavailable. MessageBudget.stageTimer
now ends the operation through the budget when the timer was capped by, or
fires at or after, the deadline.

Fixes the lane=all run 43912748460 "test 5/5" flake in
messaging-local-lifecycle.test.ts.
@lidge-jun
lidge-jun requested a review from Ingwannu as a code owner November 16, 2026 18:02
@lidge-jun lidge-jun added the ci:full Run the four full Ubuntu test shards on this pull request label Nov 16, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 16, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-16T18:04:27.178493Z a68f03e PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@coderabbitai

coderabbitai Bot commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

MessageBudget now handles deadline expiry through a shared method and provides deadline-aware stage timers. LocalMessageRpc uses these timers for connection and request timeouts. Documentation and tests cover deadline-boundary outcomes and a request timeout that occurs before the operation deadline.

Changes: Message deadline handling

Layer / File(s) Summary
Centralize deadline expiry and stage timers
src/messaging/budget.ts (lines 18–32, 42–55)
MessageBudget uses expire() when its deadline elapses. The new stageTimer() expires the operation when the deadline caps the timer or the callback fires at or after the deadline; otherwise, it calls the stage-timeout callback.
Apply deadline-aware timers to RPC
src/messaging/rpc.ts (lines 56–58, 211–212), tests/codex-integration/messaging-local-lifecycle.test.ts (lines 1, 104–164), structure/local-messaging.md (lines 63–66)
Connection and request timers use stageTimer(). Tests cover connect and initialization-request timers firing at the operation deadline, and a request timeout that occurs before the deadline. The lifecycle documentation describes these outcomes.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Suggested labels: bug

Suggested reviewers: phillipod

Merge Risk: Minimal · up to a68f0

This change makes an expired operation deadline report operation_timeout consistently when a connect or request timer fires first. Regression tests cover the boundary cases and the earlier per-request timeout case. No merge-blocking risk was found.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the main change: a stage timer (connect or request) that fires at or after the operation deadline now reports operation_timeout. The raw summary shows MessageBudget.stageTimer, its use…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the main change: a stage timer (connect or request) that fires at or after the operation deadline now reports operation_timeout. The raw summary shows MessageBudget.stageTimer, its use in rpc.ts for connect and request timers, the expire() routing, and the matching doc and test updates. The title matches the changeset and the PR objectives.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 5b864ad and a68f03e.

Files selected for processing: 4

  • src/messaging/budget.ts
  • src/messaging/rpc.ts
  • structure/local-messaging.md
  • tests/codex-integration/messaging-local-lifecycle.test.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 3 remain after this review.

@github-actions

Copy link
Copy Markdown
Contributor

✅ Deterministic PR hygiene checks passed.

@github-actions github-actions Bot added the bug Something isn't working label Nov 16, 2026
@lidge-jun
lidge-jun merged commit 2589be5 into dev Nov 16, 2026
61 of 67 checks passed
@lidge-jun
lidge-jun deleted the fix/messaging-deadline-timer-race branch November 16, 2026 18:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working ci:full Run the four full Ubuntu test shards on this pull request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant