Repository navigation
Description
Version
Tested with v20.7.0 and v18.17.1
Platform
Probably platform independent. Darwin MacBook-Pro-M1-Max.local 22.6.0 Darwin Kernel Version 22.6.0: Wed Jul 5 22:22:05 PDT 2023; root:xnu-8796.141.3~6/RELEASE_ARM64_T6000 arm64
Subsystem
http2
What steps will reproduce the bug?
When createConnection returns a socket that has connecting === true but no _handle set, the Http2Session constructor will wrap the socket with a JSStreamSocket and this wrapper will not reflect that the underlying socket is still connecting. The session will then emit a 'connect' event while the socket is still connecting.
Code demonstrating the issue when connecting to a port that is not being listened on:
const net = require('net');
const http2 = require('http2');
const session = http2.connect('http://[::1]:12345', {
createConnection: () => {
const socket = new net.Socket();
socket.connecting = true;
// socket._handle = { isStreamBase: true }; // Uncomment to confirm _handle makes a difference.
socket.on('error', err => {
console.error('socket:', err);
});
socket.on('connect', () => {
console.log('socket: connect');
});
socket.on('close', () => {
console.log('socket: close');
});
setTimeout(() => {
// socket._handle = undefined; // Uncomment to confirm _handle makes a difference.
socket.connect(12345, '::1');
}, 100);
return socket;
}
});
session.on('error', err => {
console.error('http2 session:', err);
});
session.on('connect', () => {
console.log('http2 session: connect');
});
session.on('close', () => {
console.log('http2 session: close');
});The output:
http2 session: connect
socket: Error: connect ECONNREFUSED ::1:12345
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1595:16) {
errno: -61,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '::1',
port: 12345
}
socket: close
http2 session: Error: connect ECONNREFUSED ::1:12345
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1595:16) {
errno: -61,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '::1',
port: 12345
}
http2 session: close
The 'connect' event is unexpected.
Setting the _handle as the commented 2 lines of code suggest can trick the constructor of Http2Session into not wrapping the socket and wait for the socket to emit a 'connect' event before emitting 'connect' itself. This is provided only illustration, setting _handle this way is not expected to be a valid workaround.
Code pointers:
- Http2Sesssion wraps the socket in a JSStreamSocket because the
_handleis undefined:node/lib/internal/http2/core.js
Line 1208 in a94966f
if (!socket._handle || !socket._handle.isStreamBase) { - The wrapper does not have
connecting === trueand the check of Http2Session whether the socket (now the wrapper) is still connecting does not trigger:node/lib/internal/http2/core.js
Line 1256 in a94966f
if (socket.connecting || socket.secureConnecting) {
How often does it reproduce? Is there a required condition?
Always reproduces with the above sample code.
What is the expected behavior? Why is that the expected behavior?
Http2Session should only emit a 'connect' event once the underlying socket is connected.
What do you see instead?
Http2Session emits 'connect' event while socket is still connecting.
Additional information
No response
Activity
darker16 commentedon Sep 26, 2023
Tushar-Gupta11 commentedon Oct 17, 2023
Hey!! I am quite new to the code base, what i didnt understand was why in the first place is node not assuring that the socket is connected, would it be plausible if the condition is changed from socket.connecting to checking if the socket is connected or not already. Asking because since the issue is with jsstreamsocket connection only, would it be right to change the condition all over or should i search for a way to return from jsstreamsocket itself that the socket has connected?
github-actions commentedon May 28, 2026
This issue has been marked as stale due to 210 days of inactivity.
It will be automatically closed in 30 days if no further activity occurs. If this is still relevant, please leave a comment or update it to keep it open.
github-actions commentedon Jun 28, 2026
This issue has been automatically closed after 30 days of inactivity following its stale status (no activity for a total of 240 days).
If this is still relevant, feel free to reopen it or leave a comment with additional details so we can continue the discussion.