Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ERR_IPC_CHANNEL_CLOSED: Channel closed #94

Closed
AriPerkkio opened this issue May 19, 2024 · 1 comment · Fixed by #95
Closed

ERR_IPC_CHANNEL_CLOSED: Channel closed #94

AriPerkkio opened this issue May 19, 2024 · 1 comment · Fixed by #95

Comments

@AriPerkkio
Copy link
Member

AriPerkkio commented May 19, 2024

Error below comes up quite often on Vitest's codebase. Tests themselves are passing fine, but this error is visible on test logs. My initial guess is that Tinypool is shutting down before workers have finished loading user's entrypoint. These workers are likely the idle workers that are kept ready for next pool runs.

These errors should only come up when multiple pools are nested, e.g. running Tinypool in Tinypool.

node:events:497
      throw er; // Unhandled 'error' event
      ^

Error [ERR_IPC_CHANNEL_CLOSED]: Channel closed
    at target.send (node:internal/child_process:754:16)
    at file:///<root>/vitest/node_modules/.pnpm/tinypool@0.9.0/node_modules/tinypool/dist/entry/process.js:29:7
Emitted 'error' event on process instance at:
    at node:internal/child_process:758:35
    at process.processTicksAndRejections (node:internal/process/task_queues:77:11) {
  code: 'ERR_IPC_CHANNEL_CLOSED'
}

As fix we could try adding a no-op callback here just to ignore these errors:

send!(<OutgoingMessage>{
ready: true,
source: 'pool',
__tinypool_worker_message__: true,
})

https://nodejs.org/api/process.html#processsendmessage-sendhandle-options-callback

@AriPerkkio
Copy link
Member Author

Minimal repro:

import { isMainThread } from "node:worker_threads";
import Tinypool from "tinypool";

const isMainProcess = process.send === undefined;

if (isMainProcess && isMainThread) {
  console.log("Starting main pool");

  const pool = new Tinypool({
    filename: new URL(import.meta.url).href,
    isolateWorkers: true,
  });

  await Promise.all([
    pool.run({}, { name: "wrapper" }),
    pool.run({}, { name: "wrapper" }),
    pool.run({}, { name: "wrapper" }),
  ]);
}

export async function wrapper() {
  console.log("Starting nested pool");

  const pool = new Tinypool({
    filename: new URL(import.meta.url, import.meta.url).href,
    runtime: "child_process",
    isolateWorkers: true,
    minThreads: 8,
    maxThreads: 8,
  });

  await pool.run({ a: 4, b: 6 }, { name: "sum" });
  pool.recycleWorkers();
}

export async function sum({ a, b }) {
  console.log("Running worker");
  return a + b;
}
 $ node repro.mjs 
Starting main pool
Starting nested pool
Starting nested pool
Starting nested pool
Running worker
Running worker
node:events:496
      throw er; // Unhandled 'error' event
      ^

Error [ERR_IPC_CHANNEL_CLOSED]: Channel closed
    at target.send (node:internal/child_process:754:16)
    at file:///x/repros/node_modules/.pnpm/tinypool@0.9.0/node_modules/tinypool/dist/entry/process.js:29:7
Emitted 'error' event on process instance at:
    at node:internal/child_process:758:35
    at process.processTicksAndRejections (node:internal/process/task_queues:77:11) {
  code: 'ERR_IPC_CHANNEL_CLOSED'
}

Node.js v20.12.2
node:events:496
      throw er; // Unhandled 'error' event
      ^

Error [ERR_IPC_CHANNEL_CLOSED]: Channel closed
    at target.send (node:internal/child_process:754:16)
    at file:///x/repros/node_modules/.pnpm/tinypool@0.9.0/node_modules/tinypool/dist/entry/process.js:29:7
Emitted 'error' event on process instance at:
    at node:internal/child_process:758:35
    at process.processTicksAndRejections (node:internal/process/task_queues:77:11) {
  code: 'ERR_IPC_CHANNEL_CLOSED'
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant