Skip to content

ngh2: a shim in front of nghttp2 calls - #23504

Closed
icing wants to merge 8 commits into
curl:masterfrom
icing:ngh2-shim
Closed

icing wants to merge 8 commits into
curl:masterfrom
icing:ngh2-shim

Conversation

@icing

@icing icing commented Oct 20, 2026

Copy link
Copy Markdown
Contributor

Prevent further calls into an nghttp2 session once a fatal error was returned. Use in our HTTP/2 filters.

icing added 3 commits October 20, 2026 11:49
Prevent further calls into an nghttp2 session once a
fatal error was returned. Use in our HTTP/2 filters.
@icing
icing requested a review from cmeister2 October 20, 2026 10:09
@icing

icing commented Oct 20, 2026

Copy link
Copy Markdown
Contributor Author

@cmeister2 would appreciate if you could throw this one into your grinder. Thanks!

@cmeister2

Copy link
Copy Markdown
Contributor

This is a really nice change! Centralizing the fatal-session contract in a small shim is much cleaner and more robust than trying to quarantine a broken session at every caller.

I ran f15d494d through the contract-checking branch from curl/curl-fuzzer#466 with ASan and nghttp2 1.70.0. The results are very encouraging:

  • 5144840472690688 now passes full LSan; the baseline 16-byte leak after fatal nghttp2_session_mem_recv2() is gone.
  • 5002334263115776 and 6416044626083840 pass full LSan and traced runs, including the -902 callback failure and -901 OOM paths.
  • 4824563998064640 passes the contract/traced run with leak detection disabled. It still hangs during LSan process exit in this combined instrumentation build, but emits no contract assertion.
  • The contract-shim unit test passes.

So this fixes the concrete post-fatal sequences we found (want_write after -905, set_stream_user_data after -901, and check_request_allowed after -902). I am very happy with this direction.

There is one gap I think we should close before calling the session boundary complete. on_data_chunk_recv() still has:

  • a raw nghttp2_session_consume() in the unknown-stream branch, with its return value ignored; and
  • a wrapped Curl_ngh2_session_consume() in the known-stream branch whose return value is also ignored.

nghttp2_session_consume() can return fatal NGHTTP2_ERR_NOMEM. In the wrapped case the shim records that, but the callback returns 0, so the enclosing nghttp2_session_mem_recv() can continue inside nghttp2 after the fatal result. In the raw case the shim does not record it at all. Could both branches use the shim and turn a fatal consume result into NGHTTP2_ERR_CALLBACK_FAILURE from the callback?

I also found these remaining raw session calls in callbacks, which bypass curl_ngh2:

  • nghttp2_session_get_remote_settings() at lines 1225 and 1227;
  • nghttp2_session_set_stream_user_data() at lines 1367 and 1397; and
  • nghttp2_submit_rst_stream() at line 1474.

Those look lower risk than the ignored consume() result, but wrapping them (or explicitly documenting why they are safe exceptions) would make the shim's invariant much easier to reason about and keep the fuzzer's API-surface check useful.

With those covered, this looks like an excellent replacement for #23397. I would also love to retain/port its test1717 regression case.

@icing

icing commented Oct 20, 2026

Copy link
Copy Markdown
Contributor Author

Ignoring the return value of a shimmed call is fine, imo.

Route the remaining session operations in HTTP/2 callbacks through the nghttp2 shim so a fatal session cannot be reused. Add a regression test for starting a fresh transfer after a fatal CONTINUATION error.
@github-actions github-actions Bot added the tests label Oct 20, 2026
@testclutch

Copy link
Copy Markdown

Analysis of PR #23504 at 0f0f8ce7:

Test ../../tests/http/test_32_ftps_vsftpd.py::TestFtpsVsFTPD::test_32_04_download_10_parallel[data-1m] failed, but it has been 0.2% flaky lately, so it's probably NOT a fault of the PR. Note that this test has failed in 2 different CI jobs (the link just goes to one of them). Note that this CI job has had a number of other flaky tests recently (4, to be specific) so it may be that this failure is rather a systemic issue with this job and not with this specific PR.

Generated by Testclutch

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Cached fatal errors can remain unreported in receive and proxy egress paths, potentially stalling transfers.

2 open issues
What changed in this PR

Adds an nghttp2 shim to prevent further session calls after fatal errors in curl’s HTTP/2 filters.

Changes:

  • Tracks fatal session errors and guards subsequent calls.
  • Migrates HTTP/2 and proxy filters to the shim.
  • Adds a regression test for a fatal error followed by a successful transfer.
File Description
tests/​data/​test1717 Tests fatal CONTINUATION handling and a subsequent transfer.
tests/​data/​Makefile.am Registers the regression test.
lib/​ngh2_shim.h Declares guarded session interfaces.
lib/​ngh2_shim.c Implements fatal-error tracking and guards.
lib/​Makefile.inc Registers shim source and header.
lib/​http2.c Uses the shim for HTTP/2 sessions.
lib/​cf-h2-proxy.c Uses the shim for HTTP/2 proxy sessions.

Review effort: Balanced

Comment thread lib/cf-h2-proxy.c Resolved
Comment thread lib/ngh2_shim.c Resolved
icing added 2 commits October 21, 2026 10:56
- recheck fatal state after nghttp2 send/recv calls that
  may involve callbacks
- http2.c/cf-h2-proxy.c check fatal state after egress loop
Comment thread lib/cf-h2-proxy.c Resolved
@icing

icing commented Oct 21, 2026

Copy link
Copy Markdown
Contributor Author

@cmeister2 from my view it's ready for merge if it survives your torture chamber.

@cmeister2
cmeister2 requested review from cmeister2 and a balanced review from Copilot October 21, 2026 12:59

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Session lifecycle changes spanning callbacks, multiplexed streams, and proxy tunnels require final human validation.

No open issues 🎉

2 resolved since last review

Review effort: Balanced

@icing

icing commented Oct 21, 2026

Copy link
Copy Markdown
Contributor Author

@vszakats care to merge this?

@vszakats vszakats closed this in 2b59a7f Oct 21, 2026
@vszakats

Copy link
Copy Markdown
Member

Thanks to all, merged now!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Development

Successfully merging this pull request may close these issues.

5 participants