Skip to content

tls: stop emitting data after ReadStop() - #67676

Open
barathraj048 wants to merge 1 commit into
nodejs:mainfrom
barathraj048:tls-readstop-pipelined
Open

barathraj048 wants to merge 1 commit into
nodejs:mainfrom
barathraj048:tls-readstop-pipelined

Conversation

@barathraj048

Copy link
Copy Markdown
Contributor

When JS calls ReadStop() on a TLS socket, TLSWrap only stopped the TCP socket underneath. Records that were already received kept going through ClearOut() and were handed to JS anyway.

I ran into this through the HTTP server. When a pipelined request comes in while a big response is still being sent, the server pauses the socket and the parser. Over TLS, the rest of the request was still pushed into the paused parser, which failed with HPE_PAUSED and the server dropped the connection. Plain HTTP was fine.

With this change, ClearOut() stops after an emit if ReadStop() was called, and leaves the remaining records in enc_in_. ReadStart() then runs a cycle to deliver them, since the client may not send anything else that would trigger it. The cycle runs after the TCP socket is started again, because JS can pause and resume while handling the data, and starting the socket twice fails with EALREADY.

Fixes: #67455
Refs: connectrpc/connect-es#1884

Testing

Tested on Linux (Ubuntu 24.04 on WSL2, x64).

  • The new test test-https-pipeline-paused-parser.js fails without this change (clientError with HPE_PAUSED) and passes with it.
  • It uses two body sizes. With the large body, more data keeps arriving from the network. With the small body, the whole request arrives in one read, so nothing else would deliver the held records. An earlier version of my fix only stopped ClearOut() and hung in that case, which is why ReadStart() now runs a cycle.
  • test-tls-*, test-https-*, test-http2-* (parallel and sequential): 644 passed.
  • Full make test-only: 7628 passed. 6 test-domain-*abort* tests timed out under load because of WSL's crash handler (/wsl-capture-crash). They pass when run alone and don't use TLS.

Notes for reviewers

  • I could not reproduce the bug on Windows. There, the server's writes complete without backpressure in this setup, so the server never pauses. The test passes on Windows with and without the fix.
  • connect-es worked around this by adding a data listener on the TLS socket, which unconsumes the parser. Users of https.createServer() can still hit it without that workaround.

When JS calls ReadStop() on a TLS socket, TLSWrap only stopped the
TCP socket underneath. Records that were already received kept going
through ClearOut() and were handed to JS anyway.

I ran into this through the HTTP server. When a pipelined request
comes in while a big response is still being sent, the server pauses
the socket and the parser. Over TLS, the rest of the request was
still pushed into the paused parser, which failed with HPE_PAUSED and
the server dropped the connection. Plain HTTP was fine.

With this change, ClearOut() stops after an emit if ReadStop() was
called, and leaves the remaining records in enc_in_. ReadStart() then
runs a cycle to deliver them, since the client may not send anything
else that would trigger it. The cycle runs after the TCP socket is
started again, because JS can pause and resume while handling the
data, and starting the socket twice fails with EALREADY.

Fixes: nodejs#67455
Signed-off-by: Barath <barathraj048@gmail.com>
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/crypto

@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. crypto Issues and PRs related to the crypto subsystem. needs-ci PRs that need a full CI run. labels Nov 21, 2026
@codecov

codecov Bot commented Nov 21, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 88.88889% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 90.60%. Comparing base (5884b8e) to head (9a93873).
⚠️ Report is 14 commits behind head on main.

Files with missing lines Patch % Lines
src/crypto/crypto_tls.cc 88.88% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #67676      +/-   ##
==========================================
- Coverage   90.71%   90.60%   -0.12%     
==========================================
  Files         801      801              
  Lines      280906   281062     +156     
  Branches    54249    54252       +3     
==========================================
- Hits       254832   254663     -169     
- Misses      16246    16576     +330     
+ Partials     9828     9823       -5     
Files with missing lines Coverage Δ
src/crypto/crypto_tls.h 90.62% <ø> (ø)
src/crypto/crypto_tls.cc 79.38% <88.88%> (+0.01%) ⬆️

... and 53 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@jasnell

jasnell commented Nov 22, 2026

Copy link
Copy Markdown
Member

This might be semver-major... it's not clear.

@nodejs/security @nodejs/crypto @nodejs/http

@barathraj048

Copy link
Copy Markdown
Contributor Author

Thanks for looking at this! I wasn't sure about semver either, so here's what actually changes, to make it easier to decide:

  1. Once ReadStop() is called, TLSWrap doesn't hand any more data to the stream listener. Before, records that came in the same network read could still get through, which is what broke the paused HTTP parser. This affects anything that reads from the TLS stream, including the consumed paths in the HTTP parser and HTTP/2. The test-tls-*, test-https-* and test-http2-* tests all pass with the change.

  2. ReadStart() can now emit data right away (synchronously) if some records were held back during the pause. Before, that data only showed up with the next read from the network, or never if the client didn't send anything else. I think this is the part that matters most for semver.

About memory: while reading is stopped, the TCP stream underneath is stopped too, so enc_in_ only holds what had already arrived before the pause. It doesn't keep growing.

If the synchronous emit in ReadStart() is a problem, I can change it to run later with ScheduleDeferredCycle(), so the data arrives asynchronously like before. Happy to do that if you think it's the safer option.

Also, the failing FIPS job (test-process-exit-background-gc, PBKDF2 derivation failed) isn't related. It was fixed on main in 93078b4 after I created this branch.

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

Labels

c++ Issues and PRs that require attention from people who are familiar with C++. crypto Issues and PRs related to the crypto subsystem. needs-ci PRs that need a full CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

https: pipelined request during a response drain fails with HPE_PAUSED

3 participants