Skip to content

http request 'end' event not fired #21398

Closed

Description

@gonenduk

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?

Activity

  1. apapirovski commented on Jun 20, 2018

    @apapirovski
    Contributor

    @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

  2. added
    questionIssues asking questions about Node.js.
    httpIssues and PRs related to the http subsystem.
    streamIssues and PRs related to Node.js streams.
    on Jun 20, 2018
  3. apapirovski commented on Jun 20, 2018

    @apapirovski
    Contributor

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

  4. gonenduk commented on Jun 20, 2018

    @gonenduk
    ContributorAuthor

    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.

  5. mcollina commented on Jun 20, 2018

    @mcollina
    SponsorMember
  6. gonenduk commented on Jun 28, 2018

    @gonenduk
    ContributorAuthor

    any updates?

  7. yosuke-furukawa commented on Jul 2, 2018

    @yosuke-furukawa
    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,
              }
            });
          });
        });
  8. mcollina commented on Jul 5, 2018

    @mcollina
    SponsorMember

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

  9. gonenduk commented on Jul 5, 2018

    @gonenduk
    ContributorAuthor

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

  10. mcollina commented on Jul 5, 2018

    @mcollina
    SponsorMember

    @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.

  11. lundibundi commented on Jul 6, 2018

    @lundibundi
    Member

    @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.

  12. gonenduk commented on Jul 7, 2018

    @gonenduk
    ContributorAuthor

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

    The end event is fired!

  13. Trott commented on Aug 22, 2018

    @Trott
    Member

    Fixed in 98cf84f

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    httpIssues and PRs related to the http subsystem.questionIssues asking questions about Node.js.streamIssues and PRs related to Node.js streams.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions