Skip to content

Globals have their own microtask queue that is separate from script thread's queue #20908

Closed

Description

@jdm

GlobalScope has a microtask queue that is used to support promises in worker runtimes. This is used from places like AutoEntryScript, which directly invokes GlobalScope::perform_a_microtask_checkpoint. Meanwhile, ScriptThread has its own perform_a_microtask_checkpoint which ignores the microtasks of any globals associated with that thread and uses its own internal microtask queue, which is manipulated by code like MutationObserver. We need to move the queue out of GlobalScope and into worker-specific subtypes, and ensure that GlobalScope delegates to ScriptThread or the subtypes as necessary.

Activity

  1. added
    A-content/script
    on Jun 3, 2018
  2. gterzian commented on Jun 6, 2019

    @gterzian
    Member

    As part of that refactoring, we could maybe add a perform_a_microtask_checkpoint method to

    pub trait WorkerEventLoopMethods {

    Or it could be a method on WorkerGlobalScope, which by the way is owned by each worker sub-type, so the underlying microtask queue could be owned by either.

  3. added
    I-refactor
    on Jun 6, 2019
  4. jdm commented on Jun 6, 2019

    @jdm
    MemberAuthor

    Huh, I wonder if this is related to the surprising behaviour that @KiChjang saw in #21456.

  5. gterzian commented on Jun 6, 2019

    @gterzian
    Member

    Huh, I wonder if this is related to the surprising behaviour that @KiChjang saw in #21456.

    Since the parser calls the microtask checkpoints of globals, and there can be several globals in a script-thread, yes I think it could lead to problems. Also the spec mentions "Each event loop has a microtask queue", so we defenitely only want one per script-thread(and per worker thread).

  6. KiChjang commented on Jul 5, 2019

    @KiChjang
    Contributor

    This sounds like something I should look into.

  7. self-assigned this
    on Jul 5, 2019
  8. KiChjang commented on Jul 5, 2019

    @KiChjang
    Contributor

    @jdm I'm actually quite puzzled when you say that ScriptThread and GlobalScope use their own internal microtask queue, because the queue is currently stored as an Rc<MicrotaskQueue> everywhere and they are essentially shared between the script thread and the global scope.

  9. jdm commented on Jul 6, 2019

    @jdm
    MemberAuthor

    I agree. I think I must have misread the code, since f903474 is quite a bit older than this issue and adds pretty clear documentation.

    However, reading through the code, I think I do see a problem - the checkpoint code relies on the provided set of targets to decide whether to run promise job microtasks or silently discard them. When ScriptThread::perform_a_microtask is executed, the full set of same-origin globals are available. However, if the Window global's microtask check point is invoked instead, all promise reactions will end up using that global as the target, regardless of the pipeline id associated with that job.

    To avoid this, I think we should make GlobalScope's microtask checkpoint API delegate to the ScriptThread one if we are a Window global.

  10. akshatagarwl commented on Mar 26, 2020

    @akshatagarwl
    Contributor

    @jdm Can I work on this?

  11. jdm commented on Mar 26, 2020

    @jdm
    MemberAuthor

    Of course!

  12. removed their assignment
    on Mar 26, 2020
  13. akshatagarwl commented on Apr 15, 2020

    @akshatagarwl
    Contributor

    @jdm What do mean when you say if we are a window global and how would I check that?

  14. jdm commented on Apr 15, 2020

    @jdm
    MemberAuthor

    @humancalico This is an example of some code that casts a GlobalScope into a Window. You can also use some_global.is::<Window>() for a simple yes/no if that is all that's necessary.

  15. jdm commented on Apr 15, 2020

    @jdm
    MemberAuthor

    More specifically, GlobalScope is a base class of various kinds of globals - Window, WorkerGlobalScope, WorkletGlobalScope, and others. These correspond to the global object of various JS environments - page window objects, dedicated workers, and worklets.

  16. self-assigned this
    on Oct 31, 2025
  17. Gae24 commented on Nov 1, 2025

    @Gae24
    Contributor

    Should MicrotaskQueue still be behind an Rc in the WorkerGlobalScope case?

  18. jdm commented on Nov 1, 2025

    @jdm
    MemberAuthor

    If it doesn't need to use an Rc then we should avoid it. I don't know the answer without doing some digging, though.

  19. Gae24 commented on Nov 1, 2025

    @Gae24
    Contributor

    It shouldn't since a worker agent has a single WorkerGlobalScope, while multiple windows can be owned by the same agent.
    Actually, I should check if a new microtask queue should be created instead of cloning the one that gets passed down.

  20. added a commit that references this issue on Nov 3, 2025
    44a0570
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

A-content/scriptE-candidate-for-mentoringI-refactor

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions