镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

feature request: add calling thread ID to loader resolve() context #49472

Description

@cjihrig

What is the problem this feature will solve?

Some loaders, such as a mocking loader, should be aware of the thread initiating the operation. For example, it is common for ESM mocking to pass a list of export names to the loader thread, and later resolve those exports on an application thread. If a module is mocked from one application thread, and another thread tries to import the same module, it will be unaware of the export values. The export values themselves cannot be passed between threads because some values, such as function, do not serialize.

What is the feature you are proposing to solve the problem?

The resolve() loader hook already has a context object, which includes the parentURL. I am requesting that the thread ID also be included. If the thread ID were in the context, the resolve() hook could identify the proper mock (or real) implementation to use, depending on the thread.

What alternatives have you considered?

No response

Activity

  1. added
    loadersIssues and PRs related to ES module loaders.
    on Sep 3, 2023
  2. MrJithil commented on Sep 27, 2023

    @MrJithil
    Member

    Is this something we can start working? Or should we wait for enough opinions?

  3. bnoordhuis commented on Sep 27, 2023

    @bnoordhuis
    Member

    #30061 (comment) may be relevant.

  4. cjihrig commented on Sep 27, 2023

    @cjihrig
    ContributorAuthor

    FWIW, I was referring to using worker.threadId here.

    I would avoid starting any work until you get some input from the loaders team in case they push back for some reason.

  5. targos commented on Oct 2, 2023

    @targos
    Member

    I feel like there's a misunderstanding here (or I don't really understand the use case). If your application has one main thread and two worker threads, then there are three separate loader threads. With this feature, each loader would receive a different thread ID, but always the same one.

  6. cjihrig commented on Oct 2, 2023

    @cjihrig
    ContributorAuthor

    If your application has one main thread and two worker threads, then there are three separate loader threads.

    Maybe there is a misunderstanding then. I was under the impression that once the loaders were moved off thread, there was a single loader thread for the entire application. If that is not the current state, I thought it was at least the goal state based on #47747 (comment).

  7. cjihrig commented on Nov 13, 2023

    @cjihrig
    ContributorAuthor

    Another data point from Slack:

    when hooks are registered, node spawns a thread to run them in. when user code creates a thread, node spawns another thread for running hooks; there should just be one hooks thread that handles however many application threads there are

    If we will eventually end up with a single hook thread, then it seems like it would be very useful to have the calling threadId for use cases like mocking.

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

    feature requestIssues requesting new Node.js features.loadersIssues and PRs related to ES module loaders.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions