Skip to content

Commit c4fc1ff

Browse files
addaleaxMylesBorins
authored andcommitted
test: fix flaky async-hooks/test-zlib.zlib-binding.deflate
Previously, the typed arrays used in this test would not automatically be kept alive by the native handle when it’s using them, so the V8 garbage collector could collect them while they are still in use by the zlib module, leading to memory corruption. Fixes: #20907 PR-URL: #21077 Reviewed-By: Anatoli Papirovski <[email protected]> Reviewed-By: Jeremiah Senkpiel <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Yang Guo <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 44fe78b commit c4fc1ff

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

test/async-hooks/test-zlib.zlib-binding.deflate.js

+16-7
Original file line numberDiff line numberDiff line change
@@ -21,31 +21,37 @@ assert.strictEqual(typeof hdl.uid, 'number');
2121
assert.strictEqual(typeof hdl.triggerAsyncId, 'number');
2222
checkInvocations(hdl, { init: 1 }, 'when created handle');
2323

24+
// Store all buffers together so that they do not get
25+
// garbage collected.
26+
const buffers = {
27+
writeResult: new Uint32Array(2),
28+
dictionary: new Uint8Array(0),
29+
inBuf: new Uint8Array([0x78]),
30+
outBuf: new Uint8Array(1)
31+
};
32+
2433
handle.init(
2534
constants.Z_DEFAULT_WINDOWBITS,
2635
constants.Z_MIN_LEVEL,
2736
constants.Z_DEFAULT_MEMLEVEL,
2837
constants.Z_DEFAULT_STRATEGY,
29-
new Uint32Array(2),
38+
buffers.writeResult,
3039
function processCallback() { this.cb(); },
31-
Buffer.from('')
40+
buffers.dictionary
3241
);
3342
checkInvocations(hdl, { init: 1 }, 'when initialized handle');
3443

35-
const inBuf = Buffer.from('x');
36-
const outBuf = Buffer.allocUnsafe(1);
37-
3844
let count = 2;
3945
handle.cb = common.mustCall(onwritten, 2);
40-
handle.write(true, inBuf, 0, 1, outBuf, 0, 1);
46+
handle.write(true, buffers.inBuf, 0, 1, buffers.outBuf, 0, 1);
4147
checkInvocations(hdl, { init: 1 }, 'when invoked write() on handle');
4248

4349
function onwritten() {
4450
if (--count) {
4551
// first write
4652
checkInvocations(hdl, { init: 1, before: 1 },
4753
'when wrote to handle the first time');
48-
handle.write(true, inBuf, 0, 1, outBuf, 0, 1);
54+
handle.write(true, buffers.inBuf, 0, 1, buffers.outBuf, 0, 1);
4955
} else {
5056
// second write
5157
checkInvocations(hdl, { init: 1, before: 2, after: 1 },
@@ -61,4 +67,7 @@ function onexit() {
6167
// TODO: destroy never called here even with large amounts of ticks
6268
// is that correct?
6369
checkInvocations(hdl, { init: 1, before: 2, after: 2 }, 'when process exits');
70+
71+
// Do something with `buffers` to keep them alive until here.
72+
buffers.buffers = buffers;
6473
}

0 commit comments

Comments
 (0)