Skip to content

Commit 98fd5e1

Browse files
authored
Add stacked Python snapshot tests (#5206)
1 parent 0403295 commit 98fd5e1

File tree

4 files changed

+41
-11
lines changed

4 files changed

+41
-11
lines changed

build/wd_test.bzl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,8 @@ SH_RUNTEST_NORMAL = """"$@" -dTEST_TMPDIR=$TEST_TMPDIR"""
113113
# invoke workerd twice for snapshot tests.
114114

115115
WINDOWS_RUNTEST_SNAPSHOT = """
116-
powershell -Command \"%* --python-save-snapshot $ENV:PYTHON_SAVE_SNAPSHOT_ARGS\" `-dTEST_TMPDIR=$ENV:TEST_TMPDIR
116+
$PYTHON_SAVE_SNAPSHOT_OPTIONS = $ENV:PYTHON_SAVE_SNAPSHOT_ARGS -split ' '
117+
powershell -Command \"%* --python-save-snapshot @PYTHON_SAVE_SNAPSHOT_OPTIONS\" `-dTEST_TMPDIR=$ENV:TEST_TMPDIR
117118
set TEST_EXIT=!ERRORLEVEL!
118119
if !TEST_EXIT! EQU 0 (
119120
powershell -Command \"%* --python-load-snapshot snapshot.bin\" `-dTEST_TMPDIR=$ENV:TEST_TMPDIR

src/pyodide/internal/snapshot.ts

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ const CREATED_SNAPSHOT_META: DsoLoadInfo = {
107107
loadOrder: [],
108108
};
109109
if (LOADED_SNAPSHOT_META) {
110+
// Make sure we include the soMemoryBases and loadOrder from the baseline snapshot when we are
111+
// generating stacked snapshots.
110112
Object.assign(
111113
CREATED_SNAPSHOT_META.soMemoryBases,
112114
LOADED_SNAPSHOT_META.soMemoryBases
@@ -642,16 +644,13 @@ export function isRestoringSnapshot(): boolean {
642644
return !!LOADED_SNAPSHOT_META;
643645
}
644646

645-
export function maybeRestoreSnapshot(Module: Module): void {
646-
if (!LOADED_SNAPSHOT_META) {
647+
function checkSnapshotType(snapshotType: string): void {
648+
if (SHOULD_SNAPSHOT_TO_DISK) {
647649
return;
648650
}
649-
const { snapshotSize, snapshotOffset, snapshotReader, settings } =
650-
LOADED_SNAPSHOT_META;
651-
652651
if (
653652
!IS_EW_VALIDATING &&
654-
settings.snapshotType === 'dedicated' &&
653+
snapshotType === 'dedicated' &&
655654
!IS_DEDICATED_SNAPSHOT_ENABLED
656655
) {
657656
throw new PythonRuntimeError(
@@ -661,7 +660,7 @@ export function maybeRestoreSnapshot(Module: Module): void {
661660

662661
if (
663662
!IS_EW_VALIDATING &&
664-
settings.snapshotType !== 'dedicated' &&
663+
snapshotType !== 'dedicated' &&
665664
IS_DEDICATED_SNAPSHOT_ENABLED
666665
) {
667666
throw new PythonRuntimeError(
@@ -672,12 +671,21 @@ export function maybeRestoreSnapshot(Module: Module): void {
672671
// If we have a snapshot in the bundle and the dedicated snapshot flag is enabled, then we
673672
// should verify that the snapshot in the bundle is a dedicated snapshot. If it is not
674673
// we should fail with an error.
675-
if (settings.snapshotType !== 'dedicated' && IS_SECOND_VALIDATION_PHASE) {
674+
if (snapshotType !== 'dedicated' && IS_SECOND_VALIDATION_PHASE) {
676675
throw new PythonRuntimeError(
677676
'The second validation phase should receive a dedicated snapshot, got ' +
678-
settings.snapshotType
677+
snapshotType
679678
);
680679
}
680+
}
681+
682+
export function maybeRestoreSnapshot(Module: Module): void {
683+
if (!LOADED_SNAPSHOT_META) {
684+
return;
685+
}
686+
const { snapshotSize, snapshotOffset, snapshotReader, settings } =
687+
LOADED_SNAPSHOT_META;
688+
checkSnapshotType(settings.snapshotType);
681689

682690
Module.growMemory(snapshotSize);
683691
snapshotReader.readMemorySnapshot(snapshotOffset, Module.HEAP8);

src/workerd/server/tests/python/BUILD.bazel

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,21 @@ py_wd_test(
7272
make_snapshot = False,
7373
use_snapshot = "numpy",
7474
)
75+
76+
py_wd_test(
77+
name = "numpy_stacked_snapshot",
78+
directory = "numpy",
79+
feature_flags = ["python_dedicated_snapshot"],
80+
make_snapshot = True,
81+
python_flags = ["0.28.2"],
82+
use_snapshot = "baseline",
83+
)
84+
85+
py_wd_test(
86+
name = "fastapi_stacked_snapshot",
87+
directory = "fastapi",
88+
feature_flags = ["python_dedicated_snapshot"],
89+
make_snapshot = True,
90+
python_flags = ["0.28.2"],
91+
use_snapshot = "baseline",
92+
)

src/workerd/server/tests/python/py_wd_test.bzl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,13 @@ def _py_wd_test_helper(
1717
make_snapshot,
1818
use_snapshot,
1919
args,
20+
feature_flags,
2021
data = [],
2122
**kwargs):
2223
name_flag = name + "_" + python_flag
2324
templated_src = name_flag.replace("/", "-") + "@template"
2425
templated_src = "/".join(src.split("/")[:-1] + [templated_src])
25-
flags = _get_enable_flags(python_flag)
26+
flags = _get_enable_flags(python_flag) + feature_flags
2627
feature_flags_txt = ",".join(['"{}"'.format(flag) for flag in flags])
2728

2829
load_snapshot = None
@@ -106,6 +107,7 @@ def py_wd_test(
106107
name = None,
107108
python_flags = "all",
108109
skip_python_flags = [],
110+
feature_flags = [],
109111
args = [],
110112
size = "enormous",
111113
tags = [],
@@ -148,6 +150,7 @@ def py_wd_test(
148150
python_flag,
149151
make_snapshot = make_snapshot,
150152
use_snapshot = use_snapshot,
153+
feature_flags = feature_flags,
151154
data = data,
152155
args = args,
153156
size = size,

0 commit comments

Comments
 (0)