question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

The "transfer closed with outstanding read data remaining" error is not thrown when abort stream connection

See original GitHub issue

I 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:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:7

github_iconTop GitHub Comments

5reactions
vladminskycommented, Jun 6, 2017

Any updates?

0reactions
stale[bot]commented, Jul 21, 2021

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

error: RPC failed; curl transfer closed with outstanding read ...
The most common issue is that the connection closes and the whole clone is cancelled. Cloning into 'large-repository'... remote: Counting ...
Read more >
git clone fails due to "curl 18 transfer closed with outstanding ...
I hit this issue ( error: RPC failed; curl 18 transfer closed with outstanding read data remaining ) using cloud-hosted GitLab.com and self- ......
Read more >
EndReadWithoutValidation
To write or read data, we call a method on the connection object. ... IO is not allowed due to error stream state...
Read more >
About transactions | Cloud Spanner
Locking read-write transactions may abort, requiring the application to retry. ... until the transaction commits or a non-retryable error is encountered.
Read more >
How to Fix with java.net.SocketException: Connection reset ...
This occurred when Server closed the connection, while the client is still waiting to read data from its InputStream. For example, if you...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found