Skip to content

Commit ab2b066

Browse files
szmarczakBethGriggs
authored andcommittedDec 15, 2020
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 029ea16 commit ab2b066

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
@@ -3105,16 +3105,21 @@ function connect(authority, options, listener) {
31053105
if (typeof listener === 'function')
31063106
session.once('connect', listener);
31073107

3108-
debug('Http2Session connect', options.createConnection);
3109-
// Socket already has some buffered data - emulate receiving it
3110-
// https://github.com/nodejs/node/issues/35475
3111-
if (socket && socket.readableLength) {
3112-
let buf;
3113-
while ((buf = socket.read()) !== null) {
3114-
debug(`Http2Session connect: injecting ${buf.length} already in buffer`);
3115-
session[kHandle].receive(buf);
3108+
// Process data on the next tick - a remoteSettings handler may be attached.
3109+
// https://github.com/nodejs/node/issues/35981
3110+
process.nextTick(() => {
3111+
debug('Http2Session connect', options.createConnection);
3112+
// Socket already has some buffered data - emulate receiving it
3113+
// https://github.com/nodejs/node/issues/35475
3114+
if (socket && socket.readableLength) {
3115+
let buf;
3116+
while ((buf = socket.read()) !== null) {
3117+
debug(`Http2Session connect: ${buf.length} bytes already in buffer`);
3118+
session[kHandle].receive(buf);
3119+
}
31163120
}
3117-
}
3121+
});
3122+
31183123
return session;
31193124
}
31203125

‎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)
Please sign in to comment.