Skip to content

Response promise resolves before response closes #642

@medikoo

Description

@medikoo

In some scenarios promise returned by e.g.response.text() resolves before 'close' event is emitted on Node.js response object (as emitted with requests's 'response' event).

It's not an issue per se, but:

  • Not sure if that's on par with how Fetch works in a browser.
  • It seems not possible to observe an async request/response process as a whole (it finalizes with 'close' event emitted on response, which is not exposed and ignored by node-fetch internals). It's an issue when e.g. we want to detect orphaned async flows, and for that want to guarantee that promises returned by fetch do not resolve prior response closes.

For example following test case will report an async leak in Node.js v12.2, when using node-fetch at v2.6.0

const fetch = require('node-fetch');

const checkAsyncLeak = () =>
  setTimeout(() => {
    throw new Error('Async leak!');
  }).unref();

fetch('https://google.com', { compress: false })
  .then(response => response.text())
  .then(() => {
    // All request related async jobs should be finalized by now
    checkAsyncLeak();
  });

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions