-
Notifications
You must be signed in to change notification settings - Fork 29.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Crash in JSStreamSocket.finishShutdown #48519
Comments
downgrade for now |
I've found a way to reproduce this and I'm going to investigate and open a PR for a fix myself. Just some quick notes for reference, in case anybody else is interested:
I'll keep digging, my best guess is that there's some race between parallel TLS & HTTP/2 shutdown events that causes this, in which case I'd imagine it's a general issue for any JS stream sockets. Any clues from anybody who might know more would be very welcome 😄. |
I got back to this, and I can reproduce the issue reliably in a test, like so: const common = require('../common');
const fixtures = require('../common/fixtures');
if (!common.hasCrypto)
common.skip('missing crypto');
const assert = require('assert');
const net = require('net');
const tls = require('tls');
const h2 = require('http2');
const tlsOptions = {
key: fixtures.readKey('agent1-key.pem'),
cert: fixtures.readKey('agent1-cert.pem'),
ALPNProtocols: ['h2']
};
const netServer = net.createServer((socket) => {
socket.allowHalfOpen = false;
// ^ This allows us to trigger this reliably, but it's not strictly required for the bug.
// In my production case, skipping this just fails elsewhere later.
h2Server.emit('connection', socket);
});
const h2Server = h2.createSecureServer(tlsOptions, (req, res) => {
res.writeHead(200);
res.end();
});
h2Server.on('connect', (req, res) => {
res.writeHead(200, {});
netServer.emit('connection', res.stream);
})
netServer.listen(0, common.mustCall(() => {
const proxyClient = h2.connect(`https://localhost:${netServer.address().port}`, {
rejectUnauthorized: false
});
const proxyReq = proxyClient.request({
':method': 'CONNECT',
':authority': 'example.com:443'
});
proxyReq.on('response', common.mustCall(((response) => {
assert.equal(response[':status'], 200);
console.log('Tunnel created');
// Create a TLS socket within the tunnel, and start sending a request:
const tlsSocket = tls.connect({
socket: proxyReq,
ALPNProtocols: ['h2'],
rejectUnauthorized: false
});
// Immediately kill the TLS socket:
tlsSocket.destroy();
// => async CRASH
})));
})); This crashes uncatchably with Node v18+ but runs without errors in Node v16. I'll open a PR with that soon, I need to think a little more about the fixes. The error here is on the server side, when proxying a stream over HTTP/2, but I think this is a general race condition that could affect any use of JS Stream Socket. The stack of streams in this case is:
A requirement to easily reproduce this is that the TLS stream has While the example ends up complicated, that's just because this setup ensures it fails 100% of the time - I think there's a fundamental race here, and most other uses of JS Socket Stream (JSS) can fail due to this with the right timing. What's going wrong here is:
This didn't happen before 9ccf8b2, because with This is easiest to reproduce with I've got quite a few working solutions to this - the challenging is finding the 'right' one. I'll open a PR for that tomorrow. |
Fixed by #49400 |
Version
v18.16.0
Platform
Both Windows + Linux, many different devices
Subsystem
net
What steps will reproduce the bug?
Unfortunately, I can't reproduce this myself. It's happening at frequent intervals to many of my users though, crashing Node on their machines when running httptoolkit-server (an HTTP debugging proxy) locally.
This issue has just appeared (10s of times a day now) after deploying an update from Node v16.20.1 to v18.16.0.
Internally, this code uses https://github.com/httptoolkit/mockttp/ which does plenty of interesting things with networking & streams that could be related, including running TLS, HTTP and WebSockets on top of other HTTP/1 connections & HTTP/2 streams for traffic proxying purposes. The relevant code for that is here: https://github.com/httptoolkit/mockttp/blob/main/src/server/http-combo-server.ts.
How often does it reproduce? Is there a required condition?
From the direct reports I've had, it seems this happens intermittently after handling a few minutes of HTTP traffic. The client is Chrome is every case I'm aware of, configured to proxy traffic via Node.
There's no obvious trigger on the server side - each user seems to be doing entirely different things there - but it's likely that a good amount of live web traffic is travelling through this stream so some kind of race condition seems very plausible.
What is the expected behavior? Why is that the expected behavior?
It shouldn't crash! 😄
What do you see instead?
It crashes. The full stack trace I have is:
Additional information
Obviously this isn't the most helpful bug report - there's no repro, and I have no easy way to quickly test possible solutions.
That said, I'm hoping somebody might be aware of what changes could be related and triggering this that I can investigate, or might know more about possible race conditions in the way that
finishShutdown
is used that can be tied up to avoid issues like this.From a quick skim of the JSStreamSocket code, this looks suspicious to me:
finishShutdown
is being called withnull
as the first argument.doShutdown
here which meansthis._handle
was null whendoShutdown
was called.finishWrite
would do this if a shutdown was deferred to wait for a write to complete (there's a TODO indoShutdown
from @addaleax specifically pointing out that this is a bit messy).doClose
,this._handle
will be null here immediately before a call tofinishWrite
.So, AFAICT: if something calls
doShutdown
with a pending write (deferring the shutdown) and then callsdoClose
before the shutdown completes,doClose
will clear_handle
and then callfinishWrite
, and that will then calldoShutdown
to finish the pending shutdown, which will crash.Does that make sense? Is there a reason that can't happen? There may be other cases but from the outside that seems like the clearest series that would lead to a crash here.
If that's the cause, some possible fixes:
It would seem easy to just pass
handle
intodoShutdown
(everywhere we call it here, we know we have the handle still in scope) but I don't know if that has larger implications - it looks likedoShutdown
is part of the interface used elsewhere so we can't just change it.Maybe we can just return out of
doShutdown
if thethis._handle
is alreadynull
- that means something else is already shutting everything down, so we shouldn't get involved (in this case,doClose
callsfinishShutdown
for us straight after callingfinishWrite
so there is indeed no need for this).Maybe
doClose
should check for this - if you callfinishWrite
with a shutdown pending after destroying the stream, you're in trouble.doClose
callsfinishShutdown
itself anyway, so it would seem reasonable for it to clear pending shutdowns because it's going to handle the shutdown itself.The only change in this file between these versions is 9ccf8b2 from @lpinca, which did change the logic directly around the failing code! Plausibly switching from setImmediate to process.nextTick meant that the IO events triggering this are now firing in a different order, causing the above race? Seems we should protect against the race regardless, but it's a plausible trigger imo.
The text was updated successfully, but these errors were encountered: