Skip to content

Commit fd93622

Browse files
bnoordhuisMylesBorins
authored andcommitted
tls: fix SecurePair external memory reporting
Ensure that AdjustAmountOfExternalAllocatedMemory() is called when the SecurePair is destroyed. Not doing so is not an actual memory leak but it makes `process.memoryUsage().external` wildly inaccurate and can cause performance problems due to excessive garbage collection. PR-URL: #11896 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Anna Henningsen <[email protected]>
1 parent a64aa44 commit fd93622

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

src/node_crypto.cc

+1-5
Original file line numberDiff line numberDiff line change
@@ -3227,11 +3227,7 @@ void Connection::Start(const FunctionCallbackInfo<Value>& args) {
32273227
void Connection::Close(const FunctionCallbackInfo<Value>& args) {
32283228
Connection* conn;
32293229
ASSIGN_OR_RETURN_UNWRAP(&conn, args.Holder());
3230-
3231-
if (conn->ssl_ != nullptr) {
3232-
SSL_free(conn->ssl_);
3233-
conn->ssl_ = nullptr;
3234-
}
3230+
conn->DestroySSL();
32353231
}
32363232

32373233

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Flags: --expose-gc --no-deprecation
2+
'use strict';
3+
4+
const common = require('../common');
5+
const assert = require('assert');
6+
7+
if (!common.hasCrypto) {
8+
common.skip('missing crypto');
9+
return;
10+
}
11+
12+
const { createSecureContext } = require('tls');
13+
const { createSecurePair } = require('_tls_legacy');
14+
15+
const before = process.memoryUsage().external;
16+
{
17+
const context = createSecureContext();
18+
const options = {};
19+
for (let i = 0; i < 1e4; i += 1)
20+
createSecurePair(context, false, false, false, options).destroy();
21+
}
22+
global.gc();
23+
const after = process.memoryUsage().external;
24+
25+
// It's not an exact science but a SecurePair grows .external by about 45 kB.
26+
// Unless AdjustAmountOfExternalAllocatedMemory() is called on destruction,
27+
// 10,000 instances make it grow by well over 400 MB. Allow for some slop
28+
// because objects like buffers also affect the external limit.
29+
assert(after - before < 25 << 20);

0 commit comments

Comments
 (0)