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

Event for worker termination #5869

Open
surma opened this issue Aug 27, 2020 · 8 comments
Open

Event for worker termination #5869

surma opened this issue Aug 27, 2020 · 8 comments
Labels
addition/proposal New features or enhancements needs implementer interest Moving the issue forward requires implementers to express interest topic: workers

Comments

@surma
Copy link
Contributor

surma commented Aug 27, 2020

The AbstractWorker mixin provides an onerror prop to get notified when the worker throws. However, graceful or forced termination is not detectable right now.

I have found a bunch of situations where I’d like to be notified when a worker is being terminated. In my head, all of these are desired sources for this notification:

  • DedicatedWorkerGlobalScope.close()
  • SharedWorkerGlobalScope.close()
  • Worker.terminate()
  • graceful termination (i.e. script ends with no listeners active)
  • exceptions

Proposal: Expose an onterminate prop (and/or terminate event) to AbstractWorker.

@annevk annevk added addition/proposal New features or enhancements needs implementer interest Moving the issue forward requires implementers to express interest topic: workers labels Aug 27, 2020
@domenic
Copy link
Member

domenic commented Aug 27, 2020

I could swear we already had an issue for this, but the closest I can find is #1766 and #1885.

@annevk
Copy link
Member

annevk commented Aug 27, 2020

#1607, #5323, #5362, and #5363 are also somewhat related.

@yutakahirano
Copy link
Member

@hiroshige-g may be interested.

@hiroshige-g
Copy link
Contributor

My interest is test-only (to write a couple of worker termination WPTs).
cc/ @nhiroki for implementation perspective.

@lucacasonato
Copy link
Member

There is interest from Deno users on this topic. Node also implements a similar close event for worker_threads.

@nhiroki
Copy link
Contributor

nhiroki commented Apr 21, 2022

I'm supportive of this proposal if there are valid use cases in real applications. This will also help us to write more solid WPTs as @hiroshige-g mentioned.

I'm curious how this event should behave on close of the owner context, that is, unload of the parent document or termination of the parent worker. Do we want to guarantee that the event is always fired before the context close? IMHO I think we shouldn't guarantee that as it will degrade navigation performance. On the other hand, this will impair the reliability of the event and may not satisfy some of the use cases.

@andreubotella
Copy link
Member

andreubotella commented Apr 21, 2022

I'm curious how this event should behave on close of the owner context, that is, unload of the parent document or termination of the parent worker. Do we want to guarantee that the event is always fired before the context close? IMHO I think we shouldn't guarantee that as it will degrade navigation performance. On the other hand, this will impair the reliability of the event and may not satisfy some of the use cases.

A worker doesn't survive for long if its parent document has closed; see the "closing orphan workers" step in the worker event loop. And while the behavior of nested dedicated workers when their dedicated worker parent dies is not very clear in the spec, implementations also don't keep the nested worker alive for long (see #7840). So it doesn't seem like this is worth worrying about too much.

@jjkola-eskosystems
Copy link

@andreubotella What about shared workers? There could be several windows referencing same worker.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
addition/proposal New features or enhancements needs implementer interest Moving the issue forward requires implementers to express interest topic: workers
Development

No branches or pull requests

9 participants