Skip to content

Service worker code only supports a single content process #15217

Closed

Description

@Ms2ger

When a new content process is spawned, we send a SWManagerMsg::OwnSender message to the constellation, which does

                // store service worker manager for communicating with it.
                self.swmanager_chan = Some(sw_sender);

and any previous value of that field is gone.

Activity

  1. added
    A-constellation
    A-content/dom
    on Jan 25, 2017
  2. gterzian commented on Mar 29, 2020

    @gterzian
    Member

    Ok I'd like to fix this one, and I think this actually is a chance to change the infra for service worker a bit, and do something we haven't done yet: run something else than a script-thread in it's own process.

    The issue mentioned above is essentially due to running the ServiceWorkerManager into the script-process(in multi-process mode), and having the constellation only have the ability to have one.

    So we could enable the constellation to have senders to more than one manager, however, the real solution I think is to not actually run the ServiceWorkerManager in an EventLoop at all.

    Instead, I think we should try to run each ServiceWorkerManager in their own process, and have the constellation own a map of ImmutableOrigin -> ServiceWorkerManager.

    The ServiceWorkerManager would then be similar to an EventLoop, essentially a process running script code, however it would be running a service worker event-loop. Since service workers are supposed to be entirely event-driven, I think we can get away with running one thread per ServiceWorkerManager, which would run the event-loop for all service worker global scopes for a given origin.

    Then, similar to what has already been done with WebGPU, the constellation would create a ServiceWorkerManager for a given origin when an EventLoop would first start using a ServiceWorker.

    I think this is also important as a step to be able to fully implement service workers, since they are supposed to be shared on a per-origin basis across the entire browser(currently they are scoped to an EventLoop, meaning that there can be multiple ones for one origin when they are not part of the same browsing-context-group), and they are also supposed to be able to run when there is no window open at all, so by running them in their own process outside of an EventLoop, we both ensure there is only one per origin across the UA, and we also open the door to running them even when there are no EventLoop running.

    @jdm what do you think of that proposal?

    cc @CYBAI

  3. CYBAI commented on Mar 31, 2020

    @CYBAI
    Member

    since they are supposed to be shared on a per-origin basis across the entire browser (currently they are scoped to an EventLoop, meaning that there can be multiple ones for one origin when they are not part of the same browsing-context-group)

    I haven't dig into Servo's SW infra though, I recently studied a little bit about SW and yes, I agree with on this!

  4. gterzian commented on Mar 31, 2020

    @gterzian
    Member

    Ok in the light of #7458 (comment) I will try to fix this without introducing more processes...

  5. gterzian commented on Apr 1, 2020

    @gterzian
    Member

    Ok as a bit of a recap of #7458 (comment)

    1. In order to fix this issue, I would like to run the "service worker manager" in it's own process, and have the constellation own a map of "Origin" -> "Sender to a worker manager process".
    2. That way, we don't start the ServiceWorkerManager in each content-process, instead, when a content-process starts using a service worker, it would send a message to the constellation, which could then either spin-up a manager for that origin, or simply keep using a manager that is already running for that origin.
    3. After that, everytime a content-process needs to do an operation with the service worker, the message goes via the constellation and is forwarded to the manager running in its own process(this is what is currently done anyway, except that the manager runs in an EventLoop).

    That way, at the cost of an additional process, we get a clearly isolated(from a business logic perspective) ServiceWorkerManager running all worker scope for a given origin, acting a bit like an EventLoop does for a script-thread.

    I tried fixing it without introducing the new process, in #26073, and as noted in #7458 (comment), I think going that way would open a can of worms. It means we need to keep an EventLoop alive when it's running a worker manager that is(or could be) used by other EventLoops, and all sorts of other business logic that would go away if we just put the worker manager in it's own construct at the constellation level.

    Also, with this structure, there is no logical difference between single- and multi-process mode. Essentially, the ServiceWorkerManager is an EventLoop-like construct that either runs in it's own process or not. The current problem in this issue is an example of how something can suddenly not work in a given mode, hence currently requires the code to be aware of the mode.

    Finally, the spec puts a service worker in it's own agent-cluster, which doesn't necessarily mean it's own process, however it is the most straightforward way to actually do it.

    @Manishearth @asajeffrey @jdm Since this is quite a change, please let me know what you think. I think this equally applies to fixing Service workers, as well as to a potential implementation of Shared workers(Actually both could potentially run in one process per origin, which I think could be an optimization making more sense then trying to co-locate them in a script-thread process).

  6. jdm commented on Apr 2, 2020

    @jdm
    Member

    I'm in favour!

  7. added 13 commits that reference this issue on Apr 4, 2020
    36520f5
    f34d913
    cb85e6d
    fd5b4cd
    aba4cce
    039a7f4
    a9ae047
    9502906
    acb9e6f
    a3a0f2a
    1f1f488
    ade9fc9
    ae49473
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-constellationA-content/dom

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions