Skip to content

Commit 27d48ed

Browse files
committed
src: unregister Isolate with platform before disposing
I previously thought the order of these calls was no longer relevant. I was wrong. This commit undoes the changes from 312c02d, adds a comment explaining why I was wrong, and flips the order of the calls elsewhere for consistency, the latter having been the goal of 312c02d. Fixes: nodejs#30846 Refs: nodejs#30181
1 parent 086c7b4 commit 27d48ed

5 files changed

Lines changed: 10 additions & 4 deletions

File tree

src/node.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,7 @@ class NODE_EXTERN MultiIsolatePlatform : public v8::Platform {
298298

299299
// This function may only be called once per `Isolate`, and discard any
300300
// pending delayed tasks scheduled for that isolate.
301+
// This needs to be called right before calling `Isolate::Dispose()`.
301302
virtual void UnregisterIsolate(v8::Isolate* isolate) = 0;
302303

303304
// The platform should call the passed function once all state associated

src/node_main_instance.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@ NodeMainInstance::~NodeMainInstance() {
9999
if (!owns_isolate_) {
100100
return;
101101
}
102-
isolate_->Dispose();
103102
platform_->UnregisterIsolate(isolate_);
103+
isolate_->Dispose();
104104
}
105105

106106
int NodeMainInstance::Run() {

src/node_worker.cc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,13 @@ class WorkerThreadData {
189189
*static_cast<bool*>(data) = true;
190190
}, &platform_finished);
191191

192-
isolate->Dispose();
192+
// The order of these calls is important; if the Isolate is first disposed
193+
// and then unregistered, there is a race condition window in which no
194+
// new Isolate at the same address can successfully be registered with
195+
// the platform.
196+
// (Refs: https://github.com/nodejs/node/issues/30846)
193197
w_->platform_->UnregisterIsolate(isolate);
198+
isolate->Dispose();
194199

195200
// Wait until the platform has cleaned up all relevant resources.
196201
while (!platform_finished)

test/cctest/node_test_fixture.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,8 @@ class NodeTestFixture : public NodeZeroIsolateTestFixture {
116116
void TearDown() override {
117117
platform->DrainTasks(isolate_);
118118
isolate_->Exit();
119-
isolate_->Dispose();
120119
platform->UnregisterIsolate(isolate_);
120+
isolate_->Dispose();
121121
isolate_ = nullptr;
122122
NodeZeroIsolateTestFixture::TearDown();
123123
}

test/cctest/test_platform.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,6 @@ TEST_F(NodeZeroIsolateTestFixture, IsolatePlatformDelegateTest) {
101101

102102
// Graceful shutdown
103103
delegate->Shutdown();
104-
isolate->Dispose();
105104
platform->UnregisterIsolate(isolate);
105+
isolate->Dispose();
106106
}

0 commit comments

Comments
 (0)