Skip to content

worker.terminate() during zlib stream construction aborts the process: Assertion failed: init_done_ && "close before init" #67485

Open

Description

@nicolasdma

Version

v22.14.0 (also present in the source of v22.23.5, v24.11, v26.13.1, and main).

Platform

Linux x86_64 (Ubuntu, GCP e2/c3d instances). Not tested on macOS or Windows.

Subsystem

zlib, worker_threads

What steps will reproduce the bug?

repro.mjs
// Minimal Node repro: terminate() a worker that is busy building zlib streams. One trial = one child process (an abort kills it).
// node repro.mjs <trials> <mode: terminate|graceful> <api: zlib|web>
import { spawnSync } from 'node:child_process';
import { Worker } from 'node:worker_threads';
const [, , trialsArg = '100', mode = 'terminate', api = 'web', role] = process.argv;
const body = api === 'zlib'
  ? `const zlib = require('node:zlib'); const { parentPort } = require('node:worker_threads'); let stop = false; parentPort.on('message', () => { stop = true; process.exit(0); });
     (function loop() { for (let i = 0; i < 200; i++) zlib.createInflateRaw().close(); if (!stop) setImmediate(loop); })();`
  : `const { parentPort } = require('node:worker_threads'); parentPort.on('message', () => process.exit(0));
     const b = new Uint8Array(require('node:zlib').deflateRawSync(Buffer.alloc(4096, 7)));
     (async function loop() { for (;;) { await Promise.all(Array.from({ length: 8 }, () => new Response(new ReadableStream({ start(c) { c.enqueue(b); c.close(); } }).pipeThrough(new DecompressionStream('deflate-raw'))).arrayBuffer())); } })();`;
if (role === 'child') {
  const w = new Worker(body, { eval: true });
  await new Promise((r) => setTimeout(r, 20 + Math.random() * 60));
  if (mode === 'terminate') await w.terminate();
  else { const exited = new Promise((r) => w.once('exit', r)); w.postMessage('close'); await exited; }
  process.exit(0);
} else {
  let aborts = 0, other = 0;
  for (let i = 0; i < Number(trialsArg); i++) {
    const r = spawnSync(process.execPath, [new URL(import.meta.url).pathname, '1', mode, api, 'child'], { encoding: 'utf8' });
    if (r.signal === 'SIGABRT') { aborts++; if (aborts === 1) console.error(r.stderr.split('\n').filter((l) => /Assert|FATAL|close before/.test(l)).slice(0, 3).join('\n')); }
    else if (r.status !== 0) { other++; console.error('other', r.status, r.signal, r.stderr.slice(0, 300)); }
  }
  console.log(JSON.stringify({ node: process.version, mode, api, trials: Number(trialsArg), aborts, other }));
}

How often does it reproduce? Is there a required condition?

The issue is intermittent.

On our machine:

  • terminate: 20 aborts in 600 trials.
  • graceful: 0 aborts in 400 trials.
    The race window appears to be between new binding.Zlib(mode) and handle.init(...) in lib/zlib.js.

What is the expected behavior? Why is that the expected behavior?

worker.terminate() should stop the worker without aborting the entire process. Terminating a worker at an arbitrary point is documented and supported behavior.

What do you see instead?

The parent process terminates with SIGABRT:

Assertion failed: init_done_ && "close before init"
CompressionStream::Close()
CleanupQueue::Drain()
Environment teardown / FreeEnvironment()
Worker::Run()

Additional information

Our understanding of the cause is that zlib stream handles are constructed in two steps in lib/zlib.js:

  1. new binding.Zlib(mode)
  2. handle.init(...)
    If the worker is terminated between these two steps, the environment's cleanup queue attempts to close a handle that has not yet been initialized. The CHECK(init_done_ && "close before init") assertion in CompressionStream::Close() (src/node_zlib.cc) then aborts the process instead of safely releasing the handle.

A possible fix would be to make Close() tolerate an uninitialized handle during environment teardown, releasing it without triggering the assertion, or to make construction and initialization atomic from the JavaScript side.

As a workaround, we currently close workers by sending them a message and awaiting their 'exit' event instead of calling terminate().

I'd be happy to submit a fix if the proposed approach makes sense.

Activity

  1. MikeMcC399 commented on Nov 15, 2026

    @MikeMcC399
    Member

    I added the closing </details> tag so the collapsed section didn't include the rest of the text, just the code. Please check if that is what you would have wanted!

  2. added
    zlibIssues and PRs related to the zlib module and its compression dependencies.
    on Nov 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    zlibIssues and PRs related to the zlib module and its compression dependencies.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions