Skip to content

fix(wasm): update workerData polyfill and usage - #2474

Merged
xusd320 merged 2 commits into
nextfrom
fix/worker-thread-id-polyfill
Dec 31, 2025
Merged

fix(wasm): update workerData polyfill and usage#2474
xusd320 merged 2 commits into
nextfrom
fix/worker-thread-id-polyfill

Conversation

@xusd320

@xusd320 xusd320 commented Dec 31, 2025

Copy link
Copy Markdown
Contributor

This pull request refactors how worker thread metadata is handled in the loader and polyfill code, standardizing the use of threadId instead of workerId and improving compatibility with Node.js worker threads APIs. The changes also introduce a more robust proxy-based implementation for accessing workerData in the polyfills, ensuring consistent access patterns across the codebase.

Worker thread metadata standardization and refactoring:

  • Replaced all uses of workerId with threadId in the LoaderRunnerMeta interface, worker initialization, and throughout the loader worker pool and worker files for consistency with Node.js conventions. [1] [2] [3] [4]
  • Updated the submodule reference for next.js, likely to pull in upstream changes or fixes.

Polyfill improvements and compatibility:

  • Refactored workerThreadsPolyfill.ts to export a proxy-based workerData object, ensuring dynamic property access and better emulation of Node.js worker threads. Also, added a threadId export based on the proxied workerData.
  • Updated all polyfills (fsPolyfill, fsPromisesPolyfill, nodePolyFills) to use the new workerData proxy for accessing worker metadata, replacing direct access to self.workerData. [1] [2] [3] [4]

These changes improve the maintainability and accuracy of the worker thread emulation, making the codebase more robust and consistent with Node.js APIs.

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @xusd320, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request refines the handling of WebAssembly (WASM) worker thread metadata and polyfills to align more closely with Node.js 'worker_threads' conventions. It standardizes the use of 'threadId' over 'workerId' and implements a more robust, proxy-based 'workerData' mechanism within the polyfills, ensuring consistent and accurate worker thread emulation across the codebase.

Highlights

  • Standardized Worker Metadata: Replaced 'workerId' with 'threadId' across worker-related interfaces and implementations (LoaderRunnerMeta, loaderWorkerPool, worker.ts) for consistency with Node.js 'worker_threads' API.
  • Enhanced workerData Polyfill: Introduced a Proxy-based 'workerData' in 'workerThreadsPolyfill.ts' to provide dynamic and robust access to worker metadata, improving emulation accuracy.
  • Unified Polyfill Access: Updated 'fsPolyfill.ts', 'fsPromisesPolyfill.ts', and 'nodePolyFills.ts' to utilize the new proxied 'workerData' from 'workerThreadsPolyfill.ts', centralizing metadata access.
  • Submodule Update: Updated the 'next.js' submodule reference to a newer commit.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request is a great improvement, refactoring the worker thread metadata handling to standardize on threadId and using a Proxy for workerData to better emulate Node.js worker threads. The changes are well-executed and improve the robustness and maintainability of the codebase. I've identified a couple of areas for improvement: a potential bug with a stale threadId value in the polyfill and a minor inconsistency in its usage. Addressing these will make the implementation even more solid.

);

export const isMainThread = false;
export const threadId = workerData?.threadId || 0;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The threadId is calculated only once when this module is imported. At that time, self.workerData is likely not yet available, causing threadId to be permanently initialized to 0. This is a potential bug as the value will be stale.

Since nodePolyFills.ts already defines a dynamic getter for threadId on the final polyfill object, this export is both buggy and redundant. I recommend removing it to prevent potential issues if this module is imported directly elsewhere.

Comment on lines +31 to +32
// @ts-ignore
return self.workerData?.workerId || 0;
return self.workerData?.threadId || 0;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

For consistency with the other changes in this PR that refactor access to workerData through the new proxy, this getter should also use workerThreads.workerData. This also has the benefit of removing the need for @ts-ignore and makes the code cleaner.

Suggested change
// @ts-ignore
return self.workerData?.workerId || 0;
return self.workerData?.threadId || 0;
return workerThreads.workerData?.threadId || 0;

@xusd320
xusd320 merged commit b023c5f into next Dec 31, 2025
21 checks passed
@xusd320
xusd320 deleted the fix/worker-thread-id-polyfill branch December 31, 2025 07:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant