-
-
Notifications
You must be signed in to change notification settings - Fork 35.2k
http: add req.signal to IncomingMessage #62541
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
akshatsrivastava11
wants to merge
4
commits into
nodejs:main
Choose a base branch
from
akshatsrivastava11:akshatsrivastava11/feature/req.signal
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
Show all changes
4 commits
Select commit
Hold shift + click to select a range
e265c6d
http: add req.signal to IncomingMessage
akshatsrivastava11 18f6103
test: add test for res.signal on client side http.request() response
akshatsrivastava11 b9a1333
http: initialize kAbortController to null instead of undefined
akshatsrivastava11 bdf63e1
http: remove abort listener and add test for server-side req.signal.a…
akshatsrivastava11 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| 'use strict'; | ||
|
|
||
| const common = require('../common'); | ||
| const assert = require('assert'); | ||
| const http = require('http'); | ||
|
|
||
| // Test 1: req.signal is an AbortSignal and aborts on 'close' | ||
| { | ||
| const server = http.createServer(common.mustCall((req, res) => { | ||
| assert.ok(req.signal instanceof AbortSignal); | ||
| assert.strictEqual(req.signal.aborted, false); | ||
| req.signal.onabort = common.mustCall(() => { | ||
| assert.strictEqual(req.signal.aborted, true); | ||
| }); | ||
| res.destroy(); | ||
| })); | ||
| server.listen(0, common.mustCall(() => { | ||
| http.get({ port: server.address().port }, () => {}).on('error', () => { | ||
| server.close(); | ||
| }); | ||
| })); | ||
| } | ||
|
|
||
| // Test 2: req.signal is aborted if accessed after destroy | ||
| { | ||
| const req = new http.IncomingMessage(null); | ||
| req.destroy(); | ||
| assert.strictEqual(req.signal.aborted, true); | ||
| } | ||
|
|
||
| // Test 3: Multiple accesses return the same signal | ||
| { | ||
| const req = new http.IncomingMessage(null); | ||
| assert.strictEqual(req.signal, req.signal); | ||
| } | ||
|
|
||
|
|
||
| // Test 4: res.signal on a client-side http.request() response (IncomingMessage). | ||
| { | ||
| const server = http.createServer(common.mustCall((req, res) => { | ||
| res.writeHead(200); | ||
| res.write('partial'); | ||
| })); | ||
|
|
||
| server.listen(0, common.mustCall(() => { | ||
| const clientReq = http.request( | ||
| { port: server.address().port }, | ||
| common.mustCall((res) => { | ||
| assert.ok(res.signal instanceof AbortSignal); | ||
| assert.strictEqual(res.signal.aborted, false); | ||
|
|
||
| res.signal.onabort = common.mustCall(() => { | ||
| assert.strictEqual(res.signal.aborted, true); | ||
| server.close(); | ||
| }); | ||
| clientReq.destroy(); | ||
| }), | ||
| ); | ||
| clientReq.on('error', () => {}); | ||
| clientReq.end(); | ||
| })); | ||
| } | ||
|
|
||
| // Test 5: Client cancels a pending request. | ||
| { | ||
| const server = http.createServer(common.mustCall((req, res) => { | ||
| req.signal.onabort = common.mustCall(() => { | ||
| assert.strictEqual(req.signal.aborted, true); | ||
| server.close(); | ||
| }); | ||
| res.flushHeaders(); | ||
| })); | ||
|
|
||
| server.listen(0, common.mustCall(() => { | ||
| const clientReq = http.request( | ||
| { port: server.address().port }, | ||
| common.mustCall((res) => { | ||
| res.on('error', () => {}); | ||
| clientReq.destroy(); | ||
| }), | ||
| ); | ||
| clientReq.on('error', () => {}); | ||
| clientReq.end(); | ||
| })); | ||
| } | ||
Oops, something went wrong.
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.
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.
I think the most important case here isn't covered: server receives a request & doesn't end the response (stays pending) => client aborts the request while it's incomplete (so the connection closes) => server should see
req.signal.aborted == true. That's the primary end goal, allowing us to easily detect on the server side when the client cancelled the requests.The two full-flow tests here (1 and 5) only cover
serverRes.destroy() => serverReq.signal.abortedandclientReq.destroy() => clientRes.signal.aborted.Looks like it should work, but we need to test to make sure we actually cover that integration.
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.
Thanks,I have added the test in the latest commit