Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions src/node_platform.cc
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ class WorkerThreadsTaskRunner::DelayedTaskScheduler {
double delay_in_seconds) {
auto locked = tasks_.Lock();

if (flush_tasks_ == nullptr) return;
Copy link
Copy Markdown
Member

@addaleax addaleax Feb 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If "just not sending" is indeed a valid solution to this issue (which I have not verified), you could set a boolean flag next to flush_tasks_ instead of making it a heap-allocated variable

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

v8 is calling PostDelayedTaskOnWorkerThread here, which looks like a task for wasm caching.
https://github.com/v8/v8/blob/1fb9a7b9671a724ebdcf57db3807d4af56dc6cbb/src/wasm/module-compiler.cc#L4004-L4011
Ignoring it should be safe, because anyway, the delay here is 2 seconds, and the node will have finished long before then.


auto entry = std::make_unique<TaskQueueEntry>(std::move(task), priority);
auto delayed = std::make_unique<ScheduleTask>(
this, std::move(entry), delay_in_seconds);
Expand All @@ -127,13 +129,13 @@ class WorkerThreadsTaskRunner::DelayedTaskScheduler {
// schedules the timers into the local task queue that will be flushed
// by the local event loop.
locked.Push(std::move(delayed));
uv_async_send(&flush_tasks_);
uv_async_send(flush_tasks_);
}

void Stop() {
auto locked = tasks_.Lock();
locked.Push(std::make_unique<StopTask>(this));
uv_async_send(&flush_tasks_);
uv_async_send(flush_tasks_);
}

private:
Expand All @@ -142,8 +144,9 @@ class WorkerThreadsTaskRunner::DelayedTaskScheduler {
"WorkerThreadsTaskRunner::DelayedTaskScheduler");
loop_.data = this;
CHECK_EQ(0, uv_loop_init(&loop_));
flush_tasks_.data = this;
CHECK_EQ(0, uv_async_init(&loop_, &flush_tasks_, FlushTasks));
flush_tasks_ = new uv_async_t();
flush_tasks_->data = this;
CHECK_EQ(0, uv_async_init(&loop_, flush_tasks_, FlushTasks));
uv_sem_post(&ready_);

uv_run(&loop_, UV_RUN_DEFAULT);
Expand Down Expand Up @@ -178,8 +181,9 @@ class WorkerThreadsTaskRunner::DelayedTaskScheduler {
timers.push_back(timer);
for (uv_timer_t* timer : timers)
scheduler_->TakeTimerTask(timer);
uv_close(reinterpret_cast<uv_handle_t*>(&scheduler_->flush_tasks_),
uv_close(reinterpret_cast<uv_handle_t*>(scheduler_->flush_tasks_),
[](uv_handle_t* handle) {});
scheduler_->flush_tasks_ = nullptr;
}

private:
Expand Down Expand Up @@ -239,7 +243,7 @@ class WorkerThreadsTaskRunner::DelayedTaskScheduler {
// It is flushed whenever the next closest timer expires.
TaskQueue<Task> tasks_;
uv_loop_t loop_;
uv_async_t flush_tasks_;
uv_async_t* flush_tasks_ = nullptr;
std::unordered_set<uv_timer_t*> timers_;
};

Expand Down
32 changes: 32 additions & 0 deletions test/parallel/test-process-exit-after-fetch-throw.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'use strict';
// Ref: https://github.com/nodejs/node/issues/56645
const common = require('../common');
const assert = require('assert');
const cp = require('child_process');
const http = require('http');

if (process.argv[2] === 'child') {
http
.createServer((_, res) => {
res.writeHead(302, { Location: '/' });
res.end();
})
.listen(0, '127.0.0.1', async function() {
try {
await fetch(`http://127.0.0.1:${this.address().port}/`);
} catch {
// ignore
}
process.exit(0);
});
} else {
const child = cp.spawn(process.execPath, [__filename, 'child']);

child.on(
'close',
common.mustCall((exitCode, signal) => {
assert.strictEqual(exitCode, 0);
assert.strictEqual(signal, null);
}),
);
}
Loading