Skip to content
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

stream: fix multiple Writable.destroy() calls. #38221

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
9 changes: 3 additions & 6 deletions lib/internal/streams/writable.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ const {
getHighWaterMark,
getDefaultHighWaterMark
} = require('internal/streams/state');
const assert = require('internal/assert');
const {
ERR_INVALID_ARG_TYPE,
ERR_METHOD_NOT_IMPLEMENTED,
Expand Down Expand Up @@ -854,11 +853,9 @@ Writable.prototype.destroy = function(err, cb) {
const state = this._writableState;

// Invoke pending callbacks.
if (
state.bufferedIndex < state.buffered.length ||
state[kOnFinished].length
) {
assert(!state.destroyed);
if (!state.destroyed &&
(state.bufferedIndex < state.buffered.length ||
state[kOnFinished].length)) {
process.nextTick(errorBuffer, state);
}

Expand Down
31 changes: 31 additions & 0 deletions test/parallel/test-http-many-requests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'use strict';

const common = require('../common');
const http = require('http');
const Countdown = require('../common/countdown');

const NUM_REQ = 128

const agent = new http.Agent({ keepAlive: true });
const countdown = new Countdown(NUM_REQ, () => server.close());

const server = http.createServer(common.mustCall(function(req, res) {
res.setHeader('content-type', 'application/json; charset=utf-8')
res.end(JSON.stringify({ hello: 'world' }))
}, NUM_REQ)).listen(0, function() {
for (let i = 0; i < NUM_REQ; ++i) {
const req = http.request({
port: server.address().port,
agent,
method: 'GET'
}, function(res) {
res.resume();
res.on('end', () => {
countdown.dec();
})
})
.end();
}
});

process.on('warning', common.mustNotCall());
11 changes: 11 additions & 0 deletions test/parallel/test-stream-writable-destroy.js
Original file line number Diff line number Diff line change
Expand Up @@ -460,3 +460,14 @@ const assert = require('assert');
assert.strictEqual(write.destroyed, true);
}));
}

{
// Destroy twice
const write = new Writable({
write(chunk, enc, cb) { cb(); }
});

write.end(common.mustCall());
write.destroy();
write.destroy();
}