From 8324a660efa00f9d6eac879fde50498971c05d8e Mon Sep 17 00:00:00 2001 From: Hood Chatham Date: Fri, 6 Sep 2024 15:49:18 +0200 Subject: [PATCH 1/2] FIX SHOULD_GATE_ENTROPY condition We can't turn it off if IS_TRACING because it needs to have the same setting when creating a snapshot as when using it. --- src/pyodide/internal/topLevelEntropy/lib.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/pyodide/internal/topLevelEntropy/lib.ts b/src/pyodide/internal/topLevelEntropy/lib.ts index 9c45f6a4450..061a9a64fd1 100644 --- a/src/pyodide/internal/topLevelEntropy/lib.ts +++ b/src/pyodide/internal/topLevelEntropy/lib.ts @@ -9,14 +9,14 @@ import { default as entropyPatches } from 'pyodide-internal:topLevelEntropy/entropy_patches.py'; import { default as entropyImportContext } from 'pyodide-internal:topLevelEntropy/entropy_import_context.py'; import { default as importPatchManager } from 'pyodide-internal:topLevelEntropy/import_patch_manager.py'; -import { IS_TRACING } from 'pyodide-internal:metadata'; import { LOADED_SNAPSHOT_VERSION } from 'pyodide-internal:snapshot'; import { simpleRunPython } from 'pyodide-internal:util'; +// Disable entropy gating when we've restored a snapshot of version 1. Version 1 snapshots were +// created without entropy gating and will crash if they are used with it. If we are creating a new +// snapshot or using one of version at least 2 we should gate. // TODO: When we've updated all the snapshots, remove this. -const SHOULD_GATE_ENTROPY = - !IS_TRACING && - (LOADED_SNAPSHOT_VERSION === undefined || LOADED_SNAPSHOT_VERSION === 2); +const SHOULD_GATE_ENTROPY = LOADED_SNAPSHOT_VERSION !== 0 && LOADED_SNAPSHOT_VERSION !== 1; let allowed_entropy_calls_addr: number; From 884486d20bab87c2ae0b50366120716a04320968 Mon Sep 17 00:00:00 2001 From: Hood Chatham Date: Fri, 6 Sep 2024 15:54:45 +0200 Subject: [PATCH 2/2] Always invalidate_caches when loading snapshots Originally we had baseline snapshots shared and dedicated snapshots per worker. But now we also have shared package snapshots. To be safe let's just always call it. --- src/pyodide/internal/snapshot.ts | 5 ++--- src/pyodide/internal/topLevelEntropy/lib.ts | 3 ++- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/pyodide/internal/snapshot.ts b/src/pyodide/internal/snapshot.ts index 6ad4131202e..68b5b12381b 100644 --- a/src/pyodide/internal/snapshot.ts +++ b/src/pyodide/internal/snapshot.ts @@ -437,9 +437,8 @@ let TEST_SNAPSHOT: Uint8Array | undefined = undefined; })(); export function finishSnapshotSetup(pyodide: Pyodide): void { - if (DSO_METADATA?.settings?.baselineSnapshot) { - // Invalidate caches if we have a baseline snapshot because the contents of site-packages may - // have changed. + if (LOADED_SNAPSHOT_VERSION !== undefined) { + // Invalidate caches if we have a snapshot because the contents of site-packages may have changed. simpleRunPython( pyodide._module, 'from importlib import invalidate_caches as f; f(); del f' diff --git a/src/pyodide/internal/topLevelEntropy/lib.ts b/src/pyodide/internal/topLevelEntropy/lib.ts index 061a9a64fd1..320b265e25e 100644 --- a/src/pyodide/internal/topLevelEntropy/lib.ts +++ b/src/pyodide/internal/topLevelEntropy/lib.ts @@ -16,7 +16,8 @@ import { simpleRunPython } from 'pyodide-internal:util'; // created without entropy gating and will crash if they are used with it. If we are creating a new // snapshot or using one of version at least 2 we should gate. // TODO: When we've updated all the snapshots, remove this. -const SHOULD_GATE_ENTROPY = LOADED_SNAPSHOT_VERSION !== 0 && LOADED_SNAPSHOT_VERSION !== 1; +const SHOULD_GATE_ENTROPY = + LOADED_SNAPSHOT_VERSION !== 0 && LOADED_SNAPSHOT_VERSION !== 1; let allowed_entropy_calls_addr: number;