From ed78fc603ad13d8a8f7b238e0556495b6646d5db Mon Sep 17 00:00:00 2001 From: Santiago Gimeno Date: Thu, 16 Nov 2023 14:53:46 +0100 Subject: [PATCH] src: cleanup the RunCommand queues on RemoveEnv Just to be sure no dangling SharedEnvInst references are left after the Environment is gone and the EnvInst instance can be deleted. PR-URL: https://github.com/nodesource/nsolid/pull/25 Reviewed-by: Trevor Norris --- src/nsolid/nsolid_api.cc | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/nsolid/nsolid_api.cc b/src/nsolid/nsolid_api.cc index 88cfef131e..e7b1dc4ca7 100644 --- a/src/nsolid/nsolid_api.cc +++ b/src/nsolid/nsolid_api.cc @@ -989,6 +989,22 @@ void EnvList::RemoveEnv(Environment* env) { stor.cb(envinst_sp, stor.data.get()); }); + // Cleanup the RunCommand queues just to be sure no dangling SharedEnvInst + // references are left after the Environment is gone and the EnvInst instance + // can be deleted. + EnvInst::CmdQueueStor stor; + while (envinst_sp->eloop_cmds_q_.dequeue(stor)) { + stor.cb(stor.envinst_sp, stor.data); + } + + while (envinst_sp->interrupt_cb_q_.dequeue(stor)) { + stor.cb(stor.envinst_sp, stor.data); + } + + while (envinst_sp->interrupt_only_cb_q_.dequeue(stor)) { + stor.cb(stor.envinst_sp, stor.data); + } + // Don't allow execution to continue in case a RunCommand() is running in // another thread since they might need access to Environment specific // resources (like the Isolate) that won't be valid after this returns.