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

dgram, net: support Symbol.asyncDispose #48717

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions doc/api/dgram.md
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,17 @@ added: v0.1.99
Close the underlying socket and stop listening for data on it. If a callback is
provided, it is added as a listener for the [`'close'`][] event.

### `socket[Symbol.asyncDispose]()`

<!-- YAML
added: REPLACEME
-->

> Stability: 1 - Experimental

Calls [`socket.close()`][] and returns a promise that fulfills when the
socket has closed.

### `socket.connect(port[, address][, callback])`

<!-- YAML
Expand Down Expand Up @@ -992,4 +1003,5 @@ and `udp6` sockets). The bound address and port can be retrieved using
[`socket.address().address`]: #socketaddress
[`socket.address().port`]: #socketaddress
[`socket.bind()`]: #socketbindport-address-callback
[`socket.close()`]: #socketclosecallback
[byte length]: buffer.md#static-method-bufferbytelengthstring-encoding
11 changes: 11 additions & 0 deletions doc/api/net.md
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,17 @@ The optional `callback` will be called once the `'close'` event occurs. Unlike
that event, it will be called with an `Error` as its only argument if the server
was not open when it was closed.

### `server[Symbol.asyncDispose]()`

<!-- YAML
added: REPLACEME
-->

> Stability: 1 - Experimental

Calls [`server.close()`][] and returns a promise that fulfills when the
server has closed.

### `server.getConnections(callback)`

<!-- YAML
Expand Down
10 changes: 9 additions & 1 deletion lib/dgram.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const {
ObjectDefineProperty,
ObjectSetPrototypeOf,
ReflectApply,
SymbolAsyncDispose,
SymbolDispose,
} = primordials;

Expand Down Expand Up @@ -59,7 +60,7 @@ const {
validatePort,
} = require('internal/validators');
const { Buffer } = require('buffer');
const { deprecate, guessHandleType } = require('internal/util');
const { deprecate, guessHandleType, promisify } = require('internal/util');
const { isArrayBufferView } = require('internal/util/types');
const EventEmitter = require('events');
const {
Expand Down Expand Up @@ -752,6 +753,13 @@ Socket.prototype.close = function(callback) {
return this;
};

Socket.prototype[SymbolAsyncDispose] = async function() {
if (!this[kStateSymbol].handle) {
return;
}
return FunctionPrototypeCall(promisify(this.close), this);
atlowChemi marked this conversation as resolved.
Show resolved Hide resolved
};


function socketCloseNT(self) {
self.emit('close');
Expand Down
11 changes: 10 additions & 1 deletion lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@ const {
ArrayPrototypePush,
Boolean,
FunctionPrototypeBind,
FunctionPrototypeCall,
MathMax,
Number,
NumberIsNaN,
NumberParseInt,
ObjectDefineProperty,
ObjectSetPrototypeOf,
Symbol,
SymbolAsyncDispose,
SymbolDispose,
} = primordials;

Expand Down Expand Up @@ -112,7 +114,7 @@ const {
} = require('internal/errors');
const { isUint8Array } = require('internal/util/types');
const { queueMicrotask } = require('internal/process/task_queues');
const { kEmptyObject, guessHandleType } = require('internal/util');
const { kEmptyObject, guessHandleType, promisify } = require('internal/util');
const {
validateAbortSignal,
validateBoolean,
Expand Down Expand Up @@ -2243,6 +2245,13 @@ Server.prototype.close = function(cb) {
return this;
};

Server.prototype[SymbolAsyncDispose] = async function() {
if (!this._handle) {
return;
}
return FunctionPrototypeCall(promisify(this.close), this);
atlowChemi marked this conversation as resolved.
Show resolved Hide resolved
};

Server.prototype._emitCloseIfDrained = function() {
debug('SERVER _emitCloseIfDrained');

Expand Down
20 changes: 20 additions & 0 deletions test/parallel/test-dgram-async-dispose.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import * as common from '../common/index.mjs';
import assert from 'node:assert';
import dgram from 'node:dgram';
import { describe, it } from 'node:test';

describe('dgram.Socket[Symbol.asyncDispose]()', () => {
it('should close the socket', async () => {
const server = dgram.createSocket({ type: 'udp4' });
server.on('close', common.mustCall());
await server[Symbol.asyncDispose]().then(common.mustCall());

assert.throws(() => server.address(), { code: 'ERR_SOCKET_DGRAM_NOT_RUNNING' });
});

it('should resolve even if the socket is already closed', async () => {
const server = dgram.createSocket({ type: 'udp4' });
await server[Symbol.asyncDispose]().then(common.mustCall());
await server[Symbol.asyncDispose]().then(common.mustCall(), common.mustNotCall());
});
});
30 changes: 30 additions & 0 deletions test/parallel/test-net-server-async-dispose.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import * as common from '../common/index.mjs';
import assert from 'node:assert';
import net from 'node:net';
import { describe, it } from 'node:test';

describe('net.Server[Symbol.asyncDispose]()', () => {
it('should close the server', async () => {
const server = net.createServer();
const timeoutRef = setTimeout(common.mustNotCall(), 2 ** 31 - 1);

server.listen(0, common.mustCall(async () => {
await server[Symbol.asyncDispose]().then(common.mustCall());
assert.strictEqual(server.address(), null);
clearTimeout(timeoutRef);
}));

server.on('close', common.mustCall());
});

it('should resolve even if the server is already closed', async () => {
const server = net.createServer();
const timeoutRef = setTimeout(common.mustNotCall(), 2 ** 31 - 1);

server.listen(0, common.mustCall(async () => {
await server[Symbol.asyncDispose]().then(common.mustCall());
await server[Symbol.asyncDispose]().then(common.mustCall(), common.mustNotCall());
clearTimeout(timeoutRef);
}));
});
});