Skip to content
Merged
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
Next Next commit
src: release context frame in AsyncWrap::EmitDestroy
Release the async context frame in AsyncWrap::EmitDestroy to allow
gc to collect it.

This is in special relevant for reused resources like HTTPParser
otherwise they might keep ALS stores alive.
  • Loading branch information
Flarna committed Feb 26, 2026
commit bdd2d389b90e9196355091c39a65d83fa620782c
9 changes: 6 additions & 3 deletions src/async_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -333,9 +333,12 @@ void AsyncWrap::EmitDestroy(bool from_gc) {
// Ensure no double destroy is emitted via AsyncReset().
async_id_ = kInvalidAsyncId;

if (!persistent().IsEmpty() && !from_gc) {
HandleScope handle_scope(env()->isolate());
USE(object()->Set(env()->context(), env()->resource_symbol(), object()));
if (!from_gc) {
if (!persistent().IsEmpty()) {
HandleScope handle_scope(env()->isolate());
USE(object()->Set(env()->context(), env()->resource_symbol(), object()));
}
context_frame_.Reset();
Comment thread
Flarna marked this conversation as resolved.
}
}

Expand Down
29 changes: 29 additions & 0 deletions test/parallel/test-async-local-storage-http-parser-leak.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Flags: --expose-gc
'use strict';

const common = require('../common');
const { onGC } = require('../common/gc');
const assert = require('node:assert');
const { AsyncLocalStorage } = require('node:async_hooks');
const { freeParser, parsers, HTTPParser } = require('_http_common');

let storeGCed = false;

const als = new AsyncLocalStorage();

function test() {
const store = {};
onGC(store, { ongc: common.mustCall(() => { storeGCed = true; }) });
let parser;
als.run(store, common.mustCall(() => {
parser = parsers.alloc();
parser.initialize(HTTPParser.RESPONSE, {});
}));
freeParser(parser);
}

test();
global.gc();
setImmediate(common.mustCall(() => {
assert.ok(storeGCed);
}));
Loading