grantila / fetch-h2

HTTP/1+2 Fetch API client for Node.js
MIT License
336 stars 16 forks source link

redirected HTTP/2 request causes process exit #107

Closed stefan-guggisberg closed 4 years ago

stefan-guggisberg commented 4 years ago

Requesting a redirected resource over HTTP2 causes sudden process exit with code 0.

Code to reproduce:

const { fetch, disconnectAll } = require('fetch-h2');

const ts0 = Date.now();

process.on('exit', (code) => {
  const ts1 = Date.now();
  console.log(`exited after ${ts1 - ts0} ms with code: ${code}`);
});

(async () => {
  try {
    const url = 'https://nghttp2.org/httpbin/absolute-redirect/1';
    console.log(`fetching ${url} ...`);
    const resp = await fetch(url, {
      redirect: 'follow',
    });
    console.log(`Status: ${resp.status}, HTTP Version: ${resp.httpVersion}, redirected: ${resp.redirected}`);
    console.log(`Body:\n${await resp.text()}`);
  } catch (e) {
    console.error(e);
    throw e;
  } finally {
    await disconnectAll();
  }
})();

Running above code produces the following console output:

fetching https://nghttp2.org/httpbin/absolute-redirect/3 ...
exited after 808 ms with code: 0

The process exits during the fetch call with code 0 (success) because somehow no more async operations were pending.

This is a regression of changes introduced in version 2.4.0. All versions since 2.4.0 are affected. 2.3.0 OTOH does work as expected.

Workarounds:

stefan-guggisberg commented 4 years ago

Note: I can't reproduce the issue in a Mocha test. Probably because Mocha runs the tests in forked child processes.

stefan-guggisberg commented 4 years ago

@grantila This is a serious issue for us. Could you please have a look or at least direct me to the code that need's to be fixed?

github-actions[bot] commented 4 years ago

:tada: This issue has been resolved in version 2.5.0 :tada:

The release is available on:

Your semantic-release bot :package::rocket: