Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src: fix external memory usage going negative #22594

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/node_crypto.h
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,9 @@ class SecureContext : public BaseObject {
}

inline void Reset() {
env()->isolate()->AdjustAmountOfExternalAllocatedMemory(-kExternalSize);
if (ctx_.get() != nullptr) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: just if (ctx_) should be enough.

env()->isolate()->AdjustAmountOfExternalAllocatedMemory(-kExternalSize);
}
ctx_.reset();
cert_.reset();
issuer_.reset();
Expand Down
35 changes: 35 additions & 0 deletions test/parallel/test-gc-tls-external-memory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'use strict';
// Flags: --expose-gc

// Tests that memoryUsage().external doesn't go negative
// when a lot tls connections are opened and closed

const common = require('../common');
if (!common.hasCrypto)
common.skip('missing crypto');

const assert = require('assert');
const net = require('net');
const tls = require('tls');

// Payload doesn't matter. We just need to have the tls
// connection try and connect somewhere.
const yolo = Buffer.alloc(10000).fill('yolo');
const server = net.createServer(function(socket) {
socket.write(yolo);
});

server.listen(0, function() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add common.mustCall() to the callback.

const { port } = server.address();
let runs = 0;
connect();

function connect() {
global.gc();
assert(process.memoryUsage().external >= 0);
if (runs++ < 512)
tls.connect(port).on('error', connect);
else
server.close();
}
});