Skip to content

Commit 2202774

Browse files
committed
http: fix stalled pipeline bug (v3.x backport)
Original commit ab03635: This is a two-part fix: - Fix pending data notification in `OutgoingMessage` to notify server about flushed data too - Fix pause/resume behavior for the consumed socket. `resume` event is emitted on a next tick, and `socket._paused` can already be `true` at this time. Pause the socket again to avoid PAUSED error on parser. Fix: nodejs#3332 PR-URL: nodejs#3342 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Trevor Norris <[email protected]>
1 parent 8b54f40 commit 2202774

File tree

2 files changed

+72
-31
lines changed

2 files changed

+72
-31
lines changed

lib/_http_outgoing.js

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ OutgoingMessage.prototype._send = function(data, encoding, callback) {
124124
this.outputEncodings.unshift('binary');
125125
this.outputCallbacks.unshift(null);
126126
this.outputSize += this._header.length;
127-
if (this._onPendingData !== null)
127+
if (typeof this._onPendingData === 'function')
128128
this._onPendingData(this._header.length);
129129
}
130130
this._headerSent = true;
@@ -147,20 +147,7 @@ OutgoingMessage.prototype._writeRaw = function(data, encoding, callback) {
147147
// There might be pending data in the this.output buffer.
148148
var outputLength = this.output.length;
149149
if (outputLength > 0) {
150-
var output = this.output;
151-
var outputEncodings = this.outputEncodings;
152-
var outputCallbacks = this.outputCallbacks;
153-
for (var i = 0; i < outputLength; i++) {
154-
connection.write(output[i], outputEncodings[i],
155-
outputCallbacks[i]);
156-
}
157-
158-
this.output = [];
159-
this.outputEncodings = [];
160-
this.outputCallbacks = [];
161-
if (this._onPendingData !== null)
162-
this._onPendingData(-this.outputSize);
163-
this.outputSize = 0;
150+
this._flushOutput(connection);
164151
} else if (data.length === 0) {
165152
if (typeof callback === 'function')
166153
process.nextTick(callback);
@@ -185,7 +172,7 @@ OutgoingMessage.prototype._buffer = function(data, encoding, callback) {
185172
this.outputEncodings.push(encoding);
186173
this.outputCallbacks.push(callback);
187174
this.outputSize += data.length;
188-
if (this._onPendingData !== null)
175+
if (typeof this._onPendingData === 'function')
189176
this._onPendingData(data.length);
190177
return false;
191178
};
@@ -618,24 +605,11 @@ OutgoingMessage.prototype._finish = function() {
618605
// to attempt to flush any pending messages out to the socket.
619606
OutgoingMessage.prototype._flush = function() {
620607
var socket = this.socket;
621-
var outputLength, ret;
608+
var ret;
622609

623610
if (socket && socket.writable) {
624611
// There might be remaining data in this.output; write it out
625-
outputLength = this.output.length;
626-
if (outputLength > 0) {
627-
var output = this.output;
628-
var outputEncodings = this.outputEncodings;
629-
var outputCallbacks = this.outputCallbacks;
630-
for (var i = 0; i < outputLength; i++) {
631-
ret = socket.write(output[i], outputEncodings[i],
632-
outputCallbacks[i]);
633-
}
634-
635-
this.output = [];
636-
this.outputEncodings = [];
637-
this.outputCallbacks = [];
638-
}
612+
ret = this._flushOutput(socket);
639613

640614
if (this.finished) {
641615
// This is a queue to the server or client to bring in the next this.
@@ -647,6 +621,32 @@ OutgoingMessage.prototype._flush = function() {
647621
}
648622
};
649623

624+
OutgoingMessage.prototype._flushOutput = function _flushOutput(socket) {
625+
var ret;
626+
var outputLength = this.output.length;
627+
if (outputLength <= 0)
628+
return ret;
629+
630+
var output = this.output;
631+
var outputEncodings = this.outputEncodings;
632+
var outputCallbacks = this.outputCallbacks;
633+
socket.cork();
634+
for (var i = 0; i < outputLength; i++) {
635+
ret = socket.write(output[i], outputEncodings[i],
636+
outputCallbacks[i]);
637+
}
638+
socket.uncork();
639+
640+
this.output = [];
641+
this.outputEncodings = [];
642+
this.outputCallbacks = [];
643+
if (typeof this._onPendingData === 'function')
644+
this._onPendingData(-this.outputSize);
645+
this.outputSize = 0;
646+
647+
return ret;
648+
};
649+
650650

651651
OutgoingMessage.prototype.flushHeaders = function() {
652652
if (!this._header) {
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const http = require('http');
5+
const net = require('net');
6+
7+
const big = new Buffer(16 * 1024);
8+
big.fill('A');
9+
10+
const COUNT = 1e4;
11+
12+
var received = 0;
13+
14+
var client;
15+
const server = http.createServer(function(req, res) {
16+
res.end(big, function() {
17+
if (++received === COUNT) {
18+
server.close();
19+
client.end();
20+
}
21+
});
22+
}).listen(common.PORT, function() {
23+
var req = new Array(COUNT + 1).join('GET / HTTP/1.1\r\n\r\n');
24+
client = net.connect(common.PORT, function() {
25+
client.write(req);
26+
});
27+
28+
// Just let the test terminate instead of hanging
29+
client.on('close', function() {
30+
if (received !== COUNT)
31+
server.close();
32+
});
33+
client.resume();
34+
});
35+
36+
process.on('exit', function() {
37+
// The server should pause connection on pipeline flood, but it shoul still
38+
// resume it and finish processing the requests, when its output queue will
39+
// be empty again.
40+
assert.equal(received, COUNT);
41+
});

0 commit comments

Comments
 (0)