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

feat: allow custom v8 snapshots to be used in the main process and the default snapshot in the renderer process #35266

Merged
merged 18 commits into from Sep 15, 2022

Conversation

ryanthemanuel
Copy link
Contributor

@ryanthemanuel ryanthemanuel commented Aug 8, 2022

Description of Change

We are using electron-mksnapshot to generate a custom v8 snapshot for our application that is desired to run only in the main/browser process, not the renderer process. This PR will utilize a new fuse (electron/fuses#8) called LoadV8SnapshotFromCustomPath that tells the main/browser process to look for the v8 snapshot file at custom_v8_context_snapshot.bin. Any other process will use the same path as is used today.

Consumers will then be able to build/package their binary as follows:

flipFuses(
  path.join(options.dist, 'Cypress.app', 'Contents', 'MacOS', 'Cypress'), // Returns the path to the electron binary
  {
    version: FuseVersion.V1,
    [FuseV1Options.LoadBrowserProcessSpecificV8Snapshot]: true,
  },
)

Fixes #35170

Checklist

Release Notes

Notes: Added LoadBrowserProcessSpecificV8Snapshot as a new fuse that will let the main/browser process load its v8 snapshot from a file at browser_v8_context_snapshot.bin. Any other process will use the same path as is used today.

@welcome
Copy link

welcome bot commented Aug 8, 2022

💖 Thanks for opening this pull request! 💖

We use semantic commit messages to streamline the release process. Before your pull request can be merged, you should update your pull request title to start with a semantic prefix.

Examples of commit messages with semantic prefixes:

  • fix: don't overwrite prevent_default if default wasn't prevented
  • feat: add app.isPackaged() method
  • docs: app.isDefaultProtocolClient is now available on Linux

Things that will help get your PR across the finish line:

  • Follow the JavaScript, C++, and Python coding style.
  • Run npm run lint locally to catch formatting errors earlier.
  • Document any user-facing changes you've made following the documentation styleguide.
  • Include tests when adding/changing behavior.
  • Include screenshots and animated GIFs whenever possible.

We get a lot of pull requests on this repo, so please be patient and we will get back to you as soon as we can.

@electron-cation electron-cation bot added the new-pr 🌱 PR opened in the last 24 hours label Aug 8, 2022
@ryanthemanuel ryanthemanuel changed the title Updates to allow for using a custom v8 snapshot file name feat: allow custom v8 snapshots to be used in the main process and the default snapshot in the browser process Aug 8, 2022
@ryanthemanuel
Copy link
Contributor Author

Note for reviewers: Currently, this just works on Macs, but provides an idea of what I'm trying to accomplish. It does not work on Linux because the renderer process is created from a zygote process that presumably is created prior to when the BrowserWindow arguments are specified.

There are a couple of questions I have for this PR before it is ready to go:

  1. What approach would be recommended to handle linux? I have a version of code locally that works but it involves disabling the zygote process when the custom-v8-snapshot-file-name argument is present but I'm wondering if there's a better way.
  2. Would it be better to make this a first class parameter under webPreferences rather than rely on additionalArguments. This may affect any docs that might be written so I haven't written up any docs just yet.
  3. Being new to the code base, I'm curious if there's a good place for a test for this.

@ryanthemanuel ryanthemanuel marked this pull request as ready for review August 8, 2022 21:24
@ryanthemanuel ryanthemanuel requested review from a team as code owners August 8, 2022 21:24
@jkleinsc jkleinsc added the semver/minor backwards-compatible functionality label Aug 9, 2022
@codebytere
Copy link
Member

@ryanthemanuel could you speak a little more to your use case here? what would you be trying to accomplish with per-process snapshots?

@ryanthemanuel
Copy link
Contributor Author

@codebytere, we are using a custom snapshot in our main process to preload node dependencies (similar to what atom does with electron-link). However, we have nodeIntegration disabled in our renderer process and do not want those preloaded dependencies to be available in that context. Thus, we would either want the default snapshot (or no v8 snapshot) to be used in the renderer process.

@brian-mann
Copy link

@codebytere does Ryan’s update provide you with more context regarding our use case?

the original issue we opened with more description and context is here: #35170

@MarshallOfSound
Copy link
Member

Based on the use case you described I don't think this PR is the right way forward. Allowing custom snapshots via a command line flag in the renderer process is a can of worms and will not function correctly (As I think you've already identified in a few scenarios)

  • Sandboxing
  • Site isolation
  • Delayed renderer creation
  • Etc.

additionalArguments is a flawed property that I'm surprised survived the renderer-process-reuse refactor 🤔

I think the correct approach here is to introduce a new fuse that specifically can change which snapshot is loaded in the browser process. The renderer must always use the default snapshot (or at least a modified version of that snapshot) because it contains core blink/v8 builtins. Adding a fuse that when enabled loads a different v8 snapshot in the browser process seems like a minimal lift and slightly more explainable than a command line argument to a web contents (which fundamentally doesn't align with our modern process model)

@ryanthemanuel
Copy link
Contributor Author

ryanthemanuel commented Aug 22, 2022

@MarshallOfSound, the fuse was exactly what I was looking for. I originally had explored going down the route of the custom v8 snapshot path being for the browser process, but I couldn't figure out how to make it work without passing in a parameter on launch. I hadn't heard of fuses before but a new one seems like it will solve that exact problem.

I tried it out on mac and linux and it works. The code I used is now attached to this PR; however, I think there are still some discussion points with it before I think it's fully ready for review:

  • Right now I'm calling the fuse LoadV8SnapshotFromCustomPath. I was thinking maybe LoadBrowserProcessV8SnapshotFromCustomPath to be even more explicit. Thoughts?
  • The custom path for the v8 snapshot will be at custom_v8_context_snapshot.bin not including any of the architecture info in the filename. Does that seem reasonable?
  • Do we still want to communicate the custom path via a command line parameter? I have it working that way currently as it was an easy way to test given what I had already done, but wasn't sure if there was a better option. Maybe using the electron_main_delegate as a method to communicate the path?

@ryanthemanuel
Copy link
Contributor Author

@MarshallOfSound I went ahead and refactored to use the delegate instead of a command line parameter to communicate the custom v8 snapshot path. It seemed to simplify things fairly well. Does this seem like a reasonable approach?

@ryanthemanuel
Copy link
Contributor Author

Also, I created the corresponding PR to the fuses repo

@ryanthemanuel
Copy link
Contributor Author

Hi @MarshallOfSound and @codebytere, is there any update on what needs to happen to get this PR moving? Is there any additional information you need from me? Does my current approach seem reasonable?

@ckerr ckerr merged commit f25c87d into electron:main Sep 15, 2022
@welcome
Copy link

welcome bot commented Sep 15, 2022

Congrats on merging your first pull request! 🎉🎉🎉

@release-clerk
Copy link

release-clerk bot commented Sep 15, 2022

Release Notes Persisted

Added LoadBrowserProcessSpecificV8Snapshot as a new fuse that will let the main/browser process load its v8 snapshot from a file at browser_v8_context_snapshot.bin. Any other process will use the same path as is used today.

trop bot pushed a commit that referenced this pull request Sep 15, 2022
…e default snapshot in the renderer process (#35266)

* Updates to allow for using a custom v8 snapshot file name

* Allow using a custom v8 snapshot file name

* Fix up patch due to merge

* Use fuse to set up custom v8 snapshot file in browser process

* Refactor to use delegate instead of command line parameter

* Refactoring

* Update due to merge

* PR comments

* Rename patch

* Rename patch
trop bot pushed a commit that referenced this pull request Sep 15, 2022
…e default snapshot in the renderer process (#35266)

* Updates to allow for using a custom v8 snapshot file name

* Allow using a custom v8 snapshot file name

* Fix up patch due to merge

* Use fuse to set up custom v8 snapshot file in browser process

* Refactor to use delegate instead of command line parameter

* Refactoring

* Update due to merge

* PR comments

* Rename patch

* Rename patch
@trop
Copy link
Contributor

trop bot commented Sep 15, 2022

I have automatically backported this PR to "20-x-y", please check out #35694

@ckerr
Copy link
Member

ckerr commented Sep 15, 2022

@MarshallOfSound and @ckerr does the no-backport mean that this change will make it to 21.0 or 22.0? We'd really like to consume this as soon as we can and I know 22.0 won't land for another 10 weeks.

I think it was to make the bot happy 🙂

@trop
Copy link
Contributor

trop bot commented Sep 15, 2022

I have automatically backported this PR to "21-x-y", please check out #35695

@trop trop bot added in-flight/21-x-y and removed target/20-x-y target/21-x-y PR should also be added to the "21-x-y" branch. labels Sep 15, 2022
jkleinsc pushed a commit that referenced this pull request Sep 19, 2022
…e default snapshot in the renderer process (#35266)

* Updates to allow for using a custom v8 snapshot file name

* Allow using a custom v8 snapshot file name

* Fix up patch due to merge

* Use fuse to set up custom v8 snapshot file in browser process

* Refactor to use delegate instead of command line parameter

* Refactoring

* Update due to merge

* PR comments

* Rename patch

* Rename patch

chore: update patches
VerteDinde pushed a commit that referenced this pull request Sep 22, 2022
…e default snapshot in the renderer process (#35694)

feat: allow custom v8 snapshots to be used in the main process and the default snapshot in the renderer process (#35266)

* Updates to allow for using a custom v8 snapshot file name

* Allow using a custom v8 snapshot file name

* Fix up patch due to merge

* Use fuse to set up custom v8 snapshot file in browser process

* Refactor to use delegate instead of command line parameter

* Refactoring

* Update due to merge

* PR comments

* Rename patch

* Rename patch

chore: update patches

Co-authored-by: Ryan Manuel <ryanm@cypress.io>
VerteDinde pushed a commit that referenced this pull request Sep 23, 2022
…e default snapshot in the renderer process (#35266)

* Updates to allow for using a custom v8 snapshot file name

* Allow using a custom v8 snapshot file name

* Fix up patch due to merge

* Use fuse to set up custom v8 snapshot file in browser process

* Refactor to use delegate instead of command line parameter

* Refactoring

* Update due to merge

* PR comments

* Rename patch

* Rename patch
@trop trop bot added the merged/21-x-y PR was merged to the "21-x-y" branch. label Sep 24, 2022
VerteDinde pushed a commit that referenced this pull request Sep 24, 2022
…e default snapshot in the renderer process (#35695)

* feat: allow custom v8 snapshots to be used in the main process and the default snapshot in the renderer process (#35266)

* Updates to allow for using a custom v8 snapshot file name

* Allow using a custom v8 snapshot file name

* Fix up patch due to merge

* Use fuse to set up custom v8 snapshot file in browser process

* Refactor to use delegate instead of command line parameter

* Refactoring

* Update due to merge

* PR comments

* Rename patch

* Rename patch

* chore: update patches

Co-authored-by: Ryan Manuel <ryanm@cypress.io>
Co-authored-by: PatchUp <73610968+patchup[bot]@users.noreply.github.com>
@trop trop bot removed the in-flight/21-x-y label Sep 24, 2022
khalwa pushed a commit to solarwindscloud/electron that referenced this pull request Feb 22, 2023
…e default snapshot in the renderer process (electron#35266)

* Updates to allow for using a custom v8 snapshot file name

* Allow using a custom v8 snapshot file name

* Fix up patch due to merge

* Use fuse to set up custom v8 snapshot file in browser process

* Refactor to use delegate instead of command line parameter

* Refactoring

* Update due to merge

* PR comments

* Rename patch

* Rename patch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-review/requested 🗳 backport/requested 🗳 merged/21-x-y PR was merged to the "21-x-y" branch. semver/minor backwards-compatible functionality
Projects
None yet
6 participants