Skip to content

refactor(falcon): stop overriding private HTTP/1 writer and wrapper state - #82

Merged
jimeh merged 3 commits into
mainfrom
refactor/falcon-public-api
Nov 16, 2026
Merged

jimeh merged 3 commits into
mainfrom
refactor/falcon-public-api

Conversation

@jimeh

@jimeh jimeh commented Nov 16, 2026 •

Copy link
Copy Markdown
Owner

Change

This is item 1 of the plan in #68: the optional Falcon gem stops overriding private protocol-http1 methods and stops writing Protocol::HTTP::Body::Wrapper state. The goal is that the later item that loosens Falcon's protocol-http1 pin (item 5) has no private touches left in this gem. Gemspec constraints are unchanged here. Intended behavior is unchanged.

Final-flush anchor (http1.rb). A successfully delivered response on a retiring HTTP/1 connection starts its single 500 ms close budget at the final flush, before body close callbacks such as rack.response_finished. That anchor used to come from an override of the private Protocol::HTTP1::Connection#flush_stream, plus per-connection EOF state. It now comes from HTTP1ResponseBody#close. The protocol-http1 v0.37.1 release note says the writer defers body.close in write_chunked_body, write_fixed_length_body and write_body_and_close until the response is fully written and flushed. A close without an error, after EOF or for a HEAD response, therefore marks the final flush. The hook calls final_flushed once and then closes the wrapped body, so callbacks still spend the same budget. A nil body goes through write_empty_body, which flushes but has no body to close, so write_body calls final_flushed after super returns normally.

write_body arity. The override now accepts and forwards extra positional arguments: (version, body, head = false, trailer = nil, *rest). protocol-http1 main adds a fifth request argument in socketry/protocol-http1#65, which is merged but not yet released.

CompletionBody (rack_guard.rb). The Rack fallback's completion wrapper used to call super with an empty buffered body and then assign @body after converting the Rack tuple. It now converts first and passes the result to super. If the conversion raises, it releases in the same order as close: completion callback, then the original body. One private helper now defines that order for both paths.

docs/design.md and docs/streaming.md now describe the body-close anchor instead of an EOF marker paired with the next flush. typecheck/surface.json drops the removed flush_stream and eof entries, records the new write_body parameters, and classifies HTTP1ResponseBody#initialize/#close and CompletionBody#complete as internal.

Decisions to review

  • Cancellation during the final flush. The writer's rescue => error catches only StandardError. When cancellation (Async::Stop) interrupts the final flush after EOF, the writer still closes the body with a nil error, so the hook calls final_flushed although the flush did not finish. The old override did not. Nothing observable changes: write_body raises, the owner closes undelivered, and an undelivered close ignores grace and tears down raw-first. A new example pins that outcome. I chose not to check $! in the hook. A stale $! would skip grace on a delivered response and abort a connection that should have half-closed gracefully.
  • HTTP/1.0 close-delimited bodies. write_body_and_close flushes, then half-closes the write stream, then closes the body. Grace now starts after that half-close instead of at the flush, which is later by the time one close_write takes. The other framing paths close the body right after the final flush. I accepted this rather than hook a private method again. After the final flush, close_write has nothing left to flush (io-stream 0.14 Writable#close_write). It then does a non-blocking shutdown(SHUT_WR), or for TLS one SSL_shutdown through OpenSSL::SSL::SSLSocket#close_write, which does not wait for the peer. The path also needs an HTTP/1.0 response of unknown length on a retiring Connect streaming route, and HTTP/1.0 Connect streaming POSTs are refused with 505 (http1_corrections_spec.rb).
  • Positional forwarding only. *rest covers the positional argument that upstream adds. Adding **options would trip Metrics/ParameterLists for a keyword signature that no upstream change proposes.

Which specs pin the old guarantees

  • spec/http1_flush_spec.rb "anchors the pinned writer's final flush before callbacks" (chunked, fixed, zero length, absent body, HEAD fixed, HEAD chunked, HTTP/1.0) pins that the expiry equals the last flush plus 500 ms and is already set when body callbacks run.
  • spec/http1_flush_spec.rb "never starts success grace after a failed final flush" pins that a StandardError during the final flush leaves no grace and no half-close.
  • spec/http1_flush_spec.rb "spends delayed callback and discard time within one total 500 ms budget" pins that callbacks and discard share one budget.
  • New: spec/http1_flush_spec.rb "stays undelivered when cancellation interrupts the final flush" covers the cancellation case above, which the existing examples did not.
  • New: an {empty: true} row in the anchor matrix covers the empty? branch (unknown length, body.empty? true), which goes through write_empty_body and now relies on the wrapper's empty? EOF tracking. The fixture's empty? was nil for every unknown-length row before.
  • spec/fallback_spec.rb (streamed, callable, array and conversion-error Rack bodies, including cancellation inside a conversion-error callback) and spec/body_cleanup_spec.rb pin CompletionBody's callback and original-close order.

Refs #68. This does not close it; the plan has six items.

Verification

On 4f5654a (Ruby 3.4.12): mise run falcon:verify passed, including falcon:test 395 examples with 0 failures, falcon:interop, falcon:interop:grpc (h2c, TLS, mTLS, keepalive age drain) and falcon:package.

On 2c9c8dd, which changes only docs and the flush spec: spec/http1_flush_spec.rb has 11 examples and 0 failures. With the wrapper's empty? EOF tracking removed, the new {empty: true} row fails at expect(@close_expires).to eq(flush_at + 0.5) (expected 10.5, got nil); restored and green. falcon:check and check:static report no offenses.

On ed98a5a, which changes only typecheck/surface.json: mise run rbs:check passed (109 files inventoried; public API consumers and the Falcon fallback consumers passed).

Before rebasing onto #78, which adds only docs/plans/interceptors.md:

  • mise run falcon:test -- spec/http1_flush_spec.rb: 10 examples, 0 failures, including the new example by name.
  • Perturbation: with write_body's ensure calling input_retirement&.close(true) instead of close(!!written), the new example fails at expect(events).not_to include([:shutdown, Socket::SHUT_WR]). Restored and green again.
  • mise run falcon:test -- spec/fallback_spec.rb spec/body_cleanup_spec.rb: 21 examples, 0 failures.
  • mise run falcon:test: 395 examples, 0 failures.
  • mise run falcon:check, mise run check:static: no offenses.
  • mise run falcon:interop: all pass (h2c, TLS and mTLS, both codecs). mise run falcon:conformance:http1: 160 of 160.

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

@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 not completed

Head commit changed.

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

  • Bug Fixes

    • Final-close expiry is now anchored consistently to response completion, including bodyless responses and HTTP/1.0 close-delimited responses.
    • Cancellation-related body closure no longer starts the expiry for a response that was not delivered.
    • If the final flush is interrupted, the connection is closed without recording a write shutdown.
  • Documentation

    • Updated the Falcon HTTP response lifecycle descriptions to clarify when final-close expiry begins.

Falcon HTTP/1 final-close signaling now starts after an error-free body close following EOF or HEAD, or when the writer returns for a bodyless response. The change also updates CompletionBody cleanup, tests, type-surface records, and documentation.

Changes: Falcon HTTP/1 final-close timing

Layer / File(s) Summary
CompletionBody initialization and cleanup
gems/connectrpc-falcon/lib/connectrpc/falcon/rack_guard.rb, typecheck/surface.json
CompletionBody obtains the converted body before superclass initialization and supplies an empty buffered body when the converted body is nil. Its completion helper runs the callback and ensures the original body is closed.
HTTP/1 final-close signaling
gems/connectrpc-falcon/lib/connectrpc/falcon/http1.rb, gems/connectrpc-falcon/spec/http1_flush_spec.rb, docs/design.md, docs/streaming.md, typecheck/surface.json
HTTP1ResponseBody signals final flush once on an error-free close after EOF or for HEAD. The writer signals after a successful nil-body write. Tests cover empty bodies and interruption during final flush. Documentation describes the body-close, bodyless-response, HTTP/1.0 half-close, and cancellation cases.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Refactor

Possibly related issues

  • jimeh/connectrpc-ruby issue 68: This change removes the Falcon flush_stream override, which is one of the couplings described in the issue. It does not remove the direct write to Protocol::HTTP::Body::Wrapper @body, and the issue remains open.
Merge Risk: Minimal · up to e1a0e

This refactor changes when Falcon starts its HTTP/1 final-close timer, without adding a known defect. No concrete merge-blocking risk was found, and the author's tests cover the changed cases.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title follows conventional commits format (type refactor with scope falcon) and matches the main change: the Falcon gem stops overriding private protocol-http1 HTTP/1 writer methods and wrappe…
Description check Passed The description follows the template with a Change section that explains the problem, the resulting behavior, and the design boundaries (gemspec constraints unchanged, intended behavior unchanged). It…
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 follows conventional commits format (type refactor with scope falcon) and matches the main change: the Falcon gem stops overriding private protocol-http1 HTTP/1 writer methods and wrapper state, as described in the objectives and summaries.

Full details: Description check

Explanation

The description follows the template with a Change section that explains the problem, the resulting behavior, and the design boundaries (gemspec constraints unchanged, intended behavior unchanged). It also has a Verification section listing commands, outcomes, example counts, and limits. Minor template deviation: the Verification section adds extra detail beyond the template, which is acceptable.

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: 4784b9ad-b1a8-4c74-8cd2-8cab62a99b50
Commits

Reviewing files that changed from the base of the PR and between a06fd6a and e1a0e4d.

Files selected for processing: 6

  • docs/design.md
  • docs/streaming.md
  • gems/connectrpc-falcon/lib/connectrpc/falcon/http1.rb
  • gems/connectrpc-falcon/lib/connectrpc/falcon/rack_guard.rb
  • gems/connectrpc-falcon/spec/http1_flush_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.

…tate

Anchor the retirement grace on the response body's clean close instead of
the private `flush_stream`; protocol-http1 v0.37.1+ closes the body only
after the final write and flush. Forward extra `write_body` arguments, and
build `CompletionBody`'s final body before `super` instead of assigning the
wrapper's `@body`.

Refs #68
…path

Cancellation can also close the body without an error, a bodyless response anchors when the writer returns, and the empty? branch now has its own flush-anchor case.
@jimeh
jimeh force-pushed the refactor/falcon-public-api branch from ed98a5a to e1a0e4d Compare November 16, 2026 14:37
@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.

@jimeh
jimeh marked this pull request as ready for review November 16, 2026 14:47
@jimeh
jimeh merged commit 60596ce into main Nov 16, 2026
49 checks passed
@jimeh
jimeh deleted the refactor/falcon-public-api branch November 16, 2026 14:57
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