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

src: ensure primordials are initialized exactly once #57519

Merged
merged 1 commit into from
Mar 21, 2025

Conversation

legendecas
Copy link
Member

@legendecas legendecas commented Mar 17, 2025

Right now InitializePrimordials is invoked twice on a context created via node::NewContext:

  1. InitializeMainContextForSnapshot invokes InitializePrimordials.
  2. InitializePrimordials invokes GetPerContextExports unconditionally.
    1. GetPerContextExports invokes InitializePrimordials when node:per_context_binding_exports private was not set.
      1. InitializePrimordials invokes GetPerContextExports and initialize primordials for the first time.
    2. Initialize primordials for the second time.

GetPerContextExports will be invoked during node_mksnapshot by initializing the principal realm so it is safe to remove invocation of InitializePrimordials in InitializeMainContextForSnapshot.

This patch ensures that InitializePrimordials is only invoked in GetPerContextExports and checks that primordials on the private per-context exports object are not initialized before.

@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. needs-ci PRs that need a full CI run. labels Mar 17, 2025
@legendecas legendecas added the request-ci Add this label to start a Jenkins CI on a PR. label Mar 17, 2025
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Mar 17, 2025
@nodejs-github-bot
Copy link
Collaborator

Copy link

codecov bot commented Mar 17, 2025

Codecov Report

Attention: Patch coverage is 25.00000% with 3 lines in your changes missing coverage. Please review.

Project coverage is 90.20%. Comparing base (38390e5) to head (187fbdb).
Report is 30 commits behind head on main.

Files with missing lines Patch % Lines
src/api/environment.cc 25.00% 1 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #57519      +/-   ##
==========================================
- Coverage   90.22%   90.20%   -0.02%     
==========================================
  Files         629      629              
  Lines      184845   184847       +2     
  Branches    36205    36209       +4     
==========================================
- Hits       166784   166749      -35     
- Misses      11016    11055      +39     
+ Partials     7045     7043       -2     
Files with missing lines Coverage Δ
src/api/environment.cc 76.00% <25.00%> (+0.28%) ⬆️

... and 37 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@atlowChemi
Copy link
Member

Is there anyway to benchmark this?
Can we add a test that InitializePrimordials is called only once?

@legendecas
Copy link
Member Author

This CHECK https://github.com/nodejs/node/pull/57519/files#diff-5f2fc380364ef43210af4a28c08f9a0c7ec5f85e3ec1f5c1df5e0204eb9dc721R777 ensures that primordials are initialized only once for a given context.

This does not run at runtime. Primordials are initialized at node_mksnapshot and snapshotted in the startup snapshot. I don't think it is necessary to run benchmark for build time as it is trivially small enough to the total build duration.

@nodejs-github-bot
Copy link
Collaborator

@nodejs-github-bot
Copy link
Collaborator

@nodejs-github-bot
Copy link
Collaborator

@nodejs-github-bot
Copy link
Collaborator

@jazelly jazelly added author ready PRs that have at least one approval, no pending requests for changes, and a CI started. and removed needs-ci PRs that need a full CI run. labels Mar 20, 2025
@legendecas
Copy link
Member Author

@nodejs/cpp-reviewers would you mind taking a look at this? Thank you!

@legendecas legendecas added the commit-queue Add this label to land a pull request using GitHub Actions. label Mar 21, 2025
@nodejs-github-bot nodejs-github-bot removed the commit-queue Add this label to land a pull request using GitHub Actions. label Mar 21, 2025
@nodejs-github-bot nodejs-github-bot merged commit 8b2098f into nodejs:main Mar 21, 2025
62 checks passed
@nodejs-github-bot
Copy link
Collaborator

Landed in 8b2098f

@legendecas legendecas deleted the primordials branch March 21, 2025 17:19
aduh95 pushed a commit that referenced this pull request Mar 23, 2025
PR-URL: #57519
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
author ready PRs that have at least one approval, no pending requests for changes, and a CI started. c++ Issues and PRs that require attention from people who are familiar with C++.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants