Skip to content

Platform thread race: process.exit executed before background threads are ready #23065

Closed
@ofrobots

Description

@ofrobots

Forked issue from #22938 (comment).

This test case:

const assert = require('assert');
const child_process = require('child_process');
const { promisify } = require('util');
const execFile = promisify(child_process.execFile);

const code =
  'console.log(42);process.exit(1)';

{
  execFile(process.execPath, ['-e', code])
    .catch((err) => {
      console.log(`child stderr: >>>\n${err.stderr}<<<`);
      assert.strictEqual(err.code, 1);
      assert.strictEqual(err.stdout, '42\n');
    });
}

Intermittently crashes in the child process on windows when a small IO delay is introduced in the platform worker thread startup:

static void PlatformWorkerThread(void* data) {
  fprintf(stderr, ""); // write an empty string to stderr, just to introduce a delay.
  TRACE_EVENT_METADATA1("__metadata", "thread_name", "name",
                        "PlatformWorkerThread");
  TaskQueue<Task>* pending_worker_tasks = static_cast<TaskQueue<Task>*>(data);
  while (std::unique_ptr<Task> task = pending_worker_tasks->BlockingPop()) {
    task->Run();
    pending_worker_tasks->NotifyOfCompletion();
  }
}

Crash:

C:\workspace\ofrobots\test\common\index.js:662
const crashOnUnhandledRejection = (err) => { throw err; };
                                             ^

AssertionError [ERR_ASSERTION]: Expected inputs to be strictly equal:
�[32m+ actual�[39m �[31m- expected�[39m

�[32m+�[39m 3221225477
�[31m-�[39m 1
    at execFile.catch.common.mustCall (C:\workspace\ofrobots\test\parallel\test-child-process-promisified.js:47:14)
    at C:\workspace\ofrobots\test\common\index.js:349:15
    at process._tickCallback (internal/process/next_tick.js:68:7)

The platform worker thread is trying to do IO while the main thread is already shutting things down in exit.

/cc @nodejs/platform-windows

Metadata

Metadata

Assignees

No one assigned

    Labels

    blockedPRs that are blocked by other issues or PRs.v8 platformIssues and PRs related to Node's v8::Platform implementation.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions