summaryrefslogtreecommitdiff
path: root/ext/net/01_net.js
diff options
context:
space:
mode:
authorDivy Srivastava <dj.srivastava23@gmail.com>2024-01-04 08:51:39 +0530
committerGitHub <noreply@github.com>2024-01-04 08:51:39 +0530
commitad65440092ed2e5f85d1c7cadb6f59bef0c7be75 (patch)
treef31acce3ce9246315859b1e4ad777157260011c3 /ext/net/01_net.js
parenta0b687235907ce91358677353c00f575548313b4 (diff)
fix(ext/node): UdpSocket ref and unref (#21777)
Diffstat (limited to 'ext/net/01_net.js')
-rw-r--r--ext/net/01_net.js22
1 files changed, 20 insertions, 2 deletions
diff --git a/ext/net/01_net.js b/ext/net/01_net.js
index c4a921536..699423b22 100644
--- a/ext/net/01_net.js
+++ b/ext/net/01_net.js
@@ -295,6 +295,8 @@ class Listener {
class Datagram {
#rid = 0;
#addr = null;
+ #unref = false;
+ #promise = null;
constructor(rid, addr, bufSize = 1024) {
this.#rid = rid;
@@ -367,10 +369,12 @@ class Datagram {
let remoteAddr;
switch (this.addr.transport) {
case "udp": {
- ({ 0: nread, 1: remoteAddr } = await op_net_recv_udp(
+ this.#promise = op_net_recv_udp(
this.rid,
buf,
- ));
+ );
+ if (this.#unref) core.unrefOpPromise(this.#promise);
+ ({ 0: nread, 1: remoteAddr } = await this.#promise);
remoteAddr.transport = "udp";
break;
}
@@ -413,6 +417,20 @@ class Datagram {
core.close(this.rid);
}
+ ref() {
+ this.#unref = false;
+ if (this.#promise !== null) {
+ core.refOpPromise(this.#promise);
+ }
+ }
+
+ unref() {
+ this.#unref = true;
+ if (this.#promise !== null) {
+ core.unrefOpPromise(this.#promise);
+ }
+ }
+
async *[SymbolAsyncIterator]() {
while (true) {
try {