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

Make sure we don't import pyodide into memory snapshot. #3499

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

hoodmane
Copy link
Contributor

@hoodmane hoodmane commented Feb 7, 2025

Importing pyodide before finalizeBootstrap() is called causes mayhem.

@hoodmane hoodmane requested review from a team as code owners February 7, 2025 21:54
@hoodmane hoodmane requested review from danlapid and mar-cf February 7, 2025 21:54
@hoodmane hoodmane force-pushed the hoodmane/dont-snapshot-pyodide branch from ecd7f65 to 761ce3d Compare February 7, 2025 21:58
@@ -0,0 +1,2 @@
async def test():
Copy link
Collaborator

Choose a reason for hiding this comment

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

How does this test the issue?

Copy link
Contributor Author

@hoodmane hoodmane Feb 7, 2025

Choose a reason for hiding this comment

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

To trigger the bug we need to do three things:

  1. import pyodide at top level
  2. ensure that there is some package requirement
  3. make test() async

The numpy import here is unnecessary, but if someone removed (name = "numpy", pythonRequirement = "") from the wd-test file it would no longer reproduce the problem.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh shoot ruff removed the imports. I keep having this problem.

Copy link
Collaborator

Choose a reason for hiding this comment

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

But where is the pyodide import? 😅

Copy link
Collaborator

Choose a reason for hiding this comment

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

Oh shoot ruff removed the imports. I keep having this problem.

Hahaha that explains it
There’s probably some comments that disable ruff on a file right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I put a whole explanation in now. Also #3415 is such a huge improvement to our test suite I am so happy we merged it.

const moduleSet = new Set(importedModules);
// Importing pyodide before `finalizeBootstrap()` is called causes mayhem
// Make sure we don't do that!
// TODO: in Pyodide 0.27 we can call finalizeBootstrap() before taking the snapshot. Doing so will
Copy link
Collaborator

Choose a reason for hiding this comment

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

Perhaps we need a way to check pyodide version when generating the bundles so we could make things like this conditional

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Indeed we do.

@danlapid
Copy link
Collaborator

danlapid commented Feb 7, 2025

LGTM, only problem is I don’t understand the test

@hoodmane hoodmane force-pushed the hoodmane/dont-snapshot-pyodide branch from 761ce3d to cfa585d Compare February 7, 2025 22:07
Copy link
Collaborator

@dom96 dom96 left a comment

Choose a reason for hiding this comment

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

Are there any other imports that might cause issues that should be filtered out? Should we add a test for other Pyodide modules that can be imported?

// Make sure we don't do that!
// TODO: in Pyodide 0.27 we can call finalizeBootstrap() before taking the snapshot. Doing so will
// remove the need for this.
moduleSet.delete('pyodide');
Copy link
Collaborator

Choose a reason for hiding this comment

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

We already have some filtering logic in

if (!locals.contains(moduleFilename) && pkgImport != "js" &&

Seems to only be filtering out js directly right now, but best to do the filtering in one place.

const moduleSet = new Set(importedModules);
// Importing pyodide before `finalizeBootstrap()` is called causes mayhem
// Make sure we don't do that!
// TODO: in Pyodide 0.27 we can call finalizeBootstrap() before taking the snapshot. Doing so will
Copy link
Collaborator

Choose a reason for hiding this comment

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

Since this is only necessary for 0.26, should we gate this behind a version check?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's necessary for both until we move finalizeBootstrap().

@hoodmane
Copy link
Contributor Author

hoodmane commented Feb 8, 2025

There's nothing else that is publicly allowed to import. There are a few implementation detail imports that could cause trouble but I think that'd be a won't-fix since people really shouldn't touch that stuff.

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.

3 participants