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>
This commit is contained in:
Adithya kumar Reddy Gopavaram authored and Antoine du Hamel committed 2026-11-25 18:20:41 +01:00
1 parent 7e4d0dbc94
commit 2fb9bf8f83
2 files changed
+78 -1

No files matched your search

+1 -1
View File
@@ -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();
@@ -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();
});
}
}