-
Notifications
You must be signed in to change notification settings - Fork 30.4k
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
net,tls: add abort signal support to connect #37735
Closed
Linkgoron
wants to merge
1
commit into
nodejs:master
from
Linkgoron:net-tls-add-connect-abort-signal
Closed
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 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 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 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 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 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,87 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
if (!common.hasCrypto) | ||
common.skip('missing crypto'); | ||
|
||
const assert = require('assert'); | ||
const https = require('https'); | ||
const { once } = require('events'); | ||
const Agent = https.Agent; | ||
const fixtures = require('../common/fixtures'); | ||
|
||
const { getEventListeners } = require('events'); | ||
const agent = new Agent(); | ||
|
||
const options = { | ||
key: fixtures.readKey('agent1-key.pem'), | ||
cert: fixtures.readKey('agent1-cert.pem') | ||
}; | ||
|
||
const server = https.createServer(options); | ||
|
||
server.listen(0, common.mustCall(async () => { | ||
const port = server.address().port; | ||
const host = 'localhost'; | ||
const options = { | ||
port: port, | ||
host: host, | ||
rejectUnauthorized: false, | ||
_agentKey: agent.getName({ port, host }) | ||
}; | ||
|
||
async function postCreateConnection() { | ||
const ac = new AbortController(); | ||
const { signal } = ac; | ||
const connection = agent.createConnection({ ...options, signal }); | ||
assert.strictEqual(getEventListeners(signal, 'abort').length, 1); | ||
ac.abort(); | ||
const [err] = await once(connection, 'error'); | ||
assert.strictEqual(err.name, 'AbortError'); | ||
} | ||
|
||
async function preCreateConnection() { | ||
const ac = new AbortController(); | ||
const { signal } = ac; | ||
ac.abort(); | ||
const connection = agent.createConnection({ ...options, signal }); | ||
const [err] = await once(connection, 'error'); | ||
assert.strictEqual(err.name, 'AbortError'); | ||
} | ||
|
||
|
||
async function agentAsParam() { | ||
const ac = new AbortController(); | ||
const { signal } = ac; | ||
const request = https.get({ | ||
port: server.address().port, | ||
path: '/hello', | ||
agent: agent, | ||
signal, | ||
}); | ||
assert.strictEqual(getEventListeners(signal, 'abort').length, 1); | ||
ac.abort(); | ||
const [err] = await once(request, 'error'); | ||
assert.strictEqual(err.name, 'AbortError'); | ||
} | ||
|
||
async function agentAsParamPreAbort() { | ||
const ac = new AbortController(); | ||
const { signal } = ac; | ||
ac.abort(); | ||
const request = https.get({ | ||
port: server.address().port, | ||
path: '/hello', | ||
agent: agent, | ||
signal, | ||
}); | ||
assert.strictEqual(getEventListeners(signal, 'abort').length, 0); | ||
const [err] = await once(request, 'error'); | ||
assert.strictEqual(err.name, 'AbortError'); | ||
} | ||
|
||
await postCreateConnection(); | ||
await preCreateConnection(); | ||
await agentAsParam(); | ||
await agentAsParamPreAbort(); | ||
server.close(); | ||
})); |
This file contains 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,96 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
const net = require('net'); | ||
const assert = require('assert'); | ||
const server = net.createServer(); | ||
const { getEventListeners, once } = require('events'); | ||
|
||
const liveConnections = new Set(); | ||
|
||
server.listen(0, common.mustCall(async () => { | ||
const port = server.address().port; | ||
const host = 'localhost'; | ||
const socketOptions = (signal) => ({ port, host, signal }); | ||
server.on('connection', (connection) => { | ||
liveConnections.add(connection); | ||
connection.on('close', () => { | ||
liveConnections.delete(connection); | ||
}); | ||
}); | ||
|
||
const assertAbort = async (socket, testName) => { | ||
try { | ||
await once(socket, 'close'); | ||
assert.fail(`close ${testName} should have thrown`); | ||
} catch (err) { | ||
assert.strictEqual(err.name, 'AbortError'); | ||
} | ||
}; | ||
|
||
async function postAbort() { | ||
const ac = new AbortController(); | ||
const { signal } = ac; | ||
const socket = net.connect(socketOptions(signal)); | ||
assert.strictEqual(getEventListeners(signal, 'abort').length, 1); | ||
ac.abort(); | ||
await assertAbort(socket, 'postAbort'); | ||
} | ||
|
||
async function preAbort() { | ||
const ac = new AbortController(); | ||
const { signal } = ac; | ||
ac.abort(); | ||
const socket = net.connect(socketOptions(signal)); | ||
assert.strictEqual(getEventListeners(signal, 'abort').length, 0); | ||
await assertAbort(socket, 'preAbort'); | ||
} | ||
|
||
async function tickAbort() { | ||
const ac = new AbortController(); | ||
const { signal } = ac; | ||
setImmediate(() => ac.abort()); | ||
const socket = net.connect(socketOptions(signal)); | ||
assert.strictEqual(getEventListeners(signal, 'abort').length, 1); | ||
await assertAbort(socket, 'tickAbort'); | ||
} | ||
|
||
async function testConstructor() { | ||
const ac = new AbortController(); | ||
const { signal } = ac; | ||
ac.abort(); | ||
const socket = new net.Socket(socketOptions(signal)); | ||
assert.strictEqual(getEventListeners(signal, 'abort').length, 0); | ||
await assertAbort(socket, 'testConstructor'); | ||
} | ||
|
||
async function testConstructorPost() { | ||
const ac = new AbortController(); | ||
const { signal } = ac; | ||
const socket = new net.Socket(socketOptions(signal)); | ||
assert.strictEqual(getEventListeners(signal, 'abort').length, 1); | ||
ac.abort(); | ||
await assertAbort(socket, 'testConstructorPost'); | ||
} | ||
|
||
async function testConstructorPostTick() { | ||
const ac = new AbortController(); | ||
const { signal } = ac; | ||
const socket = new net.Socket(socketOptions(signal)); | ||
assert.strictEqual(getEventListeners(signal, 'abort').length, 1); | ||
setImmediate(() => ac.abort()); | ||
await assertAbort(socket, 'testConstructorPostTick'); | ||
} | ||
|
||
await postAbort(); | ||
await preAbort(); | ||
await tickAbort(); | ||
await testConstructor(); | ||
await testConstructorPost(); | ||
await testConstructorPostTick(); | ||
|
||
// Killing the net.socket without connecting hangs the server. | ||
for (const connection of liveConnections) { | ||
connection.destroy(); | ||
} | ||
server.close(common.mustCall()); | ||
})); |
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.
Probably easier to call socket.setTimeout with a low timeout - but probably what you did is also fine