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

feat(core) support AbortSignal in Deno.listen #11569

Closed
wants to merge 6 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
21 changes: 21 additions & 0 deletions cli/tests/unit/net_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -598,3 +598,24 @@ unitTest(
listener.close();
},
);

unitTest(
{ perms: { net: true } },
async function netTcpListenCloseSignal(): void {
const ac = new AbortController();
const listener = Deno.listen({
hostname: "127.0.0.1",
port: 3500,
signal: ac.signal,
});
const p = listener.accept();
ac.abort();
await assertThrowsAsync(
async (): Promise<void> => {
await p;
},
Deno.errors.BadResource,
"Listener has been closed",
);
},
);
11 changes: 10 additions & 1 deletion extensions/net/01_net.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,16 @@
...options,
});

return new Listener(res.rid, res.localAddr);
const listener = new Listener(res.rid, res.localAddr);
if (options?.signal) {
// TODO(benjamingr) there is memory leak potential here if the signal outlives the
// server significantly it can retain it. Ideally Deno would have internal weak
// event listener like Node does *or* it would have an event for the server closing.
options?.signal?.addEventListener("abort", () => listener.close(), {
once: true,
});
}
Comment on lines +211 to +218

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (options?.signal) {
// TODO(benjamingr) there is memory leak potential here if the signal outlives the
// server significantly it can retain it. Ideally Deno would have internal weak
// event listener like Node does *or* it would have an event for the server closing.
options?.signal?.addEventListener("abort", () => listener.close(), {
once: true,
});
}
if (options?.signal) {
const weak = new WeakRef(listener);
options?.signal.addEventListener("abort", () => weak.deref()?.close(), {
once: true,
});
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That won't help - it still leaks the listener :) Happy to elaborate if that is unclear

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's true that function captures everything
if v8 is failing to optimize that without any invocations, we can move event callback creation outside scope

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@evanwashere even if the capture was only of the listener this is still a memory leak since the listener itself is captured.

Weak event listeners need to be baked into the event target implementation itself for this to not leak (here is an example how I did it in Node.js nodejs/node#36607 and I am happy to point to whatwg discussions).

That said - another (not bad) alternative is to expose a way to know when the listener is closed (and remove the handler then) but I didn't want to propose a change of the API of Deno.listen just for that :]

(This leak is small and not very problematic to be fair)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

listener is wrapped in weakref, so capture won't matter and problem will be fixed

{
  const listener = {...};
  if (signal) signal.add('event', createListenerAbortCallback(new WeakRef(listener)));
}

function createListenerAbortCallback(weakref) {
  return () => weakref.deref()?.close();
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, the fact it needs a separate fix is why I added a TODO for it :) The actual leak here is contrived, edge-casey and not that bad.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bit of a drive-by comment but can't this be fixed with a WeakMap?

const weakrefs = new WeakMap();

function listen({ hostname, ...options }) {
  // ...
  if (signal) {
    weakrefs.set(signal, new WeakRef(listener));
    signal.addEventListener("abort", handleAbort);
  }
  // ...
}

function handleAbort(e) {
  weakrefs.get(e.target)?.deref()?.close();
}

(Use of primordials omitted for brevity.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bnoordhuis that would still leak the handleAbort function reference.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's not a problem though, is it? The solutions outlined earlier leak because they create new closures every time, whereas the handleAbort() closure is created only once (and at the top-level so it's going to stay around forever anyway.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bnoordhuis is the handleAbort reference still going to be in the listeners array?

The "interesting" case here is a long-lived signal (imagine a signal owned by a controller that gets aborted on SIGINT for graceful shutdown) that gets registered often (imagine in this case you start a server on every request and do something).

Since this is Deno.listen it's not very likely - but that's the case we guarded against in Node

return listener;
}

async function connect(options) {
Expand Down
10 changes: 9 additions & 1 deletion extensions/net/02_tls.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
hostname = "0.0.0.0",
transport = "tcp",
alpnProtocols,
signal,
}) {
const res = opListenTls({
port,
Expand All @@ -61,7 +62,14 @@
transport,
alpnProtocols,
});
return new TLSListener(res.rid, res.localAddr);
const listener = new TLSListener(res.rid, res.localAddr);
if (signal) {
// TODO(benjamingr) there is memory leak potential here. See comment in 01_net.js
signal.addEventListener("abort", () => listener.close(), {
once: true,
});
}
benjamingr marked this conversation as resolved.
Show resolved Hide resolved
return listener;
}

async function startTls(
Expand Down
2 changes: 2 additions & 0 deletions extensions/net/lib.deno_net.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ declare namespace Deno {
/** A literal IP address or host name that can be resolved to an IP address.
* If not specified, defaults to `0.0.0.0`. */
hostname?: string;
// Allows closing the server by aborting the associated AbortController
signal?: AbortSignal;
}

/** Listen announces on the local transport address.
Expand Down