From e704336001706d22147c028dbacd6444e35ea747 Mon Sep 17 00:00:00 2001 From: Ben Clayton Date: Wed, 11 Jan 2023 21:03:45 +0000 Subject: [PATCH] dawn/node: Avoid exception on teardown If node is shutdown without first explicitly destroying the GPUDevice, the device's destructor will be called, which would automatically destroy the dawn device. Before doing this, it would first reject the lost-device promise. With newer versions of node, attempting to construct new GC'd objects on NodeJS tear-down triggers a fatal error. Work around this by adding a new state to promises, that allows promises to be destroyed without first being rejected or resolved, and without raising a 'Promise not resolved or rejected' fatal message. Change-Id: I810d6894d384511cdb7989b9c6c5b707dd68d8c2 Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/116864 Reviewed-by: Corentin Wallez Auto-Submit: Ben Clayton Kokoro: Ben Clayton Commit-Queue: Ben Clayton --- src/dawn/node/binding/GPUDevice.cpp | 4 +++- src/dawn/node/interop/Core.h | 7 +++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/dawn/node/binding/GPUDevice.cpp b/src/dawn/node/binding/GPUDevice.cpp index 92ec3716ce..ae11f9c407 100644 --- a/src/dawn/node/binding/GPUDevice.cpp +++ b/src/dawn/node/binding/GPUDevice.cpp @@ -174,7 +174,9 @@ GPUDevice::~GPUDevice() { // lost_promise_ is left hanging. We'll also not clean up any GPU objects before terminating the // process, which is not a good idea. if (!destroyed_) { - destroy(env_); + lost_promise_.Discard(); + device_.Destroy(); + destroyed_ = true; } } diff --git a/src/dawn/node/interop/Core.h b/src/dawn/node/interop/Core.h index be1b4ac495..a193f34458 100644 --- a/src/dawn/node/interop/Core.h +++ b/src/dawn/node/interop/Core.h @@ -185,6 +185,7 @@ enum class PromiseState { Pending, Resolved, Rejected, + Discarded, }; namespace detail { @@ -206,6 +207,12 @@ class PromiseBase { PromiseState GetState() const { return state_->state; } + // Place the promise into the Discarded state. + // This state will not throw a fatal error if this promise has not been rejected or resolved by + // the time it is finalized. This is useful for Node tear-down where the attempting to create + // new objects will result in a fatal error. + void Discard() { state_->state = PromiseState::Discarded; } + protected: void Resolve(Napi::Value value) const { state_->deferred.Resolve(value);