viewer.launch_passive: segfault (or hang) at interpreter exit after the `with` block closes the viewer

オープン
#3,923 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 6 日以内に返信

@blessedefe4real がすでに取り組んでいます。

2026年11月19日 から。

  • #3925 @blessedefe4real による — オープン

評価

難易度
3/5
見積もり時間
1〜2日
初心者へのやさしさ
35/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
停滞
技術スタック
python
領域
desktop, robotics

調査の方向性

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.py is 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 calls sim.exit(), which sets the exit request. It does not wait for the render thread.
  • On Linux, launch_passive runs _launch_internal in a daemon thread. That thread is still leaving render_loop() and running side_thread.join() / simulate.destroy().
  • Meanwhile the main thread reaches interpreter shutdown. The atexit.register(glfw.terminate) handler (registered in _launch_internal) then calls glfwTerminate() concurrently with the window and context teardown on the render thread.
Steps for reproduction
  1. Save the script below as repro.py.
  2. Run python repro.py; echo $? a few times.
  3. 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

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

google-deepmind/mujoco のほかの issue

google-deepmind/mujoco の issue をすべて見る

似ている issue

C++ の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。