Skip to content

Commit

Permalink
dgram: socket add asyncDispose
Browse files Browse the repository at this point in the history
PR-URL: #48717
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
  • Loading branch information
atlowChemi authored and ruyadorno committed Sep 16, 2023
1 parent f74c2fc commit 573eb4b
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
12 changes: 12 additions & 0 deletions doc/api/dgram.md
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 @@ -988,4 +999,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
10 changes: 9 additions & 1 deletion lib/dgram.js
Expand Up @@ -30,6 +30,7 @@ const {
ObjectDefineProperty,
ObjectSetPrototypeOf,
ReflectApply,
SymbolAsyncDispose,
SymbolDispose,
} = primordials;

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

Socket.prototype[SymbolAsyncDispose] = async function() {
if (!this[kStateSymbol].handle) {
return;
}
return FunctionPrototypeCall(promisify(this.close), this);
};


function socketCloseNT(self) {
self.emit('close');
Expand Down
20 changes: 20 additions & 0 deletions test/parallel/test-dgram-async-dispose.mjs
@@ -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());
});
});

0 comments on commit 573eb4b

Please sign in to comment.