-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
feat(node): ref/unref for udp #21521
Conversation
Bump, any thoughts? |
@littledivy please review |
Sorry for the linting issues, had some troubles running it at first, will fix asap when I'll get on my computer. |
if (this.#listener) { | ||
this.#listener!.unref(); | ||
} |
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.
if (this.#listener) { | |
this.#listener!.unref(); | |
} | |
this.#listener?.unref(); |
ext/net/01_net.js
Outdated
ref() { | ||
|
||
} | ||
|
||
unref() { | ||
|
||
} |
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.
Instead of exposing public APIs for ref and unref in Deno.Datagram
it should be implemented internally. Something like this:
Add a new private fields this.#recvPromise = null
and this.#unrefed = false
in UdpWrap
.
In #recieve
:
[buf, remoteAddr] = (await this.#listener!.receive(p)) as [ |
Change this line to store the current recieve promise in the field.
this.#recvPromise = this.#listener!.receive(p);
if (this.#unrefed) core.unrefOpPromise(this.#recvPromise);
[buf, remoteAddr] = (await this.#recvPromise) as [
// ...
// UdpWrap
ref() {
if (this.#recvPromise) core.refOpPromise(this.#recvPromise);
this.#unrefed = false;
}
unref() {
if (this.#recvPromise) core.refOpPromise(this.#recvPromise);
this.#unrefed = true;
}
cli/tests/unit/net_test.ts
Outdated
|
||
Deno.test( | ||
{ permissions: { read: true, run: true, net: true } }, | ||
async function netUdpListenUnrefAndRef() { | ||
const p = execCode2(` |
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.
Please test the Node.js API instead of Deno.listenDatagram
. It can be written in cli/tests/unit_node/dgram_test.ts
Hope I don't miss anything, but there is no
|
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.
LGTM, thank you!
@littledivy @CosminPerRam it appears there's still a failing test that leaks ops. Can you please take a look at it? |
Unfortunately this beats me, @littledivy could you please take care of it (some afterwards explanations would be appreciated)? |
if (this.#recvPromise) core.refOpPromise(this.#recvPromise); | ||
this.#unrefed = true; |
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.
if (this.#recvPromise) core.refOpPromise(this.#recvPromise); | |
this.#unrefed = true; | |
if (this.#recvPromise) core.unrefOpPromise(this.#recvPromise); | |
this.#unrefed = true; |
udpSocket.on("message", (buffer, _rinfo) => { | ||
resolve(Uint8Array.from(buffer)); | ||
}); |
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.
udpSocket.on("message", (buffer, _rinfo) => { | |
resolve(Uint8Array.from(buffer)); | |
}); | |
let data; | |
udpSocket.on("message", (buffer, _rinfo) => { | |
data = Uint8Array.from(buffer); | |
udpSocket.close(); | |
}); | |
udpSocket.on("close", () => { | |
resolve(); | |
}); |
Socket needs to be closed.
@CosminPerRam I opened #21777 with a different implementation that fixes bugs in this approach. The major one being that the promises returned by |
Thanks, I will close this PR as that one superseeds this one. |
Pull request was closed
I attempted on adding ref/unref for UDP.
I do not expect this to be merged (as I do not think what I've done is exactly what is needed for it), but suggestions and directions would be gladly appreciated.
Related:
Error: Not implemented: udp.UDP.prototype.unref
Error: Not implemented: udp.UDP.prototype.unref #20138cargo test integration::js_unit_tests::net_test
yields OK.