Skip to content

Commit 0b40568

Browse files
szmarczakcodebytere
authored andcommitted
http2: delay session.receive() by a tick
PR-URL: #35985 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
1 parent 1924255 commit 0b40568

File tree

2 files changed

+30
-39
lines changed

2 files changed

+30
-39
lines changed

lib/internal/http2/core.js

+14-9
Original file line numberDiff line numberDiff line change
@@ -3139,16 +3139,21 @@ function connect(authority, options, listener) {
31393139
if (typeof listener === 'function')
31403140
session.once('connect', listener);
31413141

3142-
debug('Http2Session connect', options.createConnection);
3143-
// Socket already has some buffered data - emulate receiving it
3144-
// https://github.com/nodejs/node/issues/35475
3145-
if (socket && socket.readableLength) {
3146-
let buf;
3147-
while ((buf = socket.read()) !== null) {
3148-
debug(`Http2Session connect: injecting ${buf.length} already in buffer`);
3149-
session[kHandle].receive(buf);
3142+
// Process data on the next tick - a remoteSettings handler may be attached.
3143+
// https://github.com/nodejs/node/issues/35981
3144+
process.nextTick(() => {
3145+
debug('Http2Session connect', options.createConnection);
3146+
// Socket already has some buffered data - emulate receiving it
3147+
// https://github.com/nodejs/node/issues/35475
3148+
if (socket && socket.readableLength) {
3149+
let buf;
3150+
while ((buf = socket.read()) !== null) {
3151+
debug(`Http2Session connect: ${buf.length} bytes already in buffer`);
3152+
session[kHandle].receive(buf);
3153+
}
31503154
}
3151-
}
3155+
});
3156+
31523157
return session;
31533158
}
31543159

test/parallel/test-http2-connect-tls-with-delay.js

+16-30
Original file line numberDiff line numberDiff line change
@@ -4,61 +4,47 @@ const common = require('../common');
44
if (!common.hasCrypto)
55
common.skip('missing crypto');
66

7-
if (!common.hasMultiLocalhost())
8-
common.skip('platform-specific test.');
9-
107
const http2 = require('http2');
11-
const assert = require('assert');
128
const tls = require('tls');
139
const fixtures = require('../common/fixtures');
1410

1511
const serverOptions = {
1612
key: fixtures.readKey('agent1-key.pem'),
1713
cert: fixtures.readKey('agent1-cert.pem')
1814
};
19-
const server = http2.createSecureServer(serverOptions, (req, res) => {
20-
console.log(`Connect from: ${req.connection.remoteAddress}`);
21-
assert.strictEqual(req.connection.remoteAddress, '127.0.0.2');
2215

23-
req.on('end', common.mustCall(() => {
24-
res.writeHead(200, { 'Content-Type': 'text/plain' });
25-
res.end(`You are from: ${req.connection.remoteAddress}`);
26-
}));
27-
req.resume();
16+
const server = http2.createSecureServer(serverOptions, (req, res) => {
17+
res.end();
2818
});
2919

3020
server.listen(0, '127.0.0.1', common.mustCall(() => {
3121
const options = {
3222
ALPNProtocols: ['h2'],
3323
host: '127.0.0.1',
3424
servername: 'localhost',
35-
localAddress: '127.0.0.2',
3625
port: server.address().port,
3726
rejectUnauthorized: false
3827
};
3928

40-
console.log('Server ready', server.address().port);
41-
4229
const socket = tls.connect(options, async () => {
43-
44-
console.log('TLS Connected!');
45-
46-
setTimeout(() => {
47-
30+
socket.once('readable', () => {
4831
const client = http2.connect(
4932
'https://localhost:' + server.address().port,
5033
{ ...options, createConnection: () => socket }
5134
);
52-
const req = client.request({
53-
':path': '/'
54-
});
55-
req.on('data', () => req.resume());
56-
req.on('end', common.mustCall(function() {
57-
client.close();
58-
req.close();
59-
server.close();
35+
36+
client.once('remoteSettings', common.mustCall(() => {
37+
const req = client.request({
38+
':path': '/'
39+
});
40+
req.on('data', () => req.resume());
41+
req.on('end', common.mustCall(() => {
42+
client.close();
43+
req.close();
44+
server.close();
45+
}));
46+
req.end();
6047
}));
61-
req.end();
62-
}, 1000);
48+
});
6349
});
6450
}));

0 commit comments

Comments
 (0)