Skip to content

[fix] Request not being properly disposed when it encounters an error #1827

Open
@dipunm

Description

@dipunm

Describe the bug

Node.js version: 18.20.4

OS version: MacOs Sequoia 15.2

Description: After causing the request to crash by supplying undefined headers, the request is not properly closed and this causes a dangling handle that lasts for a few seconds.

Actual behavior

Expected behavior

The request should be ended, even if there are unexpected exceptions.

Code to reproduce

const superagent = require('superagent');

const r = superagent.get('https://www.google.com');
try {
        r.set({ 'x-header': undefined }).end((err, result) => console.log('completed', err, result));
//      r.set({ 'x-header': undefined }).then(result => console.log('completed', result)).catch(err => {
                console.error('rejected', err);
//              r.req.end();
        });
} catch(e) {
        console.log('caught', e);

//      r.req.end();
}

This code snippet is a setup for two scenarios:

  1. The current setup will use the end() syntax and when the error is caught, the application will hang for a few seconds and then exit.
  2. If you uncomment the r.req.end() line inside the catch, the application will exit immediately.
  3. If we comment out the first line inside the try block and uncomment the second line, we now are testing the promise based method. This also hangs before allowing the application to exit.
  4. If we comment out the r.req.end() line inside the .catch() handler, then the application exits immediately.

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions