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

fix: abort request when abort signal received #30

Conversation

achingbrain
Copy link

In the case where responses don't end immediately, abort/destroy the request as well as the response as otherwise the connection is left open and the server never receives the 'abort' event for the request.

I noticed this as I've got some http endpoints that send data over time rather than finishing up the request immediately, the solution seems to be to close the request stream on the client as well as the response body stream.

In the case where responses don't end immediately, abort/destroy the request
as well as the response as otherwise the connection is left open and the
server never receives the 'abort' event for the request.
@codecov
Copy link

codecov bot commented Oct 9, 2020

Codecov Report

Merging #30 into master will increase coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master      #30   +/-   ##
=======================================
  Coverage   99.55%   99.56%           
=======================================
  Files           6        6           
  Lines         452      455    +3     
  Branches      147      148    +1     
=======================================
+ Hits          450      453    +3     
  Misses          1        1           
  Partials        1        1           
Impacted Files Coverage Δ
src/index.js 98.30% <100.00%> (+0.04%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update b6a8cb7...5814a00. Read the comment docs.

@achingbrain
Copy link
Author

@arantes555 are there any changes you'd like to see here or can this be merged?

@arantes555
Copy link
Owner

Sorry for the delay, I completely missed this PR 🤦

I'll get on it!

Copy link
Owner

@arantes555 arantes555 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, this approach does not send abort to the server in certain cases (timeouts, error on response body, ...)

I will try to fix all these issues in another branch with a slightly different approach.

Comment on lines +194 to +198
if (request.useElectronNet) {
req.abort()
} else {
req.destroy()
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (request.useElectronNet) {
req.abort()
} else {
req.destroy()
}
if (request.useElectronNet) {
req.abort() // in electron, `req.destroy()` does not send abort to server
} else {
req.destroy() // in node.js, `req.abort()` is deprecated
}

if (!aborted) {
clearInterval(interval)
res.end()
throw new Error('request was not aborted')
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not a fan of this, as it causes an uncaught exception... In addition to not being good practice, when there is a problem, it causes the tests to fail in another test-case as the one that actually causes the problem.

@arantes555
Copy link
Owner

@achingbrain I just pushed to another pull request : #31

I believe this PR addresses the problem you were trying to fix, while also resolving my concerns about your modifications.

Can you confirm that what I'm proposing works for you?

When confirmed, I will merge and publish

@achingbrain
Copy link
Author

It looks like it does, the test I added passes with the changes in #31 - thanks for picking this up.

@achingbrain
Copy link
Author

Closing in favour of #31

@arantes555
Copy link
Owner

@achingbrain this has now been published as v1.7.2.

(About the test you added, I removed it because I actually implemented the same logic of checking if the request actually appears to be finished for the server after every single test ^^ that's how I caught the timeout / encoding error thing)

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

Successfully merging this pull request may close these issues.

2 participants