From 611440f77f5485a7b2f7b0e7126a3c3944e2f7a3 Mon Sep 17 00:00:00 2001 From: LU XIANGRUI <1078666705@qq.com> Date: Sat, 28 Nov 2026 21:49:10 +0900 Subject: [PATCH] fix(python): run the Python Console in a Docker or Compose interpreter's container (#6686) With a Docker or Compose run target as the project interpreter, the Python Console reported "No Python interpreter" because it only asked for a local interpreter. When there is none but a run target interpreter is set, the console now starts its driver as a python run, which Run already routes into the target's container (project mounted, the target's interpreter, docker stop on Stop). The driver exits on SIGTERM itself, since as a container's PID 1 it would otherwise ignore Stop until docker's SIGKILL. Closes #6685. Co-authored-by: wildcatDownstairs <26499420+wildcatDownstairs@users.noreply.github.com> --- src/features/python-console/console.py | 4 +++ src/features/python-console/console.test.ts | 9 +++++++ src/features/python-console/store.test.ts | 28 +++++++++++++++++++-- src/features/python-console/store.ts | 5 +++- 4 files changed, 43 insertions(+), 3 deletions(-) diff --git a/src/features/python-console/console.py b/src/features/python-console/console.py index a54bb4e113..485b208ca3 100644 --- a/src/features/python-console/console.py +++ b/src/features/python-console/console.py @@ -4,11 +4,15 @@ import json import os import reprlib +import signal import sys import textwrap import types RS = "\x1e" +# In a container (`docker compose run` has no `--init`) the driver is PID 1, which ignores +# SIGTERM without a handler: Stop would wait for docker's SIGKILL after 10 s. +signal.signal(signal.SIGTERM, lambda *_: os._exit(128 + signal.SIGTERM)) _out = sys.stdout _repr = reprlib.Repr() _repr.maxstring = _repr.maxother = 200 diff --git a/src/features/python-console/console.test.ts b/src/features/python-console/console.test.ts index a86113b45c..6ed8ddf34e 100644 --- a/src/features/python-console/console.test.ts +++ b/src/features/python-console/console.test.ts @@ -130,3 +130,12 @@ it.skipIf(!DJANGO)( }, 30_000, ); +it.skipIf(!PYTHON)("exits on SIGTERM itself, also as a container's PID 1, which ignores it without a handler", () => { + const result = spawnSync(PYTHON!, ["-I", "-S", "-u", DRIVER], { + input: request("import os, signal, time\nos.kill(os.getpid(), signal.SIGTERM)\ntime.sleep(5)"), + encoding: "utf8", + timeout: 4000, + }); + expect(result.signal).toBeNull(); + expect(result.status).toBe(143); +}); diff --git a/src/features/python-console/store.test.ts b/src/features/python-console/store.test.ts index 4991cfd36a..2a9c2401cf 100644 --- a/src/features/python-console/store.test.ts +++ b/src/features/python-console/store.test.ts @@ -6,6 +6,9 @@ const ipc = vi.hoisted(() => ({ exited: (_e: { run: number; code: number | null }) => {}, started: null as null | { resolve: (id: number) => void; channel: { onmessage?: (c: OutputChunk) => void } }, stdin: vi.fn(), + python: "/venv/bin/python" as string | null, + container: null as null | { target: string }, + command: "", })); vi.mock("@tauri-apps/api/core", () => ({ Channel: class {} })); @@ -25,10 +28,12 @@ vi.mock("@/platform/ipc", async (importOriginal) => { }, commands: { runList: () => Promise.resolve([]), - sdkPythonEffective: () => ok("/venv/bin/python"), + sdkPythonEffective: () => ok(ipc.python), + sdkContainerPython: () => ok(ipc.container), findFilesNamed: () => ok([]), - runStart: (_config: unknown, channel: NonNullable["channel"]) => + runStart: (config: { command: string }, channel: NonNullable["channel"]) => new Promise((resolve) => { + ipc.command = config.command; ipc.started = { channel, resolve: (id) => resolve({ status: "ok", data: { id } }) }; }), runStdin: (id: number, data: string) => { @@ -54,6 +59,8 @@ beforeEach(() => { stopConsole(); ipc.started = null; ipc.stdin.mockClear(); + ipc.python = "/venv/bin/python"; + ipc.container = null; }); it("runs code sent while starting once the driver is ready, even before the start call returns", async () => { @@ -98,3 +105,20 @@ it("reads the settings module manage.py sets", () => { expect(settingsModule(`os.environ.setdefault("DJANGO_SETTINGS_MODULE", f"{SITE}.settings")`)).toBeNull(); expect(settingsModule("print('hi')")).toBeNull(); }); + +it("runs in the container when the project interpreter is a Docker or Compose target's", async () => { + ipc.python = null; + ipc.container = { target: "web" }; + execute("x = 1"); + await settle(); + expect(ipc.command).toBe("python"); + expect(useConsole.getState().noInterpreter).toBe(false); +}); + +it("asks for an interpreter when there is none, local or in a container", async () => { + ipc.python = null; + execute("x = 1"); + await settle(); + expect(ipc.started).toBeNull(); + expect(useConsole.getState().noInterpreter).toBe(true); +}); diff --git a/src/features/python-console/store.ts b/src/features/python-console/store.ts index ddc57ebf76..6999f04275 100644 --- a/src/features/python-console/store.ts +++ b/src/features/python-console/store.ts @@ -128,7 +128,10 @@ export async function startConsole() { try { // The interpreter the status bar shows: in a workspace, the one of the member the active file is in. const active = editorService().activePath(); - const python = await call(commands.sdkPythonEffective(active ? dirname(active) : null)); + const python = + (await call(commands.sdkPythonEffective(active ? dirname(active) : null))) ?? + // A Docker / Compose interpreter: Run swaps `python` for the target's and starts the container. + ((await call(commands.sdkContainerPython()).catch(() => null)) ? "python" : null); if (mine !== generation) return; if (!python) { queue.length = 0;