Skip to content

Fix _writev to only call its callback when all chunks are done #50

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
24 changes: 19 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,27 @@ TransportStream.prototype._writev = function _writev(chunks, callback) {
return this.logv(infos, callback);
}

let numLogCalls = 0;
let numCallbacksCalled = 0;
const wrapCallback = i => (...args) => {
setImmediate(() => {
chunks[i].callback(...args);

numCallbacksCalled += 1;

if (numCallbacksCalled === numLogCalls) {
return callback(null);
}
});
};

for (let i = 0; i < chunks.length; i++) {
if (!this._accept(chunks[i])) continue;

numLogCalls += 1;

if (chunks[i].chunk && !this.format) {
this.log(chunks[i].chunk, chunks[i].callback);
this.log(chunks[i].chunk, wrapCallback(i));
continue;
}

Expand All @@ -151,18 +167,16 @@ TransportStream.prototype._writev = function _writev(chunks, callback) {

if (errState || !transformed) {
// eslint-disable-next-line callback-return
chunks[i].callback();
wrapCallback(i);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be wrapCallback(i)(), because this code change replaces the chunks[i].callback() function call, not the function reference chunks[i].callback (like the other two instances in this PR).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! This probably didn't come up in the tests because there were no errors. Thanks, I'll fix that up.

if (errState) {
// eslint-disable-next-line callback-return
callback(null);
throw errState;
}
} else {
this.log(transformed, chunks[i].callback);
this.log(transformed, wrapCallback(i));
}
}

return callback(null);
};

/**
Expand Down
56 changes: 56 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,62 @@ describe('TransportStream', () => {
expected.forEach(transport.write.bind(transport));
transport.uncork();
});

it("invokes the callback only when each chunk has finished", done => {
const expected = infosFor({
count: 5,
levels: ["info"]
});

const individualCallbacks = [];
const transport = new TransportStream({
level: "info",
log(info, callback) {
individualCallbacks.push(callback);
}
});

//
// Make the standard _write throw to ensure that _writev is called.
//
transport._write = () => {
throw new Error(
"TransportStream.prototype._write should never be called."
);
};

// Wrap the standard _writev in a way to tell if the callback was called
let callbackCalled = false;
let individualCallbacksCalled = false;
const standardWriteV = transport._writev;
transport._writev = (chunks, callback) => {
standardWriteV.call(transport, chunks, () => {
assume(individualCallbacksCalled).equals(true);
callback();
callbackCalled = true;
done();
});
};

transport.cork();
transport.levels = testLevels;
expected.forEach(transport.write.bind(transport));
transport.uncork();

// Callback shouldn't be called yet, since the individual log callbacks
// haven't been called

setImmediate(() => {
assume(callbackCalled).equals(false);

// After each individual log is finished, only then should the stream
// should be finished
for (const callback of individualCallbacks) {
callback();
}
individualCallbacksCalled = true;
});
});
});

describe('parent (i.e. "logger") ["pipe", "unpipe"]', () => {
Expand Down