Skip to content

Commit 75a9192

Browse files
mafintoshtargos
authored andcommitted
src: fix external memory usage going negative
PR-URL: #22594 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Tiancheng "Timothy" Gu <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Tobias Nießen <[email protected]>
1 parent 3cec988 commit 75a9192

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

src/node_crypto.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,9 @@ class SecureContext : public BaseObject {
198198
}
199199

200200
inline void Reset() {
201-
env()->isolate()->AdjustAmountOfExternalAllocatedMemory(-kExternalSize);
201+
if (ctx_ != nullptr) {
202+
env()->isolate()->AdjustAmountOfExternalAllocatedMemory(-kExternalSize);
203+
}
202204
ctx_.reset();
203205
cert_.reset();
204206
issuer_.reset();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
'use strict';
2+
// Flags: --expose-gc
3+
4+
// Tests that memoryUsage().external doesn't go negative
5+
// when a lot tls connections are opened and closed
6+
7+
const common = require('../common');
8+
if (!common.hasCrypto)
9+
common.skip('missing crypto');
10+
11+
const assert = require('assert');
12+
const net = require('net');
13+
const tls = require('tls');
14+
15+
// Payload doesn't matter. We just need to have the tls
16+
// connection try and connect somewhere.
17+
const yolo = Buffer.alloc(10000).fill('yolo');
18+
const server = net.createServer(function(socket) {
19+
socket.write(yolo);
20+
});
21+
22+
server.listen(0, common.mustCall(function() {
23+
const { port } = server.address();
24+
let runs = 0;
25+
connect();
26+
27+
function connect() {
28+
global.gc();
29+
assert(process.memoryUsage().external >= 0);
30+
if (runs++ < 512)
31+
tls.connect(port).on('error', connect);
32+
else
33+
server.close();
34+
}
35+
}));

0 commit comments

Comments
 (0)