Skip to content

Commit

Permalink
http_server: fix resume after socket close
Browse files Browse the repository at this point in the history
Socket resume may happen on a next tick, and in following scenario:

1. `socket.resume()`
2. `socket._handle.close()`
3. `socket._handle = null;`

The `_resume` will be invoked with empty `._handle` property. There is
nothing bad about it, and we should just ignore the `resume`/`pause`
events in this case.

Same applies to the unconsuming of socket on adding `data` and/or
`readable` event listeners.

Fix: #2821

PR-URL: #2824
Reviewed-By: Сковорода Никита Андреевич <chalkerx@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
  • Loading branch information
indutny authored and rvagg committed Sep 15, 2015
1 parent 777eb00 commit d8371a8
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
8 changes: 5 additions & 3 deletions lib/_http_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -512,11 +512,13 @@ function connectionListener(socket) {
exports._connectionListener = connectionListener;

function onSocketResume() {
this._handle.readStart();
if (this._handle)
this._handle.readStart();
}

function onSocketPause() {
this._handle.readStop();
if (this._handle)
this._handle.readStop();
}

function socketOnWrap(ev, fn) {
Expand All @@ -526,7 +528,7 @@ function socketOnWrap(ev, fn) {
return res;
}

if (ev === 'data' || ev === 'readable')
if (this._handle && (ev === 'data' || ev === 'readable'))
this.parser.unconsume(this._handle._externalStream);

return res;
Expand Down
24 changes: 24 additions & 0 deletions test/parallel/test-http-regr-gh-2821.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const http = require('http');

const server = http.createServer(function(req, res) {
res.writeHead(200);
res.end();

server.close();
});

server.listen(common.PORT, function() {

const req = http.request({
method: 'POST',
port: common.PORT
});

const payload = new Buffer(16390);
payload.fill('Й');
req.write(payload);
req.end();
});

0 comments on commit d8371a8

Please sign in to comment.