The "transfer closed with outstanding read data remaining" error is not thrown when abort stream connection
See original GitHub issueI try to handle HTTP stream errors.
Steps to Reproduce
When some error occurred I want to abort response stream using:
res.connection.destroy(new Error('INVALID_END_OF_STREAM'));
Below is a simple stream source example.
const express = require('express');
(function () {
const app0 = express();
app0.get('/errdata', (req, res, next) => {
let i = 0;
res.setHeader('Transfer-Encoding', 'chunked');
res.setHeader('Content-type', 'application/json; charset=utf-8');
res.write('[');
const descriptor = setInterval(
() => {
if (++i > 3) {
clearInterval(descriptor);
console.log(new Date(), 'close connection');
// when some error occurred I want to abort response stream
res.connection.destroy(new Error('INVALID_END_OF_STREAM'));
return;
}
res.write(JSON.stringify({i: i, item: i}));
res.write(',');
},
1000);
});
app0.listen(1000, () => console.log('app0:', 1000));
})();
The stream consumer is another node process which uses the request library to handle the stream:
const xSrc = request({url: 'http://localhost:1000/errdata', method: 'GET', timeout: 10000});
xSrc.on('error', (err) => {
console.log('ERROR: ', err);
})
.on('data', (chunk) => {
console.log('CHUNK: ', chunk.toString().length);
})
.on('end', () => {
console.log('END');
});
Expected Behavior
I expect to see something like below since response stream is not closed properly:
> CHUNK: xx
> ...
> CHUNK: xx
> ERROR: NET::ERR_INCOMPLETE_CHUNKED_ENCODING
Current Behavior
Currently I don’t see any error. Request just ends without any marker of incomplete stream.
> CHUNK: xx
> ...
> END
While requesting URL from Chrome I’ve got request status:
(failed) NET::ERR_INCOMPLETE_CHUNKED_ENCODING
Request from CURL also gives an error:
>curl -i --raw http://localhost:1000/errdata
HTTP/1.1 200 OK
X-Powered-By: Express
Transfer-Encoding: chunked
Date: Tue, 30 May 2017 08:35:10 GMT
Connection: keep-alive
1
[
10
{"i":1,"item":1}
1
,
10
{"i":2,"item":2}
1
,
10
{"i":3,"item":3}
1
,
curl: (18) transfer closed with outstanding read data remaining
Workaround
At the moment I found a workaround to raise an error on client side by violating HTTP chunked transfer encoding. It is handled properly but I dislike this approach as a dirty hack.
res.write('[');
res.write('{"i": 1, "item": 1}');
...
// when error - violate chunked transfer by writing size-less chunk directly to connection
res.connection.write('INVALID_END_OF_STREAM');
res.end();
Summary
Do you think it should be considered as a bug? Let me know if I missed something. Thanks in advance.
Issue Analytics
- State:
- Created 6 years ago
- Reactions:1
- Comments:7
Top GitHub Comments
Any updates?
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.