Commit 1106479d7e8 for nodejs
commit 1106479d7e85701e1dcad0f3dc43716584685702
Author: Adithya kumar Reddy Gopavaram <91198686+ADITHYAKUMARREDDY@users.noreply.github.com>
Date: Wed Nov 25 22:49:17 2026 +0530
zlib: handle close before initialization
Fixes #67485
Signed-off-by: Adithya kumar Reddy Gopavaram <91198686+ADITHYAKUMARREDDY@users.noreply.github.com>
PR-URL: https://github.com/nodejs/node/pull/67507
Fixes: https://github.com/nodejs/node/issues/67485
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
diff --git a/src/node_zlib.cc b/src/node_zlib.cc
index 72947b8b783..89f8e8ad2cc 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 00000000000..7ce1b07192a
--- /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();
+ });
+ }
+}