From 7dc6903175791a77766cf6a8b354817858555a34 Mon Sep 17 00:00:00 2001 From: Adithya kumar Reddy Gopavaram <91198686+ADITHYAKUMARREDDY@users.noreply.github.com> Date: Mon, 16 Nov 2026 09:22:44 +0530 Subject: [PATCH] zlib: handle close before initialization Fixes #67485 Signed-off-by: Adithya kumar Reddy Gopavaram <91198686+ADITHYAKUMARREDDY@users.noreply.github.com> --- src/node_zlib.cc | 2 +- ...-zlib-worker-terminate-stream-construct.js | 77 +++++++++++++++++++ 2 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 test/parallel/test-zlib-worker-terminate-stream-construct.js diff --git a/src/node_zlib.cc b/src/node_zlib.cc index c87703800fd0..17cb3dddb61f 100644 --- a/src/node_zlib.cc +++ b/src/node_zlib.cc @@ -523,7 +523,7 @@ class CompressionStream : public AsyncWrap, pending_close_ = false; closed_ = true; - CHECK(init_done_ && "close before init"); + if (!init_done_) return; AllocScope alloc_scope(this); ctx_.Close(); diff --git a/test/parallel/test-zlib-worker-terminate-stream-construct.js b/test/parallel/test-zlib-worker-terminate-stream-construct.js new file mode 100644 index 000000000000..7ce1b07192a1 --- /dev/null +++ b/test/parallel/test-zlib-worker-terminate-stream-construct.js @@ -0,0 +1,77 @@ +// Flags: --expose-gc +'use strict'; +const common = require('../common'); +const assert = require('assert'); +const { spawnSync } = require('child_process'); + +// Tests that terminating a worker thread while constructing a zlib stream +// (or garbage-collecting an uninitialized zlib stream handle) does not abort +// the process with assertion: `init_done_ && "close before init"`. +// Refs: https://github.com/nodejs/node/issues/67485 + +if (process.argv[2] !== 'child') { + // Parent process: runs the test in a child process and asserts it does not abort. + const result = spawnSync(process.execPath, [ + '--expose-gc', + __filename, + 'child', + ], { encoding: 'utf8' }); + + assert.strictEqual( + result.status, + 0, + `Child process exited with code ${result.status} (signal: ${result.signal}).\nStderr:\n${result.stderr}` + ); + assert.strictEqual(result.signal, null); + assert.doesNotMatch(result.stderr, /Assertion failed/); + return; +} + +// Child process execution: +const { Worker } = require('worker_threads'); +const zlib = require('zlib'); + +// Test 1: Worker thread terminated while constructing a zlib stream +{ + const workerCode = ` + const zlib = require('zlib'); + const { parentPort } = require('worker_threads'); + new zlib.Gzip({ + get rejectGarbageAfterEnd() { + parentPort.postMessage('in_constructor'); + const start = Date.now(); + while (Date.now() - start < 2000) {} + return false; + } + }); + `; + + const worker = new Worker(workerCode, { eval: true }); + worker.on('message', common.mustCall((msg) => { + assert.strictEqual(msg, 'in_constructor'); + worker.terminate(); + })); + + worker.on('exit', common.mustCall((exitCode) => { + assert.strictEqual(exitCode, 1); + })); +} + +// Test 2: Uninitialized zlib stream handle garbage-collected after error during construction +{ + assert.throws(() => { + new zlib.Gzip({ + get rejectGarbageAfterEnd() { + throw new Error('boom'); + } + }); + }, { message: 'boom' }); + + if (typeof global.gc === 'function') { + // Run multiple GC passes to ensure weak callbacks run + global.gc(); + setImmediate(() => { + global.gc(); + }); + } +}