-
-
Notifications
You must be signed in to change notification settings - Fork 36.1k
http: emit aborted event on requests #64693
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
Open
simhnna
wants to merge
1
commit into
nodejs:main
Choose a base branch
from
simhnna:emit-request-abort
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
test/parallel/test-http-server-aborted-after-request-end.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| 'use strict'; | ||
|
|
||
| // Regression test: the request object's 'aborted' event (and .aborted | ||
| // property) must still be emitted/set when the client destroys the | ||
| // connection, even if the request body had already been fully consumed | ||
| // (i.e. the request stream already emitted 'end') before the response | ||
| // finished. This complements test-http-server-aborted-while-reading-body.js, | ||
| // which covers aborting *while* the body is still being read. | ||
| // See https://github.com/nodejs/node/issues/40775. | ||
|
|
||
| const common = require('../common'); | ||
| const assert = require('assert'); | ||
| const http = require('http'); | ||
|
|
||
| let clientReq; | ||
| const server = http.createServer(common.mustCall((req, res) => { | ||
| // Close the server once the request stream is torn down, regardless of | ||
| // whether 'aborted' fired. This is guaranteed to happen (unlike | ||
| // 'aborted', which is exactly what's under test here), so a regression | ||
| // makes this test fail fast via the mustCall() check below instead of | ||
| // hanging forever. | ||
| req.on('close', common.mustCall(() => { | ||
| server.close(); | ||
| })); | ||
|
|
||
| req.on('aborted', common.mustCall(() => { | ||
| assert.strictEqual(req.aborted, true); | ||
| })); | ||
|
|
||
| req.on('end', common.mustCall(() => { | ||
| // At this point the request body has been fully read, but the | ||
| // response has not been sent yet. Destroying the client request | ||
| // now must still result in the server's 'aborted' event firing. | ||
| clientReq.destroy(); | ||
| })); | ||
|
|
||
| req.resume(); | ||
| })); | ||
|
|
||
| server.listen(0, common.mustCall(() => { | ||
| clientReq = http.request({ | ||
| method: 'POST', | ||
| port: server.address().port, | ||
| headers: { connection: 'keep-alive' }, | ||
| }, common.mustNotCall()); | ||
|
|
||
| // Destroying the request may or may not surface an error on the client | ||
| // side depending on timing; only the server-side 'aborted' event | ||
| // (asserted above) is under test here. | ||
| clientReq.on('error', () => {}); | ||
|
|
||
| clientReq.end(); | ||
| })); |
55 changes: 55 additions & 0 deletions
55
test/parallel/test-http-server-aborted-while-reading-body.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| 'use strict'; | ||
|
|
||
| // Regression test: the request object's 'aborted' event (and .aborted | ||
| // property) must be emitted when the client destroys the connection while | ||
| // the server is still in the middle of reading the request body, i.e. | ||
| // before 'end' has fired on the request stream. This is the "classic" | ||
| // abort case and complements | ||
| // test-http-server-aborted-after-request-end.js, which covers aborting | ||
| // *after* the body has already been fully read. | ||
| // See https://github.com/nodejs/node/issues/40775. | ||
|
|
||
| const common = require('../common'); | ||
| const assert = require('assert'); | ||
| const http = require('http'); | ||
|
|
||
| let clientReq; | ||
| const server = http.createServer(common.mustCall((req, res) => { | ||
| // Close the server once the request stream is torn down, regardless of | ||
| // whether 'aborted' fired. This is guaranteed to happen (unlike | ||
| // 'aborted', which is exactly what's under test here), so a regression | ||
| // makes this test fail fast via the mustCall() check below instead of | ||
| // hanging forever. | ||
| req.on('close', common.mustCall(() => { | ||
| server.close(); | ||
| })); | ||
|
|
||
| req.on('aborted', common.mustCall(() => { | ||
| assert.strictEqual(req.aborted, true); | ||
| assert.strictEqual(req.complete, false); | ||
| })); | ||
|
|
||
| req.on('data', common.mustCall(() => { | ||
| // Once the first chunk of the body has arrived, destroy the client | ||
| // connection before it finishes sending the rest of the body (and | ||
| // before the server ever calls res.end()/sends a response). | ||
| clientReq.destroy(); | ||
| })); | ||
| })); | ||
|
|
||
| server.listen(0, common.mustCall(() => { | ||
| clientReq = http.request({ | ||
| method: 'POST', | ||
| port: server.address().port, | ||
| headers: { connection: 'keep-alive' }, | ||
| }, common.mustNotCall()); | ||
|
|
||
| // Destroying the request may or may not surface an error on the client | ||
| // side depending on timing; only the server-side 'aborted' event | ||
| // (asserted above) is under test here. | ||
| clientReq.on('error', () => {}); | ||
|
|
||
| // Write a chunk but never call end(), so the server never sees the body | ||
| // finish before the connection is destroyed. | ||
| clientReq.write('some data'); | ||
| })); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What does this have to do with the deprecation?