From 3deda4781d639d8b382eb9d0062304265fb1a588 Mon Sep 17 00:00:00 2001 From: N0zoM1z0 <161784452+N0zoM1z0@users.noreply.github.com> Date: Tue, 13 Oct 2026 07:57:06 +0000 Subject: [PATCH] fix(hopper): parse Python helper interpreter options Resolve the script operand using CPython option boundaries, including combined flags, separate or attached option values, and --. Keep command/module arguments distinct and defer unknown invocation failures rather than declaring them clean. Co-authored-by: Kaoru0822 <208281322+kaoru0822-kitauji@users.noreply.github.com> --- scripts/lib/clean-linux-hopper-runtime.mjs | 39 ++++++++++++++- .../process/cleanLinuxHopperRuntime.test.ts | 50 ++++++++++++++----- 2 files changed, 74 insertions(+), 15 deletions(-) diff --git a/scripts/lib/clean-linux-hopper-runtime.mjs b/scripts/lib/clean-linux-hopper-runtime.mjs index 206cba53a..02664ffbe 100644 --- a/scripts/lib/clean-linux-hopper-runtime.mjs +++ b/scripts/lib/clean-linux-hopper-runtime.mjs @@ -1,6 +1,37 @@ import { readFile, readdir, stat } from "node:fs/promises"; import { basename } from "node:path"; +// CPython's interface option ends interpreter parsing; following values are +// script arguments. -W/-X consume either an attached value or the next operand. +// https://docs.python.org/3.12/using/cmdline.html +const pythonScriptOperand = (arguments_) => { + for (let index = 1; index < arguments_.length; index++) { + const option = arguments_[index]; + if (option === "--") return arguments_[index + 1]; + if (option === "-" || option === "") return undefined; + if (!option.startsWith("-")) return option; + if (option === "--check-hash-based-pycs") { + index++; + continue; + } + if (/^--(?:help(?:-env|-xoptions|-all)?|version)$/u.test(option)) + return undefined; + if (option.startsWith("--")) + throw new Error(`Unknown Python helper invocation option: ${option}`); + for (let character = 1; character < option.length; character++) { + const flag = option[character]; + if ("cmh?V".includes(flag)) return undefined; + if (flag === "W" || flag === "X") { + if (character === option.length - 1) index++; + break; + } + if (!"bBdEiIOPqRsSuvx".includes(flag)) + throw new Error(`Unknown Python helper invocation option: ${option}`); + } + } + return undefined; +}; + /** * Observe leftover demo processes on a dedicated clean Linux runner, including * groups reparented after their launcher exits. This is not ownership proof and @@ -37,8 +68,12 @@ export const assertCleanLinuxHopperRuntime = async ( retainedRuntime = arguments_[0] === "/opt/hopper/bin/Hopper" || basename(arguments_[0] ?? "") === "Xvfb" || - (basename(arguments_[0] ?? "") === "python3" && - basename(arguments_[1] ?? "") === "hopper-demo-x11.py"); + (/^python3(?:\.\d+)?$/u.test(basename(arguments_[0] ?? "")) && + arguments_.some( + (value) => basename(value) === "hopper-demo-x11.py", + ) && + basename(pythonScriptOperand(arguments_) ?? "") === + "hopper-demo-x11.py"); if (!retainedRuntime && launchers.length > 0) { const executable = await stat(`/proc/${entry.name}/exe`, { bigint: true, diff --git a/tests/boundary/process/cleanLinuxHopperRuntime.test.ts b/tests/boundary/process/cleanLinuxHopperRuntime.test.ts index 2308e5659..6194200b7 100644 --- a/tests/boundary/process/cleanLinuxHopperRuntime.test.ts +++ b/tests/boundary/process/cleanLinuxHopperRuntime.test.ts @@ -73,11 +73,26 @@ it.runIf(process.platform === "linux")( }, ); -it - .runIf(process.platform === "linux") - .each(["Xvfb-named Node", "Python helper", "alternate launcher"])( - "rejects a reparented %s without signaling it", - async (mode) => { +it.runIf(process.platform === "linux").each([ + ["Xvfb-named Node", null, true], + ["Python helper", [], true], + ["Python helper with -u", ["-u"], true], + [ + "Python helper with combined/value options", + ["-Bu", "-W", "ignore", "-X", "dev", "--check-hash-based-pycs", "always"], + true, + ], + ["Python helper after --", ["--"], true], + [ + "Python -c script argument", + ["-c", "import time; print('ready', flush=True); time.sleep(30)"], + false, + ], + ["Python -m script argument", ["-m", "unrelated"], false], + ["alternate launcher", null, true], +] as const)( + "classifies a reparented %s without signaling it", + async (mode, pythonOptions, retained) => { const runId = randomUUID(); const childCode = "process.title = 'Xvfb'; setInterval(() => {}, 1000); process.send('ready');"; @@ -86,17 +101,19 @@ it const alternate = join(directory, "Hopper-alternate"); const helper = join(directory, "hopper-demo-x11.py"); const alternateMode = mode === "alternate launcher"; - const pythonMode = mode === "Python helper"; + const pythonMode = pythonOptions !== null; if (alternateMode) { await copyFile("/usr/bin/sleep", launcher); await chmod(launcher, 0o700); await symlink(launcher, alternate); } - if (pythonMode) + if (pythonMode) { await writeFile( helper, "import time\nprint('ready', flush=True)\ntime.sleep(30)\n", ); + await copyFile(helper, join(directory, "unrelated.py")); + } const command = alternateMode ? alternate : pythonMode @@ -105,7 +122,7 @@ it const arguments_ = alternateMode ? ["30"] : pythonMode - ? [helper] + ? [...pythonOptions, helper] : ["-e", childCode]; const stdio = alternateMode ? "ignore" @@ -115,7 +132,7 @@ it const parentCode = ` const { spawn } = require('node:child_process'); const child = spawn(${JSON.stringify(command)}, ${JSON.stringify(arguments_)}, { - detached: true, stdio: ${JSON.stringify(stdio)} + detached: true, stdio: ${JSON.stringify(stdio)}, cwd: ${JSON.stringify(directory)} }); child.once('error', () => process.exit(1)); const ready = () => { @@ -175,11 +192,18 @@ it `/proc/${String(pid)}/cmdline`, "utf8", ); - expect(commandLine.split("\0").slice(0, 2)).toEqual([command, helper]); + expect( + commandLine.split("\0").slice(0, pythonOptions.length + 2), + ).toEqual([command, ...pythonOptions, helper]); } - await expect( - assertCleanLinuxHopperRuntime(alternateMode ? [launcher] : []), - ).rejects.toThrow("Clean Linux runner retained a Hopper demo process"); + const observation = assertCleanLinuxHopperRuntime( + alternateMode ? [launcher] : [], + ); + if (retained) + await expect(observation).rejects.toThrow( + "Clean Linux runner retained a Hopper demo process", + ); + else await expect(observation).resolves.toBeUndefined(); // Observation must not terminate the fixture, even though it resembles Xvfb. const { stdout: alive } = await execFileAsync("ps", [ "-p",