test: Isolation jupyter env

This commit is contained in:
tao.chen
2026-08-05 18:59:33 +08:00
parent d5de1a63d4
commit c4529782b5
7 changed files with 37 additions and 629 deletions
+21 -2
View File
@@ -64,6 +64,14 @@ JUPYTER_MAX_LIFETIME_SECONDS = int(
os.environ.get("JUPYTER_MAX_LIFETIME_SECONDS", str(24 * 3600))
)
# Jupyter Server no longer lives in /app/.venv (the runtime's own venv).
# It is provisioned in a dedicated Python 3.12 venv by runtime/Dockerfile,
# which also registers the Python 3.8 / 3.10 kernelspecs under that venv's
# share/jupyter/kernels tree. The runtime only launches the server here;
# user code runs in the per-kernel venv the user picks in the Notebook UI.
JUPYTER_PY312_VENV = os.environ.get("JUPYTER_PY312_VENV", "/opt/venv/python3.12")
JUPYTER_BIN = os.path.join(JUPYTER_PY312_VENV, "bin", "jupyter")
# Sidecar metadata file written next to the workspace directory. Holds
# the runtime state we need to reconstruct the in-memory map after a
# crash. We intentionally store only the bits that are safe to recover:
@@ -196,7 +204,7 @@ async def start_workspace(ws_id: str) -> dict:
base_path = f"/jupyter/{ws_id}/"
cmd = [
"jupyter",
JUPYTER_BIN,
"notebook",
f"--port={port}",
"--ip=0.0.0.0",
@@ -214,11 +222,22 @@ async def start_workspace(ws_id: str) -> dict:
"--NotebookApp.disable_check_xsrf=True",
]
# Isolate the Jupyter subprocess inside the dedicated Python 3.12
# venv. VIRTUAL_ENV makes `python` / `pip` resolve against it; PATH
# prepended so its `bin/` wins over the runtime's /app/.venv.
jupyter_env = os.environ.copy()
jupyter_env["VIRTUAL_ENV"] = JUPYTER_PY312_VENV
jupyter_env["PATH"] = (
os.path.join(JUPYTER_PY312_VENV, "bin")
+ os.pathsep
+ jupyter_env.get("PATH", "")
)
try:
process, log_file = start_process(
cmd,
WORKSPACES_ROOT,
env={"PATH": f"/app/.venv/bin:{os.environ.get('PATH', '')}"},
env=jupyter_env,
)
except Exception as e:
logger.error(f"Failed to start Jupyter for {ws_id}: {e}")