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

http: emit 'aborted' event for ClientRequest #15270

Closed
wants to merge 2 commits into from
Closed

http: emit 'aborted' event for ClientRequest #15270

wants to merge 2 commits into from

Conversation

ronag
Copy link
Member

@ronag ronag commented Sep 8, 2017

Not sure if this is entirely correct. I think we need a check for whether the request completed normally before emitting aborted.

It also needs a test case.

@nodejs-github-bot nodejs-github-bot added the http Issues or PRs related to the http subsystem. label Sep 8, 2017
@mscdex
Copy link
Contributor

mscdex commented Sep 8, 2017

If we are going introduce this new event, I would think it should only be emitted if the user calls .abort().

@ronag
Copy link
Member Author

ronag commented Sep 8, 2017

@mscdex: It's not a new event see (https://nodejs.org/api/http.html#http_event_aborted). It's a bug. See, #15259

There is already a working event for abort() calls. See, https://nodejs.org/api/http.html#http_event_abort

req.emit('close');
if (req.res && req.res.readable) {
// Socket closed before we emitted 'end' below.
req.res.emit('aborted');
req.aborted = Date.now();
Copy link
Member

Choose a reason for hiding this comment

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

I think this property is currently only set when calling req.abort() so this looks like a semver-major change.

@ronag
Copy link
Member Author

ronag commented Sep 10, 2017

@lpinca: Removed sem major breaking changes.

req.emit('error', createHangUpError());
}

req.emit('close');
Copy link
Member Author

@ronag ronag Sep 10, 2017

Choose a reason for hiding this comment

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

Is this correct? I think close should be emitted last. Otherwise, error might be ignored, e.g some people do:

new Promise((resolve, reject) => req.on('close', resolve).on('error', reject))

Where the error is ignored since the promise is already resolved through close.

@@ -374,9 +375,12 @@ function socketCloseListener() {
// receive a response. The error needs to
// fire on the request.
req.socket._hadError = true;
req.emit('aborted');
req.emit('error', createHangUpError());
Copy link
Member Author

@ronag ronag Sep 10, 2017

Choose a reason for hiding this comment

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

I'm not sure error makes sense here. But changing that would be a sem-major change. @apapirovski what consequences would be removing it in favor of aborted have, e.g. during pump?

Copy link
Member Author

@ronag ronag Sep 11, 2017

Choose a reason for hiding this comment

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

I think aborted should at least be emitted after error in case someone removes their error listener after aborted? Anyone else?

Copy link
Member

Choose a reason for hiding this comment

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

I'm 👎 in not emitting an error here.

Copy link
Member Author

Choose a reason for hiding this comment

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

@mcollina: aborted after or before error?

Copy link
Member

Choose a reason for hiding this comment

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

aborted after.

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed

if (req.res && req.res.readable) {
// Socket closed before we emitted 'end' below.
req.emit('aborted');
Copy link
Member

@apapirovski apapirovski Sep 11, 2017

Choose a reason for hiding this comment

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

Is this correct? This branch means there's a response coming but we haven't received its end. I don't think this qualifies for an 'aborted' event for the request (only the response).

If this is motivated by how this works in h2, it's slightly different there because you have the request & response in one (duplex stream), instead of separate like with http1.

Then again, maybe I'm severely overthinking this.

Copy link
Member Author

@ronag ronag Sep 11, 2017

Choose a reason for hiding this comment

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

Well, from an api user's perspective the response could conceptually be part of the request (think h2)?

Copy link
Member Author

@ronag ronag Sep 11, 2017

Choose a reason for hiding this comment

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

I think both behaviours make sense. I would prefer the h2 way just to avoid confusion.

@mcollina
Copy link
Member

We need to test this with the pump module: https://www.npmjs.com/package/pump.

This needs unit tests as well.

@mcollina
Copy link
Member

@ronag would you mind adding a unit test for this?

@ronag
Copy link
Member Author

ronag commented Sep 14, 2017

@mcollina: I haven't quite figured out the unit test thing for node yet. Maybe over the weekend.

@gibfahn
Copy link
Member

gibfahn commented Sep 17, 2017

@ronag there is a guide to writing tests here, might be useful.

@ronag
Copy link
Member Author

ronag commented Sep 19, 2017

We might also consider emitting a ECONNRESET for the response case as well and then deprecate the aborted event (i.e. remove it from the docs)?

@mcollina?

@mcollina
Copy link
Member

@ronag, one thing at a time. Deprecating something is way more sensitive than fixing a bug.

@ronag
Copy link
Member Author

ronag commented Sep 19, 2017

@mcollina: I'll make it a separate issue

@ronag
Copy link
Member Author

ronag commented Sep 19, 2017

Competing PR #15471

@lpinca lpinca added the semver-minor PRs that contain new features and should be released in the next minor version. label Sep 19, 2017
@lpinca
Copy link
Member

lpinca commented Sep 19, 2017

Added the semver-minor label as this is technically a new event. The 'aborted' event on http.ClientRequest is currently a documentation bug.

@BridgeAR
Copy link
Member

Closing because #15471 landed. @ronag thanks a lot for your contribution and for bringing this up!

@BridgeAR BridgeAR closed this Sep 24, 2017
@ronag
Copy link
Member Author

ronag commented Sep 24, 2017

@BridgeAR: Great! However, we still need to move the close emit to the end.

@BridgeAR
Copy link
Member

Ok, I guess you want to have this reopened.

@BridgeAR BridgeAR reopened this Sep 24, 2017
@BridgeAR
Copy link
Member

I see you just opened a new PR in the very same moment I reopened this PR. That was timing ^^.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
http Issues or PRs related to the http subsystem. semver-minor PRs that contain new features and should be released in the next minor version.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants