hotfix(cli): replace raw subprocess tracebacks with styled error messages and fix restart race condition #96

Merged
zach merged 5 commits from hotfix-restart-race-condition into main 2026-05-24 18:23:43 +02:00
2 changed files with 36 additions and 8 deletions
Showing only changes of commit b49165e7ea - Show all commits
+36 -8
View File
@@ -1,5 +1,8 @@
import subprocess
import sys
import time
import click
import typer
args = ["qs", "-c", "zshell"]
@@ -9,40 +12,65 @@ app = typer.Typer()
@app.command()
def kill():
subprocess.run(args + ["kill"], check=False)
result = subprocess.run(args + ["kill"], capture_output=True)
if result.returncode != 0:
raise click.ClickException("No running instance to kill.")
sys.stderr.write(result.stderr.decode())
@app.command()
def start(no_daemon: bool = False):
subprocess.run(args + ["-n"] + ([] if no_daemon else ["-d"]), check=True)
check = subprocess.run(args + ["ipc"] + ["show"], capture_output=True)
zach marked this conversation as resolved Outdated
Outdated
Review

The start command should not need to run qs -c zshell ipc call, IPC calls are used to trigger states and functions within the QML code. The -n flag already checks if there's a running instance of the shell.

The start command should not need to run `qs -c zshell ipc call`, IPC calls are used to trigger states and functions within the QML code. The `-n` flag already checks if there's a running instance of the shell.
Outdated
Review

Was not aware of this. Remaining functions use ipc, which is why I did not bat an eye. Resolving now.

Was not aware of this. Remaining functions use ipc, which is why I did not bat an eye. Resolving now.
Outdated
Review

Change occurred since I wanted an error block such as in the screenshot. But with that info, ipc is indeed not needed in the code.

Change occurred since I wanted an error block such as in the screenshot. But with that info, ipc is indeed not needed in the code.
if check.returncode == 0:
raise click.ClickException("An instance of this configuration is already running.")
result = subprocess.run(args + ["-n"] + ([] if no_daemon else ["-d"]), capture_output=True)
if result.returncode != 0:
raise click.ClickException(result.stderr.decode().strip())
sys.stderr.write(result.stderr.decode())
@app.command()
def restart(no_daemon: bool = False):
subprocess.run(args + ["kill"], check=False)
subprocess.run(args + ["kill"])
for _ in range(50):
AramJonghu marked this conversation as resolved Outdated
Outdated
Review

This seems ugly. Personally think we should just set a sleep timer of ~5 seconds, then run code.

This seems ugly. Personally think we should just set a sleep timer of ~5 seconds, then run code.
Outdated
Review

Is that not an messier implementation? A user would want a restart to be quick (quicker than running the kill and start command separately). 2.5 or higher stock sleep timer would make little sense.

time.monotonic() could be used to hide the ugliness, but same logic/idea.

Is that not an messier implementation? A user would want a restart to be quick (quicker than running the kill and start command separately). 2.5 or higher stock sleep timer would make little sense. time.monotonic() could be used to hide the ugliness, but same logic/idea.
Outdated
Review

Running a loop would keep a CPU thread busy until it's able to launch the shell, while a sleep timer wouldn't. I don't really see much benefit in looping upwards of 50 times just to make sure we can run the start command exactly when it's available.

Running a loop would keep a CPU thread busy until it's able to launch the shell, while a sleep timer wouldn't. I don't really see much benefit in looping upwards of 50 times just to make sure we can run the start command exactly when it's available.
Outdated
Review

Is that not okay for a rare command as restart?

We could change the loop to check every 250ms instead, so it is 10 checks instead of 50.

Is that not okay for a rare command as restart? We could change the loop to check every 250ms instead, so it is 10 checks instead of 50.
Outdated
Review

That's a good middle ground I think, agreed.

That's a good middle ground I think, agreed.
Outdated
Review

Changed it.

Changed it.
result = subprocess.run(args + ["kill"], capture_output=True)
if result.returncode == 255:
break
time.sleep(0.05)
subprocess.run(args + ["-n"] + ([] if no_daemon else ["-d"]), check=True)
result = subprocess.run(args + ["-n"] + ([] if no_daemon else ["-d"]), capture_output=True)
zach marked this conversation as resolved Outdated
Outdated
Review

Here we can just call start(), right? Not sure if Typer allows calling command functions.

Here we can just call `start()`, right? Not sure if `Typer` allows calling command functions.
Outdated
Review

It was to have separate tests. Simpler in my eyes (at least at time of writing). I can simplify it by having a start_instance() that start uses outright, and is reused by restart. This could satisfy testing and reuse of code, and perhaps future code changes.

It was to have separate tests. Simpler in my eyes (at least at time of writing). I can simplify it by having a `start_instance()` that start uses outright, and is reused by restart. This could satisfy testing and reuse of code, and perhaps future code changes.
if result.returncode != 0:
raise click.ClickException(result.stderr.decode().strip())
sys.stderr.write(result.stderr.decode())
@app.command()
def show():
subprocess.run(args + ["ipc"] + ["show"], check=True)
result = subprocess.run(args + ["ipc"] + ["show"], capture_output=True)
if result.returncode != 0:
raise click.ClickException(result.stderr.decode().strip())
sys.stderr.write(result.stderr.decode())
@app.command()
def log():
subprocess.run(args + ["log"], check=True)
result = subprocess.run(args + ["log"], capture_output=True)
if result.returncode != 0:
raise click.ClickException(result.stderr.decode().strip())
sys.stdout.write(result.stdout.decode())
sys.stderr.write(result.stderr.decode())
@app.command()
def lock():
subprocess.run(args + ["ipc"] + ["call"] + ["lock"] + ["lock"], check=True)
result = subprocess.run(args + ["ipc"] + ["call"] + ["lock"] + ["lock"], capture_output=True)
if result.returncode != 0:
raise click.ClickException(result.stderr.decode().strip())
sys.stderr.write(result.stderr.decode())
@app.command()
def call(target: str, method: str, method_args: list[str] = typer.Argument(None)):
subprocess.run(args + ["ipc"] + ["call"] + [target] + [method] + (method_args or []), check=True)
result = subprocess.run(args + ["ipc"] + ["call"] + [target] + [method] + (method_args or []), capture_output=True)
if result.returncode != 0:
raise click.ClickException(result.stderr.decode().strip())
sys.stderr.write(result.stderr.decode())