viewer.launch_passive: segfault (or hang) at interpreter exit after the `with` block closes the viewer
メンテナーはふだん 6 日以内に返信
評価
- 難易度
- 3/5
- 見積もり時間
- 1〜2日
- 初心者へのやさしさ
- 35/100
調査の方向性
Read python/mujoco/viewer.py, focusing on Handle.close(), launch_passive, and _launch_internal; the issue reports that the render thread may still be tearing down GLFW resources when interpreter shutdown calls glfw.terminate. Start by tracing how the passive viewer thread is started and stopped, then run the provided reproduction on Linux. Done when the reproduction exits cleanly without hanging or segfaulting.
索引モデルが issue の本文から書いたものです。
説明
Intro
Hi! I use MuJoCo to benchmark rigid–soft coupling (flex gel pads on a gripper). I found that scripts using the passive viewer crash on Linux when they exit right after the viewer is closed.
My setup
- MuJoCo 4.0.0 (Python package from PyPI).
python/mujoco/viewer.pyis identical at the 4.0.1 tag. - pyGLFW 2.11.0, GLFW library 3.4.0 (X11 GLX)
- Python 3.12.3, numpy 2.5.4
- Ubuntu 24.04.4 LTS, Linux 7.0 x86_64, X11 session
- NVIDIA GeForce RTX 5060 Ti, driver 595.84
What's happening? What did you expect?
The process segfaults at exit (exit code 139) after the with mujoco.viewer.launch_passive(...) block ends, i.e. after Handle.close(). Occasionally it hangs instead and never exits. I expected it to exit cleanly.
With the script below, 10 of 10 runs segfaulted. In an earlier batch of 3 runs, 2 segfaulted and 1 hung.
python -X faulthandler repro.py shows the two threads racing:
Fatal Python error: Segmentation fault
Current thread (most recent call first):
File ".../mujoco/viewer.py", line 534 in _launch_internal
File "/usr/lib/python3.12/threading.py", line 1010 in run
...
Thread (main, most recent call first):
File ".../glfw/__init__.py", line 832 in terminate
My reading of viewer.py:
Handle.close()(called by__exit__) only callssim.exit(), which sets the exit request. It does not wait for the render thread.- On Linux,
launch_passiveruns_launch_internalin a daemon thread. That thread is still leavingrender_loop()and runningside_thread.join()/simulate.destroy(). - Meanwhile the main thread reaches interpreter shutdown. The
atexit.register(glfw.terminate)handler (registered in_launch_internal) then callsglfwTerminate()concurrently with the window and context teardown on the render thread.
Steps for reproduction
- Save the script below as
repro.py. - Run
python repro.py; echo $?a few times. - It prints
139(segfault), or occasionally hangs.
Minimal model for reproduction
<mujoco><worldbody><body><freejoint/><geom size=".1"/></body></worldbody></mujoco>
Code required for reproduction
import mujoco
import mujoco.viewer
m = mujoco.MjModel.from_xml_string(
'<mujoco><worldbody><body><freejoint/><geom size=".1"/></body></worldbody></mujoco>')
d = mujoco.MjData(m)
with mujoco.viewer.launch_passive(m, d) as viewer:
for _ in range(100):
mujoco.mj_step(m, d)
viewer.sync()
# exits here -> segfault in glfw.terminate (atexit) racing with the viewer thread
Workaround
Wait for the viewer's thread before exiting. With this change, the runs I tried (on a larger flex model) exited with code 0, while the same script without the join segfaulted:
import threading
before = set(threading.enumerate())
with mujoco.viewer.launch_passive(m, d) as viewer:
...
for t in set(threading.enumerate()) - before:
t.join(timeout=10.0)
Suggested fix
Keep a reference to the render thread in the Handle returned by launch_passive, and have close() / __exit__ join it after sim.exit() (non-macOS path). Alternatively, exit_simulate/glfw.terminate at exit could wait for the render loop to finish. Either way, glfwTerminate would no longer run while the viewer is still destroying its window.
Confirmations
- I searched the latest documentation thoroughly before posting.
- I searched previous Issues and Discussions, I am certain this has not been raised before. (Older, different passive viewer crashes: #649, #790.)
- 主要言語
- C++
- スター
- 18.3k
- フォーク
- 2.2k
- 平均マージ
- 9日 11時間
- マージ済み PR(30日)
- 5
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートなし
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
google-deepmind/mujoco のほかの issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
google-deepmind/mujoco#3932 ·
メンテナーはふだん 6 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
google-deepmind/mujoco#3929 ·
メンテナーはふだん 6 日以内に返信
-
`import mujoco.mjx` fails with TypeError when warp-lang is too old対応中かも @moah0911 が 22 日前に担当しました。 オープンbug
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
google-deepmind/mujoco#3738 ·
メンテナーはふだん 6 日以内に返信
-
Unity 6.6 support再び着手できるかも @Balint-H が 53 日前に担当しましたが、オープン中のプルリクエストはありません。 オープンenhancement
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
google-deepmind/mujoco#3615 · コメント 1 件 · 担当者 1 名 ·
メンテナーはふだん 6 日以内に返信
-
Filament: `filament.ao.bilateral_threshold` and six other model options are ignored since 4.0.1, which changed the default ambient occlusion対応中かも @jeongseok-meta が今日担当しました。 オープン
難易度 4/5 3〜5日 初心者へのやさしさ 56/100
google-deepmind/mujoco#3933 ·
メンテナーはふだん 6 日以内に返信
google-deepmind/mujoco の issue をすべて見る
似ている issue
-
難易度 1/5 1時間未満 初心者へのやさしさ 91/100
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 69/100
maplibre/maplibre-native#5072 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 79/100
tenstorrent/tt-umd#4017 ·
メンテナーはふだん 3 日以内に返信
-
difficulty:easy func:gui prio:low type:bug
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
NatronGitHub/Natron#1538 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
hyprwm/hyprsunset#103 · コメント 1 件 ·