Loading page...Загружаем страницу...正在加载页面...
AtlasRepo

Hunting Down a Zombie Python Thread: My First PR to Webots

说明与用途

问题:Webots 在 Windows 上的 Python 脚本未能正常退出,留下僵尸进程。解决方案:将辅助线程设为守护线程,并在 atexit 中显式关闭管道,确保进程干净退出。

问题

Python controller scripts on Windows never exited, leaving zombie python.exe processes after simulation ended.

结果

The patch eliminates the zombie process issue on Windows by making the helper thread daemon and ensuring the pipe is closed before the interpreter exits, allowing clean shutdown.

工作流

  • 1. Identify that python.exe stays alive after simulation ends.
  • 2. Inspect Webots Python API (robot.py) to find StdStreamRedirect handling.
  • 3. Discover helper thread is non‑daemon and blocks on readline waiting for EOF.
  • 4. Realize EOF is sent only after atexit closes write end, causing deadlock.
  • 5. Modify StdStreamRedirect: set daemon=True for helper thread.
  • 6. In atexit callback, close write pipe, join thread, restore sys.stdout/sys.stderr.
  • 7. Test on Windows 11 to confirm no hangs and exit code 0.

证据

When running a controller with stdout/stderr redirection enabled (–stdout-redirect): ... python.exe never exited but stayed stuck in the background as a zombie process until forcefully killed.

来源

The helper thread was created with daemon=False. It was blocked inside self._r.readline(), waiting for an EOF on the pipe.

来源

The fix was clean and minimal: Daemonize the helper thread (daemon=True).

来源

Tested on Windows 11 — zero hangs, instant exit with code 0.

来源

  • 记录日期: 2026-10-10T15:41:03.440Z