forwardemail / forwardemail/superagent

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

Open
#1,827 1 comment 0 reactions 0 assignees View on GitHub
Bug
Dominant language
JavaScript
Stars
16.6k
Forks
1.3k
PR merge metrics
No merged PRs in 30d

Description

## 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

```js
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

- [x] I have searched through GitHub issues for similar issues.
- [x] I have completely read through the README and documentation.
- [x] I have tested my code with the latest version of Node.js and this package and confirmed it is still not working.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.