Skip to content

Commit

Permalink
Fixed #172: race condition in the stream implementation.
Browse files Browse the repository at this point in the history
  • Loading branch information
uhop committed Nov 12, 2024
1 parent 2f8d140 commit 8895e64
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 7 deletions.
11 changes: 4 additions & 7 deletions Disassembler.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,13 +190,10 @@ class Disassembler extends Duplex {
const iterator = dump(chunk, this[kOptions]),
pump = (this[kPump] = new Pump(iterator, this));

pump
.start()
.then(
() => callback(),
error => callback(error)
)
.finally(() => (this[kPump] = null));
pump.start().then(
() => ((this[kPump] = null), callback()),
error => ((this[kPump] = null), callback(error))
);
}

_final(callback) {
Expand Down
39 changes: 39 additions & 0 deletions tests/test_disassembler.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

const {Readable} = require('stream');

const unit = require('heya-unit');

const {chain} = require('stream-chain');
Expand Down Expand Up @@ -304,5 +306,42 @@ unit.add(module, [
pipeline.write(item);
}
pipeline.end();
},
function test_disassembler_issue_172_1(t) {
const async = t.startAsync('test_disassembler_issue_172_1');

const input = [
{
a: {}
},
{
a: {
a: {}
}
}
],
result = [];

const pipeline = Readable.from(input).pipe(disassembler()).pipe(streamValues());

pipeline.on('data', item => result.push(item.value));
pipeline.on('end', () => {
eval(t.TEST('t.unify(result, input)'));
async.done();
});
},
function test_disassembler_issue_172_2(t) {
const async = t.startAsync('test_disassembler_issue_172_2');

const input = Array.from({length: 1000}, () => ({a: 'a'.repeat(1000)})),
result = [];

const pipeline = Readable.from(input).pipe(disassembler()).pipe(streamValues());

pipeline.on('data', item => result.push(item.value));
pipeline.on('end', () => {
eval(t.TEST('t.unify(result, input)'));
async.done();
});
}
]);

0 comments on commit 8895e64

Please sign in to comment.