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

Conversation

benjamingr
Copy link
Contributor

Refs #10181

This continues the work in #10943 and #11568 adds AbortSignal support to Deno.listen (and since it uses the same infrastructure writeTextFile)

There shouldn't be a DOMException here for AbortError looking at the guidance at WHATWG (the guidance is about the signature and errors of methods returning promises which isn't really the case here). I opted to do the same thing we did for Node.js

In all likelihood there should be weak event listeners (internal) in Deno core but that's a bigger refactor and honestly the event_target implementation is kind of a mess.

Comment on lines +211 to +218
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,
});
}

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

extensions/net/02_tls.js Show resolved Hide resolved
@bartlomieju bartlomieju added this to the 1.15.0 milestone Sep 13, 2021
@lucacasonato lucacasonato removed this from the 1.15.0 milestone Oct 11, 2021
@CLAassistant
Copy link

CLAassistant commented Oct 15, 2021

CLA assistant check
All committers have signed the CLA.

@stale
Copy link

stale bot commented Dec 14, 2021

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed in 7 days if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Dec 14, 2021
@stale stale bot closed this Dec 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants