-
-
Notifications
You must be signed in to change notification settings - Fork 241
Do not swallow calls to res.end()
#188
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
Changes from all commits
53d771d
a9bb7b2
176847d
0ea9e82
e9309d1
4631d24
7ff0e90
3ae0d9b
57d735a
d431ae8
73576d9
fd1500b
175a50d
11f8f24
9cbf992
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,7 @@ var assert = require('assert') | |
| var bytes = require('bytes') | ||
| var crypto = require('crypto') | ||
| var http = require('http') | ||
| var net = require('net') | ||
| var request = require('supertest') | ||
| var zlib = require('zlib') | ||
| var http2 = require('http2') | ||
|
|
@@ -953,6 +954,198 @@ describe('compression()', function () { | |
| .expect(200, done) | ||
| }) | ||
| }) | ||
|
|
||
| describe('when the client closes the connection before consuming the response', function () { | ||
| it('should call the original res.end() if connection is cut early on', function (done) { | ||
| var server = http.createServer(function (req, res) { | ||
| var originalResEnd = res.end | ||
| var originalResEndCalledTimes = 0 | ||
| res.end = function () { | ||
| originalResEndCalledTimes++ | ||
| return originalResEnd.apply(this, arguments) | ||
| } | ||
|
|
||
| compression({ threshold: 0 })(req, res, function () { | ||
| socket.end() | ||
|
|
||
| res.setHeader('Content-Type', 'text/plain') | ||
| res.write('hello, ') | ||
| setTimeout(function () { | ||
| res.end('world!') | ||
|
|
||
| setTimeout(function () { | ||
| server.close(function () { | ||
| if (originalResEndCalledTimes === 1) { | ||
| done() | ||
| } else { | ||
| done(new Error('The original res.end() was called ' + originalResEndCalledTimes + ' times')) | ||
| } | ||
| }) | ||
| }, 5) | ||
|
Comment on lines
+976
to
+984
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Waiting for the condition to assert could be extracted into a generic helper, and it could also poll a couple of times if that is desired. I left this code in its naked form for now, and it is also duplicated in the other added test cases. I'm happy to improve this further as needed. |
||
| }, 5) | ||
| }) | ||
| }) | ||
|
|
||
| server.listen() | ||
|
|
||
| var port = server.address().port | ||
| var socket = openSocketWithRequest(port) | ||
| }) | ||
|
|
||
| it('should call the original res.end() if connection is cut right after setting headers', function (done) { | ||
| var server = http.createServer(function (req, res) { | ||
| var originalResEnd = res.end | ||
| var originalResEndCalledTimes = 0 | ||
| res.end = function () { | ||
| originalResEndCalledTimes++ | ||
| return originalResEnd.apply(this, arguments) | ||
| } | ||
|
|
||
| compression({ threshold: 0 })(req, res, function () { | ||
| res.setHeader('Content-Type', 'text/plain') | ||
| socket.end() | ||
|
|
||
| res.write('hello, ') | ||
| setTimeout(function () { | ||
| res.end('world!') | ||
|
|
||
| setTimeout(function () { | ||
| server.close(function () { | ||
| if (originalResEndCalledTimes === 1) { | ||
| done() | ||
| } else { | ||
| done(new Error('The original res.end() was called ' + originalResEndCalledTimes + ' times')) | ||
| } | ||
| }) | ||
| }, 5) | ||
| }, 5) | ||
| }) | ||
| }) | ||
|
|
||
| server.listen() | ||
|
|
||
| var port = server.address().port | ||
| var socket = openSocketWithRequest(port) | ||
| }) | ||
|
|
||
| it('should call the original res.end() if connection is cut after an initial write', function (done) { | ||
| var server = http.createServer(function (req, res) { | ||
| var originalResEnd = res.end | ||
| var originalResEndCalledTimes = 0 | ||
| res.end = function () { | ||
| originalResEndCalledTimes++ | ||
| return originalResEnd.apply(this, arguments) | ||
| } | ||
|
|
||
| compression({ threshold: 0 })(req, res, function () { | ||
| res.setHeader('Content-Type', 'text/plain') | ||
| res.write('hello, ') | ||
| socket.end() | ||
|
|
||
| setTimeout(function () { | ||
| res.end('world!') | ||
|
|
||
| setTimeout(function () { | ||
| server.close(function () { | ||
| if (originalResEndCalledTimes === 1) { | ||
| done() | ||
| } else { | ||
| done(new Error('The original res.end() was called ' + originalResEndCalledTimes + ' times')) | ||
| } | ||
| }) | ||
| }, 5) | ||
| }, 5) | ||
| }) | ||
| }) | ||
|
|
||
| server.listen() | ||
|
|
||
| var port = server.address().port | ||
| var socket = openSocketWithRequest(port) | ||
| }) | ||
|
|
||
| it('should call the original res.end() if connection is cut just after response body was generated', function (done) { | ||
| var server = http.createServer(function (req, res) { | ||
| var originalResEnd = res.end | ||
| var originalResEndCalledTimes = 0 | ||
| res.end = function () { | ||
| originalResEndCalledTimes++ | ||
| return originalResEnd.apply(this, arguments) | ||
| } | ||
|
|
||
| compression({ threshold: 0 })(req, res, function () { | ||
| res.setHeader('Content-Type', 'text/plain') | ||
| res.write('hello, ') | ||
| res.end('world!') | ||
| socket.end() | ||
|
|
||
| setTimeout(function () { | ||
| server.close(function () { | ||
| if (originalResEndCalledTimes === 1) { | ||
| done() | ||
| } else { | ||
| done(new Error('The original res.end() was called ' + originalResEndCalledTimes + ' times')) | ||
| } | ||
| }) | ||
| }, 5) | ||
| }) | ||
| }) | ||
|
|
||
| server.listen() | ||
|
|
||
| var port = server.address().port | ||
| var socket = openSocketWithRequest(port) | ||
| }) | ||
|
|
||
| it('should not trigger write errors if connection is cut just after response body was generated', function (done) { | ||
| var requestCount = 0 | ||
|
|
||
| var server = http.createServer(function (req, res) { | ||
| requestCount += 1 | ||
|
|
||
| var originalWrite = res.write | ||
| var writeError = null | ||
| res.write = function (chunk, callback) { | ||
| return originalWrite.call(this, chunk, function (error) { | ||
| if (error) { | ||
| writeError = error | ||
| } | ||
| return callback?.(error) | ||
| }) | ||
| } | ||
|
|
||
| var originalResEnd = res.end | ||
| res.end = function () { | ||
| setTimeout(function () { | ||
| if (writeError !== null) { | ||
| server.close(function () { | ||
| done(new Error(`Write error occurred: ${writeError}`)) | ||
| }) | ||
| } else { | ||
| if (requestCount < 50) { | ||
| socket = openSocketWithRequest(port) | ||
| } else { | ||
| server.close(done) | ||
| } | ||
| } | ||
| }, 0) | ||
| return originalResEnd.apply(this, arguments) | ||
| } | ||
|
|
||
| compression({ threshold: 0 })(req, res, function () { | ||
| res.setHeader('Content-Type', 'text/plain') | ||
| res.write('hello, ') | ||
| res.end('world!') | ||
| socket.end() | ||
| }) | ||
| }) | ||
|
|
||
| server.listen() | ||
|
|
||
| var port = server.address().port | ||
| var socket = openSocketWithRequest(port) | ||
| }) | ||
| }) | ||
| }) | ||
|
|
||
| function createServer (opts, fn) { | ||
|
|
@@ -1056,3 +1249,16 @@ function unchunk (encoding, onchunk, onend) { | |
| stream.on('end', onend) | ||
| } | ||
| } | ||
|
|
||
| function openSocketWithRequest (port) { | ||
| var socket = net.connect(port, function onConnect () { | ||
| socket.write('GET / HTTP/1.1\r\n') | ||
| socket.write('Accept-Encoding: gzip\r\n') | ||
| socket.write('Host: localhost:' + port + '\r\n') | ||
| socket.write('Content-Type: text/plain\r\n') | ||
| socket.write('Content-Length: 0\r\n') | ||
| socket.write('Connection: keep-alive\r\n') | ||
| socket.write('\r\n') | ||
| }) | ||
| return socket | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I still don't quite understand at what point this becomes necessary, given that finished runs after ('error', 'end', 'finish', and 'close') have already been emitted. Why should we call
stream.resume()?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, after doing more research, I think resume() is necessary — but it's already being called in the data event, so doing it again would be redundant.
I'm going to leave this here in case anyone has comments about it
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For completeness: There is no test that fails if this piece of code gets removed.
I kept it in place in case the sequence of events goes something like this:
stream.pause(), which is all completely fine.drainevent on the response does not get emitted.endevent, and the originalres.end()never gets called.I can attempt to write a test that captures this scenario but it might end up being somewhat artificial (i.e. relying on mocking
res.write()so it returnsfalsewithout the response appearing as finished). What do you think?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's better to add a comment explaining why it's necessary
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added a tentative comment in 11f8f24.