Clear loaded modules to remove state between runs #1287
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
closes RaspberryPiFoundation/digital-editor-issues/issues/878
In [1] we changed pyodide to keep the current state rather than reloading on each run in order to speed up loading of large packages.
A consequence of this is that local files are also not reloaded. This means that class variables and other data can persist between runs.
To fix this, make sure we clear all local modules as well as global varibles. We need to import the sys module to do this (which will then be unloaded as a global variable in the same function).
An alternative to this approach that I considered is to create new instances of pyodide, but keep the state site-packages directory between runs which may be possible and would better mirror how python runs locally. I couldn't get this to work as the FS interface has limited options and,I think would have to manually copy files to a local variable and back again to do this.
[1] #1113