Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/runtime/node/zlib/NativeBrotli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,12 @@ mod _impl {
}

pub fn close(&mut self) {
self.deinit_state();
// Idempotent: a second close() (or close() before init()) sees
// `state == None` and must not reach deinit_state(), whose match
// has no arm for `mode == NONE`.
if self.state.is_some() {
self.deinit_state();
}
self.mode = bun_zlib::NodeMode::NONE;
}

Expand Down
41 changes: 23 additions & 18 deletions src/runtime/node/zlib/NativeZstd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -514,24 +514,29 @@ mod _impl {
}

pub fn close(&mut self) {
let _ = match self.mode {
// SAFETY: state is a valid CCtx/DCtx for this mode.
NodeMode::ZSTD_COMPRESS => unsafe {
c::ZSTD_CCtx_reset(
self.state_ptr().cast(),
c::ZSTD_reset_session_and_parameters,
)
},
// SAFETY: state is a valid DCtx set by init() for this mode.
NodeMode::ZSTD_DECOMPRESS => unsafe {
c::ZSTD_DCtx_reset(
self.state_ptr().cast(),
c::ZSTD_reset_session_and_parameters,
)
},
_ => unreachable!(),
};
self.deinit_state();
// Idempotent: a second close() (or close() before init()) sees
// `state == None` and must not reach the reset/free calls below,
// whose match has no arm for `mode == NONE`.
if self.state.is_some() {
let _ = match self.mode {
// SAFETY: state is a valid CCtx/DCtx for this mode.
NodeMode::ZSTD_COMPRESS => unsafe {
c::ZSTD_CCtx_reset(
self.state_ptr().cast(),
c::ZSTD_reset_session_and_parameters,
)
},
// SAFETY: state is a valid DCtx set by init() for this mode.
NodeMode::ZSTD_DECOMPRESS => unsafe {
c::ZSTD_DCtx_reset(
self.state_ptr().cast(),
c::ZSTD_reset_session_and_parameters,
)
},
_ => unreachable!(),
};
self.deinit_state();
}
self.mode = NodeMode::NONE;
}

Expand Down
40 changes: 40 additions & 0 deletions test/js/node/zlib/zlib-handle-double-close.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { describe, expect, test } from "bun:test";
import { bunEnv, bunExe } from "harness";

// Calling `_handle.close()` twice on a brotli/zstd stream must be a no-op,
// matching the deflate/gzip handle behavior. The first close() sets
// `mode = NONE` and frees the native encoder/decoder state; a second close()
// previously fell through to `unreachable!()` in Context::deinit_state() and
// aborted the process.

describe.concurrent("zlib native handle double close()", () => {
test.each([
["createBrotliCompress"],
["createBrotliDecompress"],
["createZstdCompress"],
["createZstdDecompress"],
["createGzip"],
["createGunzip"],
])("%s", async factory => {
await using proc = Bun.spawn({
cmd: [
bunExe(),
"-e",
`
const zlib = require("zlib");
const stream = zlib.${factory}();
const h = stream._handle;
h.close();
h.close();
h.close();
console.log("OK");
`,
],
env: bunEnv,
stdout: "pipe",
stderr: "pipe",
});
const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]);
expect({ stdout: stdout.trim(), stderr, exitCode }).toEqual({ stdout: "OK", stderr: "", exitCode: 0 });
});
});
Loading