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

async_hooks: check for empty contexts before removing #39095

Closed
wants to merge 3 commits into from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/env-inl.h
Expand Up @@ -256,8 +256,13 @@ inline void AsyncHooks::RemoveContext(v8::Local<v8::Context> ctx) {
v8::Isolate* isolate = env()->isolate();
v8::HandleScope handle_scope(isolate);
for (auto it = contexts_.begin(); it != contexts_.end(); it++) {
if (it->IsEmpty()) {
contexts_.erase(it);
it--;
bengl marked this conversation as resolved.
Show resolved Hide resolved
continue;
}
v8::Local<v8::Context> saved_context =
PersistentToLocal::Weak(env()->isolate(), *it);
PersistentToLocal::Weak(isolate, *it);
if (saved_context == ctx) {
it->Reset();
contexts_.erase(it);
Expand Down
15 changes: 15 additions & 0 deletions test/parallel/test-async-hooks-vm-gc.js
@@ -0,0 +1,15 @@
// Flags: --expose-gc
'use strict';

require('../common');
const asyncHooks = require('async_hooks');
const vm = require('vm');

// This is a regression test for https://github.com/nodejs/node/issues/39019
//
// It should not segfault.

const hook = asyncHooks.createHook({ init() {} }).enable();
vm.createContext();
globalThis.gc();
hook.disable();