Cancel session background processes

This commit is contained in:
wuyang6
2026-06-12 16:24:25 +08:00
parent 953126e1d3
commit 0a3e044c9d
4 changed files with 125 additions and 7 deletions
+18
View File
@@ -156,6 +156,24 @@ class BashBgStore:
).fetchall()
return [dict(row) for row in rows]
def list_active_for_session(
self,
account_key: str,
session_id: str,
) -> list[dict[str, Any]]:
with self._connect() as conn:
rows = conn.execute(
"""
select * from bash_bg_tasks
where account_key = ?
and session_id = ?
and status = 'running'
order by started_at desc
""",
(account_key, session_id),
).fetchall()
return [dict(row) for row in rows]
def get(self, task_id: str) -> dict[str, Any] | None:
with self._connect() as conn:
row = conn.execute(
+18 -3
View File
@@ -536,13 +536,28 @@ PY
raise JupyterRuntimeError('Remote command is empty.')
marker = f'__ZK_AGENT_EXIT_{uuid.uuid4().hex}__'
workspace = shlex.quote(self.binding.workspace_cwd)
run_dir = f'{self.binding.workspace_cwd}/scratchpad/.run_pids'
quoted_run_dir = shlex.quote(run_dir)
pid_file = shlex.quote(f'{run_dir}/{marker}.pid')
env_prefix = self._remote_env_prefix()
inner_command = f'{env_prefix}{command}'
wrapped = (
f'mkdir -p {workspace} && '
f'mkdir -p {workspace} {quoted_run_dir} && '
f'cd {workspace} && '
f'bash -lc {shlex.quote(inner_command)}; '
f'printf "\\n{marker}:%s\\n" "$?"'
f'( setsid bash -lc {shlex.quote(inner_command)} ) & '
'__zk_pid=$!; '
f'printf "%s\\n" "$__zk_pid" > {pid_file}; '
'cleanup() { '
'kill -TERM -- -"${__zk_pid}" 2>/dev/null '
'|| kill -TERM "${__zk_pid}" 2>/dev/null || true; '
'sleep 0.2; '
'kill -KILL -- -"${__zk_pid}" 2>/dev/null || true; '
'}; '
'trap "cleanup" INT TERM; '
'wait "$__zk_pid"; __zk_status=$?; '
'trap - INT TERM; '
f'rm -f {pid_file}; '
f'printf "\\n{marker}:%s\\n" "$__zk_status"'
)
with self._lock:
name = self._ensure_terminal()