Skip to content

Commit a89bf59

Browse files
committed
fix: page in .py sources during warmup for snapshot restore
Signed-off-by: danbugs <danilochiarlone@gmail.com>
1 parent 1545726 commit a89bf59

1 file changed

Lines changed: 28 additions & 0 deletions

File tree

examples/python-agent-driver/hl_pydriver.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,34 @@ static void py_initialize_once(void)
259259
" except Exception as _e:\n"
260260
" sys.stderr.write(f'warn: preload {_mod} failed: {_e}\\n')\n");
261261

262+
/* Exercise pyarrow's pandas shim so it imports pandas_compat,
263+
* vendored.version, etc. during warmup. Without this, the shim
264+
* lazily imports these on first use, but after snapshot restore
265+
* (without initrd re-mapping) the .py files are zeroed pages. */
266+
PyRun_SimpleString(
267+
"try:\n"
268+
" import pandas as _pd\n"
269+
" _pd.DataFrame({'_': ['x']})\n"
270+
" del _pd\n"
271+
"except Exception:\n"
272+
" pass\n");
273+
274+
/* Page in all .py sources so they survive snapshot restore.
275+
* The snapshot captures only pages faulted during warmup; any
276+
* .py file not yet read would appear as null bytes after restore
277+
* (since we skip initrd re-mapping to avoid overlap). */
278+
PyRun_SimpleString(
279+
"import os as _os\n"
280+
"for _r, _d, _ff in _os.walk('/usr/local/lib/python3.12'):\n"
281+
" for _fn in _ff:\n"
282+
" if _fn.endswith('.py'):\n"
283+
" try:\n"
284+
" with open(_os.path.join(_r, _fn), 'rb') as _fh:\n"
285+
" _fh.read()\n"
286+
" except Exception:\n"
287+
" pass\n"
288+
"del _os\n");
289+
262290
/* Monkey-patch zipfile.ZipInfo.__init__ to clamp pre-1980 timestamps.
263291
* Unikraft's ramfs reports epoch-0 (1970) for file mtimes; Python's
264292
* zipfile rejects timestamps before 1980, breaking openpyxl's XLSX

0 commit comments

Comments
 (0)