Skip to content
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

http request 'end' event not fired #21398

Closed
gonenduk opened this issue Jun 19, 2018 · 12 comments
Closed

http request 'end' event not fired #21398

gonenduk opened this issue Jun 19, 2018 · 12 comments
Labels
http Issues or PRs related to the http subsystem. question Issues that look for answers. stream Issues and PRs related to the stream subsystem.

Comments

@gonenduk
Copy link
Contributor

Hi,

I am using the http request and breakdown the response times using events emitted at different stages of the request/response process.
With node < 10 it used to work well. With node >= 10 it has stopped working. The on end event of the response is not called. If I remove the readable event - then it is called.

Here is a sample code of rising stack on this subject (measuring http timings). Their sample also works on node < 10 but has the same problem with node >= 10:
Rising Stack sample code

Was anything changed in the way http calls are made in version 10?

@apapirovski
Copy link
Member

apapirovski commented Jun 20, 2018

@gonenduk there was a change around the semantics, yes. See the following section of the Streams documentation: https://nodejs.org/dist/latest-v10.x/docs/api/stream.html#stream_event_readable

@apapirovski apapirovski added question Issues that look for answers. http Issues or PRs related to the http subsystem. stream Issues and PRs related to the stream subsystem. labels Jun 20, 2018
@apapirovski
Copy link
Member

ping @mcollina — is this currently working fully as intended?

@gonenduk
Copy link
Contributor Author

Do we have a document with the changes in version 10 about this?
In the sample code I linked, the end event if not fired on version 10 but it is fired on previous versions.
Need to know what was the breaking change to adopt the code to the changes.

@mcollina
Copy link
Member

mcollina commented Jun 20, 2018 via email

@gonenduk
Copy link
Contributor Author

any updates?

@yosuke-furukawa
Copy link
Member

similar issue is opened from my lib.
https://github.com/yosuke-furukawa/httpstat

http is called end event, but https is not called end when readable is not finished.

    const req = protocol.request(url, (res) => {
      res.once('readable', () => {
        onTransfer = Date.now();
      });
      res.on('data', (chunk) => {
        body += chunk;
      });
      res.on('end', () => {
        onTotal = Date.now();
        res.body = body;
        resolve({
          url: url,
          response: res,
          time: {
            begin: begin,
            onLookup: onLookup,
            onConnect: onConnect,
            onSecureConnect: onSecureConnect,
            onTransfer: onTransfer,
            onTotal: onTotal,
          }
        });
      });
    });

@mcollina
Copy link
Member

mcollina commented Jul 5, 2018

I think there is a bug when using both 'readable' and 'data'.

@gonenduk
Copy link
Contributor Author

gonenduk commented Jul 5, 2018

But why is the end event not fired with node 10 and was always fired with 8 and below?

@mcollina
Copy link
Member

mcollina commented Jul 5, 2018

@gonenduk I think it's due to #18994 possibly in combination of the _readableState manipulation within http. Moreover we are not really testing this use anywhere.

@lundibundi
Copy link
Member

lundibundi commented Jul 6, 2018

@gonenduk until the bug is fixed, if you need it to work you can put listener on data event inside 'readable' event:

    res.once('readable', () => {
      eventTimes.firstByteAt = process.hrtime()
      res.on('data', (chunk) => { responseBody += chunk })
    })

This seems to work fine.

@gonenduk
Copy link
Contributor Author

gonenduk commented Jul 7, 2018

Thanks!
Adding an empty data event inside the readable event did solve the problem:
res.on('data', () => { })

The end event is fired!

lundibundi added a commit to lundibundi/node that referenced this issue Aug 9, 2018
When there is at least one 'data' listener try to flow when
last 'readable' listener gets removed and the stream is not piped.

Currently if we have both 'readable' and 'data' listeners set only
'readable' listener will get called and stream will be stalled without
'data' and 'end' events if 'readable' listener neither read()'s nor
resume()'s the stream.

Fixes: nodejs#21398
@Trott Trott closed this as completed in 98cf84f Aug 22, 2018
@Trott
Copy link
Member

Trott commented Aug 22, 2018

Fixed in 98cf84f

targos pushed a commit that referenced this issue Aug 24, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
http Issues or PRs related to the http subsystem. question Issues that look for answers. stream Issues and PRs related to the stream subsystem.
Projects
None yet
Development

No branches or pull requests

6 participants