Issues / #1872

#1872 Ctrl+C / SIGTERM skip the clean shutdown while --open's browser launch is still running (Linux)

open · @spratling · 0 コメント · GitHub で見る

Setup & installServer & APILinux

本文

On Linux, when `--open` starts the browser itself (no browser already running), `webbrowser.open()` waits on the launcher. Here that is `/usr/bin/brave-browser`, a bash wrapper that runs until the browser exits. The main thread stays inside that call for as long as the browser is open.

That call runs before both the SIGTERM handler (`serve/server.py` ~6308 on current `main`) and the `try: while True: time.sleep(1)` / `except KeyboardInterrupt` shutdown block (~6311). So while the browser is open:

- Ctrl+C raises `KeyboardInterrupt` inside `webbrowser.open`. Python prints a traceback and exits, and the `httpd.shutdown` / `engine.close` / `vision.shutdown` path never runs.
- SIGTERM gets Python's default action, because the handler is not installed yet. The process dies with no cleanup.
- The browser is in the same process group, so Ctrl+C also takes it down.

Traceback (v0.1.40.3, Python 3.11):

```
^CTraceback (most recent call last):
  File ".../serve/server.py", line 5611, in <module>
    sys.exit(main())
             ^^^^^^
  File ".../serve/server.py", line 5583, in main
    webbrowser.open(f"http://{'127.0.0.1' if a.host in ('0.0.0.0', '') else a.host}:{a.port}/")
  File ".../lib/python3.11/webbrowser.py", line 87, in open
    if browser.open(url, new, autoraise):
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".../lib/python3.11/webbrowser.py", line 182, in open
    return not p.wait()
               ^^^^^^^^
  File ".../lib/python3.11/subprocess.py", line 1264, in wait
    return self._wait(timeout=timeout)
  File ".../lib/python3.11/subprocess.py", line 2046, in _wait
    (pid, sts) = self._try_wait(0)
  File ".../lib/python3.11/subprocess.py", line 2004, in _try_wait
    (pid, sts) = os.waitpid(self.pid, wait_flags)
KeyboardInterrupt
```

While it was stuck, the server's children were the engine, `strata-vision` and `/bin/bash /usr/bin/brave-browser http://127.0.0.1:8081/`. After the browser was closed, the main thread was back in the `time.sleep(1)` loop, and Ctrl+C then works normally. `"open_browser": false` in `strata-<model>.json` avoids the problem.

Possible fix: open the browser on a daemon thread (`threading.Thread(target=webbrowser.open, args=(url,), daemon=True).start()`), and install the SIGTERM handler before it.

Setup: Strata v0.1.40.3 started as `serve/server.py --engine strata --config ... --port 8081 --open`, Linux, X11, Brave as the default browser.

関連リンク

インストール・モデル・リリースへの站内リンク。