From 64fb52b288fac8557af1188d622a8d68539df41d Mon Sep 17 00:00:00 2001 From: Angel <23444930+spankyed@users.noreply.github.com> Date: Wed, 11 Nov 2026 12:45:44 -0500 Subject: [PATCH] src: stop waking task_queues_async_ before closing it Environment::RunCleanup() closed task_queues_async_ and only afterwards cleared task_queues_async_initialized_, so another thread could still call uv_async_send() on the closing handle through SetImmediateThreadsafe() or RequestInterrupt(). On Windows that aborts with "Assertion failed: !(handle->flags & UV_HANDLE_CLOSING)". This happens when V8 frees the backing store of an external Buffer on a sweeper thread while the process exits. Clear the flag, under the mutex the senders hold, before the handle is closed. Signed-off-by: Angel <23444930+spankyed@users.noreply.github.com> --- src/env.cc | 10 +++++++ test/addons/buffer-free-callback/test-exit.js | 26 +++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 test/addons/buffer-free-callback/test-exit.js diff --git a/src/env.cc b/src/env.cc index 5f8e22073fa7..b66dc31d44f9 100644 --- a/src/env.cc +++ b/src/env.cc @@ -1481,6 +1481,16 @@ void Environment::ClosePerEnvHandles() { close_and_finish(reinterpret_cast(immediate_idle_handle())); close_and_finish(reinterpret_cast(&idle_prepare_handle_)); close_and_finish(reinterpret_cast(&idle_check_handle_)); + // Other threads may still call SetImmediateThreadsafe() or + // RequestInterrupt(), e.g. when V8 frees the backing store of an external + // Buffer on a sweeper thread. uv_async_send() must not be called on a closing + // handle (it aborts on Windows), so stop them from waking task_queues_async_ + // before it is closed. Anything they queue from here on is still run by + // RunCleanup(). + { + Mutex::ScopedLock lock(native_immediates_threadsafe_mutex_); + task_queues_async_initialized_ = false; + } close_and_finish(reinterpret_cast(&task_queues_async_)); } diff --git a/test/addons/buffer-free-callback/test-exit.js b/test/addons/buffer-free-callback/test-exit.js new file mode 100644 index 000000000000..1eb818291f19 --- /dev/null +++ b/test/addons/buffer-free-callback/test-exit.js @@ -0,0 +1,26 @@ +'use strict'; + +// The backing store of an external Buffer may be freed by V8 on another thread +// while the environment is being cleaned up. That must not wake an async +// handle that is already closing, which aborts the process on Windows. + +const common = require('../../common'); +const assert = require('assert'); +const { spawnSync } = require('child_process'); + +if (process.argv[2] === 'child') { + const binding = require(`./build/${common.buildType}/binding`); + for (let i = 0; i < 20000; i++) binding.alloc(64, 1, 0); + // Start freeing them and exit right away. + globalThis.gc({ type: 'minor' }); + return; +} + +// The race is not hit on every run. +for (let i = 0; i < 20; i++) { + const child = spawnSync( + process.execPath, ['--expose-gc', __filename, 'child'], { encoding: 'utf8' }); + assert.strictEqual(child.stderr, ''); + assert.strictEqual(child.signal, null); + assert.strictEqual(child.status, 0); +}