-
Notifications
You must be signed in to change notification settings - Fork 29.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
http2: fix memory leak caused by premature listener removing
Http2Session should always call ondone into JS to detach the handle. In some case, ondone is defered to be called by the StreamListener through WriteWrap, we should be careful of this before getting rid of the StreamListener. PR-URL: #55966 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Juan José Arboleda <[email protected]>
- Loading branch information
Showing
2 changed files
with
83 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
test/parallel/test-h2leak-destroy-session-on-socket-ended.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
'use strict'; | ||
// Flags: --expose-gc | ||
|
||
const common = require('../common'); | ||
if (!common.hasCrypto) | ||
common.skip('missing crypto'); | ||
const http2 = require('http2'); | ||
const tls = require('tls'); | ||
const fixtures = require('../common/fixtures'); | ||
const assert = require('assert'); | ||
|
||
const registry = new FinalizationRegistry(common.mustCall((name) => { | ||
assert(name, 'session'); | ||
})); | ||
|
||
const server = http2.createSecureServer({ | ||
key: fixtures.readKey('agent1-key.pem'), | ||
cert: fixtures.readKey('agent1-cert.pem'), | ||
}); | ||
|
||
let firstServerStream; | ||
|
||
|
||
server.on('secureConnection', (s) => { | ||
console.log('secureConnection'); | ||
s.on('end', () => { | ||
console.log(s.destroyed); // false !! | ||
s.destroy(); | ||
firstServerStream.session.destroy(); | ||
|
||
firstServerStream = null; | ||
|
||
setImmediate(() => { | ||
global.gc(); | ||
global.gc(); | ||
|
||
server.close(); | ||
}); | ||
}); | ||
}); | ||
|
||
server.on('session', (s) => { | ||
registry.register(s, 'session'); | ||
}); | ||
|
||
server.on('stream', (stream) => { | ||
console.log('stream...'); | ||
stream.write('a'.repeat(1024)); | ||
firstServerStream = stream; | ||
setImmediate(() => console.log('Draining setImmediate after writing')); | ||
}); | ||
|
||
|
||
server.listen(() => { | ||
client(); | ||
}); | ||
|
||
|
||
const h2fstStream = [ | ||
'UFJJICogSFRUUC8yLjANCg0KU00NCg0K', | ||
// http message (1st stream:) | ||
'AAAABAAAAAAA', | ||
'AAAPAQUAAAABhIJBiqDkHROdCbjwHgeG', | ||
]; | ||
function client() { | ||
const client = tls.connect({ | ||
port: server.address().port, | ||
host: 'localhost', | ||
rejectUnauthorized: false, | ||
ALPNProtocols: ['h2'] | ||
}, () => { | ||
client.end(Buffer.concat(h2fstStream.map((s) => Buffer.from(s, 'base64'))), (err) => { | ||
assert.ifError(err); | ||
}); | ||
}); | ||
|
||
client.on('error', (error) => { | ||
console.error('Connection error:', error); | ||
}); | ||
} |