Repository navigation
http: add timeout option to server.close() #67851
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
araujogui
wants to merge
3
commits into
nodejs:main
Choose a base branch
from
araujogui:http-server-close-timeout
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
3 commits
Select commit
Hold shift + click to select a range
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
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,174 @@ | ||
| 'use strict'; | ||
|
|
||
| const common = require('../common'); | ||
| const assert = require('assert'); | ||
| const { createServer, get } = require('http'); | ||
| const net = require('net'); | ||
|
|
||
| function listen(server) { | ||
| return new Promise((resolve) => server.listen(0, resolve)); | ||
| } | ||
|
|
||
| // Active connections are closed when the timeout expires. | ||
| async function closesActiveConnections() { | ||
| const server = createServer(common.mustCall((req, res) => { | ||
| // Never send a response. | ||
| res.on('close', common.mustCall()); | ||
| const ret = server.close({ timeout: common.platformTimeout(100) }, | ||
| common.mustSucceed()); | ||
| assert.strictEqual(ret, server); | ||
| })); | ||
|
|
||
| await listen(server); | ||
|
|
||
| const { promise, resolve } = Promise.withResolvers(); | ||
| server.on('close', resolve); | ||
|
|
||
| get({ port: server.address().port }, common.mustNotCall()) | ||
| .on('error', common.mustCall((err) => { | ||
| assert.strictEqual(err.code, 'ECONNRESET'); | ||
| })); | ||
|
|
||
| await promise; | ||
| } | ||
|
|
||
| // Requests that end before the timeout are not interrupted. | ||
| async function doesNotInterruptRequests() { | ||
| const server = createServer(common.mustCall((req, res) => { | ||
| server.close({ timeout: common.platformTimeout(60_000) }, | ||
| common.mustSucceed()); | ||
| setTimeout(() => res.end('ok'), common.platformTimeout(50)); | ||
| })); | ||
|
|
||
| await listen(server); | ||
|
|
||
| const { promise, resolve } = Promise.withResolvers(); | ||
| server.on('close', resolve); | ||
|
|
||
| get({ | ||
| port: server.address().port, | ||
| headers: { connection: 'close' }, | ||
| }, common.mustCall((res) => { | ||
| let body = ''; | ||
| res.setEncoding('utf8'); | ||
| res.on('data', (chunk) => { body += chunk; }); | ||
| res.on('end', common.mustCall(() => { | ||
| assert.strictEqual(body, 'ok'); | ||
| })); | ||
| })); | ||
|
|
||
| await promise; | ||
| } | ||
|
|
||
| // An upgraded socket keeps the server open past the timeout. Calling close() | ||
| // again does not leak 'close' listeners. | ||
| async function closeAgainAfterTimeout() { | ||
| const server = createServer(common.mustNotCall()); | ||
|
|
||
| server.on('upgrade', common.mustCall((req, socket) => { | ||
| const baseline = server.listenerCount('close'); | ||
| server.close({ timeout: common.platformTimeout(10) }); | ||
| assert.strictEqual(server.listenerCount('close'), baseline + 1); | ||
|
|
||
| setTimeout(common.mustCall(() => { | ||
| assert.strictEqual(server.listening, false); | ||
| assert.strictEqual(server.listenerCount('close'), baseline); | ||
|
|
||
| server.close({ timeout: common.platformTimeout(60_000) }); | ||
| assert.strictEqual(server.listenerCount('close'), baseline + 1); | ||
|
|
||
| server.once('close', common.mustCall(() => { | ||
| assert.strictEqual(server.listenerCount('close'), baseline); | ||
| })); | ||
| socket.destroy(); | ||
| }), common.platformTimeout(50)); | ||
| })); | ||
|
|
||
| await listen(server); | ||
|
|
||
| const { promise, resolve } = Promise.withResolvers(); | ||
| server.on('close', resolve); | ||
|
|
||
| const client = net.connect(server.address().port, common.mustCall(() => { | ||
| client.write('GET / HTTP/1.1\r\n' + | ||
| 'Host: localhost\r\n' + | ||
| 'Connection: Upgrade\r\n' + | ||
| 'Upgrade: test\r\n\r\n'); | ||
| })); | ||
| client.resume(); | ||
| client.on('close', common.mustCall()); | ||
|
|
||
| await promise; | ||
| } | ||
|
|
||
| // Listening again cancels the timeout. | ||
| async function listenAgainCancelsTimeout() { | ||
| const server = createServer(common.mustCall((req, res) => { | ||
| const baseline = server.listenerCount('close'); | ||
| server.close({ timeout: common.platformTimeout(10) }); | ||
| assert.strictEqual(server.listenerCount('close'), baseline + 1); | ||
|
|
||
| server.listen(0, common.mustCall(() => { | ||
| assert.strictEqual(server.listenerCount('close'), baseline); | ||
|
|
||
| // Close first, or closeIdleConnections() destroys the socket. | ||
| setTimeout(common.mustCall(() => { | ||
| server.close(); | ||
| res.end('ok'); | ||
| }), common.platformTimeout(50)); | ||
| })); | ||
| })); | ||
|
|
||
| await listen(server); | ||
|
|
||
| const { promise, resolve } = Promise.withResolvers(); | ||
| server.on('close', resolve); | ||
|
|
||
| get({ | ||
| port: server.address().port, | ||
| headers: { connection: 'close' }, | ||
| }, common.mustCall((res) => { | ||
| res.resume(); | ||
| res.on('end', common.mustCall()); | ||
| })); | ||
|
|
||
| await promise; | ||
| } | ||
|
|
||
| // Options are validated before the server stops listening. | ||
| async function validatesOptions() { | ||
| const server = createServer(common.mustNotCall()); | ||
|
|
||
| await listen(server); | ||
|
|
||
| for (const timeout of [0, -1, 1.5, 2 ** 31, NaN, Infinity]) { | ||
| assert.throws(() => server.close({ timeout }), { | ||
| code: 'ERR_OUT_OF_RANGE', | ||
| name: 'RangeError', | ||
| }); | ||
| } | ||
|
|
||
| for (const timeout of ['1', null, true, {}, 1n]) { | ||
| assert.throws(() => server.close({ timeout }), { | ||
| code: 'ERR_INVALID_ARG_TYPE', | ||
| name: 'TypeError', | ||
| }); | ||
| } | ||
|
|
||
| assert.strictEqual(server.listening, true); | ||
|
|
||
| // Other first arguments are ignored. | ||
| const { promise, resolve } = Promise.withResolvers(); | ||
| server.on('close', resolve); | ||
| server.close('SIGTERM'); | ||
|
|
||
| await promise; | ||
| } | ||
|
|
||
| (async () => { | ||
| await closesActiveConnections(); | ||
| await doesNotInterruptRequests(); | ||
| await closeAgainAfterTimeout(); | ||
| await listenAgainCancelsTimeout(); | ||
| await validatesOptions(); | ||
| })().then(common.mustCall()); |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.