Skip to content

feat(async): fail require when upstream Async contracts change - #88

Merged
jimeh merged 4 commits into
mainfrom
feat/upstream-contract-guards
Nov 16, 2026
Merged

jimeh merged 4 commits into
mainfrom
feat/upstream-contract-guards

Conversation

@jimeh

@jimeh jimeh commented Nov 16, 2026 •

Copy link
Copy Markdown
Owner

Change

Item 3 of the plan in #68 (comment). connectrpc-async and connectrpc-falcon still replace or call some async-http and protocol-http2 hooks, and CompletionJoin relies on how Async finalizes tasks. If Async stops calling a task's per-instance #consume after clearing its fiber, retirement joins wait forever. The investigation's P9 simulation reproduced that hang. This PR makes those dependencies fail loudly at require, before anything can hang. It is the guard that item 5 needs before async can move from ~> 2.46.0 to >= 2.46.0, < 3. No gemspec constraint changes here.

Boot-time guard (connectrpc/async/upstream_contract.rb)

require "connectrpc/async" now runs two checks once per process:

  • Hook shape checks. Each check covers one hook's defining owner, visibility and parameter shape. Positional parameter names are ignored. There are 25 checks, covering:

    • the async-http HTTP/2 Output private stream/passthrough and start(parent:);
    • read_in_background, synchronize and close on the client and server connections;
    • Client#create_response, Server#each(task:), protocol-http2 create_stream and send_goaway;
    • the stream methods we replace without super (prepare_input, process_data, send_body, send_headers, finish_output, request_window_update) on request and response streams.

    Hooks that items 1, 2 and 4 (refactor(falcon): stop overriding private HTTP/1 writer and wrapper state #82, refactor(async): use public upstream readers and own the HTTP/2 frame guard #83, refactor(async): own the HTTP/1 client stream #85) removed are not guarded.

  • Finalization probe. It runs in a dedicated thread with its own Sync reactor, so it never touches the caller's scheduler or task tree. It checks what CompletionJoin relies on, for tasks that end by normal return, by a raised failure and by cancellation:

    • A task's per-instance #consume runs after its fiber is cleared, and the task is finished at that point.
    • A parent whose block ends while a child still waits stays unfinished. Its #consume runs again, now finished, when the child ends.
    • An object pushed to Fiber.scheduler from inside #consume runs on a later iteration, after the task's original fiber is dead, and not inline.

    The whole probe, including reactor teardown and the grace for stopping its thread, has a 5 s budget. Async's own teardown depends on the same node consumption the probe checks, so a broken contract can hang it. In that case require still raises within the budget. The error lists the failures the probe already recorded, then a line naming the step that did not finish and saying that Async could not stop the probe thread. That thread then stays asleep for the rest of the process; it uses no CPU and does not block exit. If the probe crashes, the error names the step that crashed.

Every broken contract goes into one ConnectRPC::Async::UpstreamContractError, which is a private constant like LifecycleError. The message names each contract, the installed gem version, the versions this package was verified with, and #68. For example, with the P9 simulation:

connectrpc-async 0.1.2 is incompatible with the installed socketry gems:
  - async 2.46.0: Task finalization after normal return no longer calls a per-instance #consume once the task's fiber is cleared (observed no #consume call)
  - async 2.46.0: Task finalization after a raised failure no longer calls ...
  - async 2.46.0: Task finalization after cancellation no longer calls ...
Verified with async 2.46.0, async-http 0.105.0, protocol-http2 0.29.2. See https://github.com/jimeh/connectrpc-ruby/issues/68.

There is no switch to skip the check, because skipping it would bring the hang back. require "connectrpc/falcon" checks async-http Endpoint#build_endpoint and the protected #tcp_endpoint the same way.

Scope. The guards cover the patch-pinned socketry hooks (async-http, protocol-http2) and Async's finalization contract. Falcon's own overrides of the falcon gem (Service#run, Server#accept/#call, and the Environment methods) are deliberately not guarded. falcon follows the floor policy, so a shape check would fail boot on harmless changes its version range allows.

One CompletionJoin. Falcon's copy is now a private alias of ConnectRPC::Async::Internal::CompletionJoin. The two copies were identical apart from the namespace and the observation ivar. Every join on a task now shares one observation (@connectrpc_async_completion). The Falcon example that checked two independent observers now checks that the Falcon and Async joins share one. The class stays internal; no hosting SPI is added.

upstream_contract_spec.rb per gem. Each example pins one upstream behavior, so a pin bump or the future pins-lifted lane (item 6) shows exactly which contract broke:

  • Relied on: failure means we are broken.
  • Worked around: failure means upstream fixed it, and the example names the workaround to delete:
    • consume_local_window lets the receive window go negative;
    • send_window_update writes before it expands the window;
    • read_in_background and Output#start run their first pass before the task handle exists;
    • write_body(nil) on a request emits content-length: 0;
    • async-http's build_endpoint builds TLS in the calling process.
  • Perturbed at require: these run in a subprocess with a deadline and require the named error, so the process must fail with that error rather than just exit. They cover:
    • P9;
    • a cancellation-only bypass;
    • removed ancestor #consume propagation;
    • a no-op Node#consume that also hangs teardown;
    • a push that transfers inline;
    • a probe crash, which must name the crashing step;
    • a hook visibility flip and signature change in both gems.

Refs #68. The issue stays open for items 5 and 6.

Verification

  • CI: all required checks pass on 736ce14, including the async and Falcon spec, package and Go interop lanes on Ruby 3.4.12 and 4.0.9, and the lowest-allowed async and Falcon lanes.
  • Local runs on the first two commits, rebased onto 712aace: mise run async:verify passed with 916 examples and 0 failures, and mise run falcon:verify passed with 404 examples and 0 failures. Both cover specs, build, isolated packages and Go interop. The two correction commits after that change only the contract module, its spec and surface.json.
  • Local runs on 736ce14: the async contract spec file passes with 34 examples and 0 failures. mise run rbs:check, mise run check and mise run async:check pass.
  • P9 against the existing suite: with the investigation's P9 edit applied to a scratch copy, the async retirement_preemption_spec run fails while loading, with the UpstreamContractError above, in 0.8 s. Before this change, the same simulation made 4 joins time out.
  • No-op Node#consume: require raises after 5.04 s. The error lists the three parent-#consume failures and the teardown timeout line.
  • Perturbations of the new specs:
    • With verify! disabled, the subprocess examples fail at the error-class assertion.
    • With a write that acquires async-http's @write_frame_guard, the frame-guard example fails at not_to include(:upstream).
    • Before their fixes, the teardown-hang example failed at the missing recorded-failure line, and the crash-step example failed with "while closing its reactor".
    • With the old kill grace added on top of the budget, the kill-resistant stub fails its 0.45 s bound.
  • Cost: in fresh processes, the first verify! takes 1.2–1.4 ms, including the probe thread and reactor. Hook checks account for about 70 µs of that. There is no per-request cost.

Limits:

  • The probe can only detect a finalization change that its scenarios exercise; the subprocess perturbations cover the ones known so far.
  • TESTED is a hardcoded list. A spec fails if a listed version no longer satisfies the gemspec, so a pin bump has to update the list.
  • The 5 s budget is wall-clock time. With dozens of CPU-bound Ruby threads competing during require, the probe takes longer. In testing this produced no false failure, because the waiting thread was starved just as much.
  • If a probe step crashes and teardown then raises a second, different exception, the error names the first step with the second exception.

Written on behalf of jimeh by claude-opus-5-5 using Claude Code via T3 Code.

Check the private async-http/protocol-http2 hooks we replace or call, and
Async's task finalization contract that CompletionJoin relies on, once at
require. A change now raises one named UpstreamContractError instead of
hanging retirement joins. Falcon shares connectrpc-async's CompletionJoin
and checks its Endpoint hooks the same way. Per-gem upstream_contract_spec
pins the behaviors relied on and the upstream bugs worked around.

Refs #68
…ream

#85 owns HTTP1Stream and covers its TLS read errors directly, so drop the
duplicate transport-level spec.
Teardown of the probe reactor depends on the same node consumption the
probe checks, so a broken contract can hang it. Report the failures
already recorded with the timeout, keep the kill grace inside the 5 s
budget, say when Async could not stop the probe thread, and report an
unknown gem version instead of raising KeyError.
@jimeh
jimeh force-pushed the feat/upstream-contract-guards branch from 42201b7 to 070ca69 Compare November 16, 2026 16:11
@jimeh

jimeh commented Nov 16, 2026

Copy link
Copy Markdown
Owner Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Nov 16, 2026 •

Copy link
Copy Markdown
Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@coderabbitai

coderabbitai Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Summary

Summary by CodeRabbit

  • Reliability
    • Added startup checks for compatibility with required async networking and task-scheduler behavior. Incompatible setups now report detailed errors when the library loads.
    • Checks are time-bounded, with diagnostics if task-finalization checks exceed their limit.
  • Documentation
    • Added guidance on startup checks, reported errors, and compatibility boundaries for Async and Falcon.

The PR adds require-time upstream contract checks to connectrpc-async and connectrpc-falcon. Async checks hook contracts and task-finalization and scheduler behavior. Falcon delegates CompletionJoin to Async. New specs, package manifests, README text, and type-surface metadata cover these changes.

Changes: Upstream Contract Checks and Completion Join

Layer / File(s) Summary
Async contract verification and load integration
gems/connectrpc-async/lib/connectrpc/async/upstream_contract.rb, gems/connectrpc-async/lib/connectrpc/async.rb, gems/connectrpc-async/scripts/package.rb, typecheck/surface.json, gems/connectrpc-async/README.md
Adds checks for upstream hook visibility, parameter shape, and ownership. A bounded probe checks task-finalization and scheduler behavior. The Async entry point runs verification during load.
Async contract validation
gems/connectrpc-async/spec/support/perturbed_require.rb, gems/connectrpc-async/spec/upstream_contract_spec.rb
Adds specs for hook and task contracts, HTTP stream behavior, load-time failures, and recorded upstream workarounds.
Falcon guard and shared completion join
gems/connectrpc-falcon/lib/connectrpc/falcon/upstream_contract.rb, gems/connectrpc-falcon/lib/connectrpc/falcon.rb, gems/connectrpc-falcon/lib/connectrpc/falcon/completion_join.rb, gems/connectrpc-falcon/spec/retirement_preemption_spec.rb, gems/connectrpc-falcon/spec/upstream_contract_spec.rb, gems/connectrpc-falcon/scripts/package.rb, typecheck/surface.json, gems/connectrpc-async/lib/connectrpc/async/completion_join.rb
Adds Falcon's delegated upstream guard and aliases its CompletionJoin to Async's implementation. Specs check Falcon's HTTP/1 contracts and shared completion observation.

Estimated code review effort: 4 (Complex) | ~45 minutes

Change: Feature

Possibly related issues

  • Issue 68 tracks specs for upstream behavior used by the Async stack. This PR adds those contract specs and load-time checks.

Possibly related PRs

  • jimeh/connectrpc-ruby#49: Both PRs change Falcon completion and join behavior; this PR makes Falcon joins use Async's shared completion observation.
Merge Risk: Minimal · up to 736ce

Loading the Async and Falcon integrations now checks the upstream library contracts they depend on and fails fast with a clear error, instead of letting connection retirement hang later. Falcon also now shares Async's completion-tracking logic. No concrete defects were found, and the change is easy to revert, so it appears ready to merge.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title "feat(async): fail require when upstream Async contracts change" follows the conventional commits format (type, scope, description). It matches the main change: the require-time upstream con…
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.
Description check Passed The description follows the template's Change and Verification sections. The Change section explains the boot-time guard, the Falcon CompletionJoin alias, scope limits, and the reason for no skip swit…
Full details: Title check

Explanation

The title "feat(async): fail require when upstream Async contracts change" follows the conventional commits format (type, scope, description). It matches the main change: the require-time upstream contract guard in connectrpc-async that raises an error when hooks or Async task finalization differ from what the gem expects.

Full details: Description check

Explanation

The description follows the template's Change and Verification sections. The Change section explains the boot-time guard, the Falcon CompletionJoin alias, scope limits, and the reason for no skip switch. The Verification section lists CI results, local runs with example counts, perturbation checks, cost, and limits. It is thorough and specific, so it is mostly complete. Minor concern: the Verification section cites a local run on the first two commits and other runs on a later commit, but the claims are reported as evidence and do not show a missing required section.

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Central YAML (base), Organization UI (inherited)
  • Review profile: CHILL
  • Plan: Team
  • Run ID: 875f5d18-9ab1-4fb2-a404-4a007b0197a8
Commits

Reviewing files that changed from the base of the PR and between 66a03a3 and 736ce14.

Files selected for processing: 14

  • gems/connectrpc-async/README.md
  • gems/connectrpc-async/lib/connectrpc/async.rb
  • gems/connectrpc-async/lib/connectrpc/async/completion_join.rb
  • gems/connectrpc-async/lib/connectrpc/async/upstream_contract.rb
  • gems/connectrpc-async/scripts/package.rb
  • gems/connectrpc-async/spec/support/perturbed_require.rb
  • gems/connectrpc-async/spec/upstream_contract_spec.rb
  • gems/connectrpc-falcon/lib/connectrpc/falcon.rb
  • gems/connectrpc-falcon/lib/connectrpc/falcon/completion_join.rb
  • gems/connectrpc-falcon/lib/connectrpc/falcon/upstream_contract.rb
  • gems/connectrpc-falcon/scripts/package.rb
  • gems/connectrpc-falcon/spec/retirement_preemption_spec.rb
  • gems/connectrpc-falcon/spec/upstream_contract_spec.rb
  • typecheck/surface.json

Included review availability: This review used your included allowance. 5 included reviews remain after this review. Your included PR review attempts over the past 7 days set your current allowance at 8 reviews per hour.

@jimeh
jimeh marked this pull request as ready for review November 16, 2026 16:32
@jimeh
jimeh merged commit 83b817e into main Nov 16, 2026
49 checks passed
@jimeh
jimeh deleted the feat/upstream-contract-guards branch November 16, 2026 16:33
@jimehbot jimehbot Bot mentioned this pull request Nov 16, 2026
jimeh added a commit that referenced this pull request Nov 16, 2026
connectrpc-async's protocol-http1 and io-stream and connectrpc-falcon's
protocol-http1 now follow the floor-and-cap policy, since #82, #83 and #85
removed their private coupling. async moves to >= 2.46.0, < 3 under a new
guarded-coupling tier, justified by #88's require-time finalization probe.
async-http and protocol-http2 stay patch-pinned, with their reasons in the
gemspec and docs/development.md.

The daily, non-gating canary workflow and canary:* tasks lift the remaining
patch pins, run the Async and Falcon upstream contract specs and suites under
wall-clock deadlines, and classify which contract broke.

Refs #68
jimeh added a commit that referenced this pull request Nov 16, 2026
)

* build: loosen freed socketry pins and add a pins-lifted canary lane

connectrpc-async's protocol-http1 and io-stream and connectrpc-falcon's
protocol-http1 now follow the floor-and-cap policy, since #82, #83 and #85
removed their private coupling. async moves to >= 2.46.0, < 3 under a new
guarded-coupling tier, justified by #88's require-time finalization probe.
async-http and protocol-http2 stay patch-pinned, with their reasons in the
gemspec and docs/development.md.

The daily, non-gating canary workflow and canary:* tasks lift the remaining
patch pins, run the Async and Falcon upstream contract specs and suites under
wall-clock deadlines, and classify which contract broke.

Refs #68

* fix(canary): fail phases on leftover output holders and keep failure detail

A phase whose command exits while a descendant still holds its output now
stops reading after a short drain grace and fails, and one that exhausts its
deadline while draining reports a timeout. The report script aborts with its
reason instead of ending on a backtrace, and errors raised outside examples
no longer hide the failed example ids.

* fix(canary): attribute each hook error to its own hook

Parse each RSpec hook-error block up to its own backtrace, so an expectation
failure in one hook no longer borrows the error class of a later hook, and
report a failed expectation by its first message line.

* fix(canary): report a multiline hook expectation by its diagnostic

RSpec prints a multiline source snippet before a failed expectation's
diagnostic, so take the first line after the snippet's blank line.

* fix(canary): keep quoted RSpec rerun ids in failure details

RSpec reruns examples that share a location by id and quotes it unless the
shell allows unquoted ids, so accept both forms. Also compare the canary's
full scheduled time with CI's and check that a leftover descendant is gone
after a drain timeout.
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