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

"superagent: double callback bug" with timeout #1801

Open
3 tasks done
gramakri opened this issue Apr 23, 2024 · 1 comment
Open
3 tasks done

"superagent: double callback bug" with timeout #1801

gramakri opened this issue Apr 23, 2024 · 1 comment
Labels

Comments

@gramakri
Copy link

gramakri commented Apr 23, 2024

Describe the bug

Node.js version: v18.16.0

OS version: ubuntu 22.04

Description:
When a request aborts with a timeout, it calls the callback twice resulting in a message being printed on the console "superagent: double callback bug"

Expected behavior

Should not print the console message and should not call the callback twice

Code to reproduce

const superagent = require('superagent');

(async function () {
    try {
        await superagent.get('https://some/slow/api').timeout(20000);
    } catch (error) {
        console.log(error);
    }
})();

Output (with DEBUG=*) :

  superagent GET https://some/slow/api +0ms
  superagent GET https://some/slow/api -> 200 +5s

Error: Timeout of 20000ms exceeded
    at RequestBase._timeoutError (/home/yellowtent/box/node_modules/superagent/lib/request-base.js:712:17)
    at Timeout.<anonymous> (/home/yellowtent/box/node_modules/superagent/lib/request-base.js:727:12)
    at listOnTimeout (node:internal/timers:569:17)
    at process.processTimers (node:internal/timers:512:7) {
  timeout: 20000,
  code: 'ECONNABORTED',
  errno: 'ETIME',
  response: undefined
}
superagent: double callback bug

Checklist

  • I have searched through GitHub issues for similar issues.
  • I have completely read through the README and documentation.
  • I have tested my code with the latest version of Node.js and this package and confirmed it is still not working.
@gramakri gramakri added the Bug label Apr 23, 2024
@gramakri
Copy link
Author

Unfortunately, this timeout error doens't happen always but most of the time. I debugged this a bit and the second callback error that causes the message is:

Error: aborted
    at connResetException (node:internal/errors:717:14)
    at TLSSocket.socketCloseListener (node:_http_client:462:19)
    at TLSSocket.emit (node:events:525:35)
    at node:net:322:12
    at TCP.done (node:_tls_wrap:588:7) {
  code: 'ECONNRESET'
}

The timeout() code calls abort which maybe somehow ends up with an 'error' event with reset ? This matches the behavior outlined in https://nodejs.org/api/http.html#httprequesturl-options-callback which says:

In the case of a premature connection close after the response is received, the following events will be emitted in the following order:

    'socket'
    'response'
        'data' any number of times, on the res object
    (connection closed here)
    'aborted' on the res object
    'error' on the res object with an error with message 'Error: aborted' and code 'ECONNRESET'
    'close'
    'close' on the res object

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant