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

Abierto
#3,923 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 6 días

@blessedefe4real ya está trabajando en esto.

Desde el 19/11/2026.

  • #3925 de @blessedefe4real — abierto

Evaluación

Dificultad
3/5
Tiempo estimado
1-2 días
Aptitud para principiantes
35/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Estancado
Stack tecnológico
python
Área
desktop, robotics

Línea de trabajo

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.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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.)
Lenguaje dominante
C++
Estrellas
18.3k
Forks
2.2k
Merge medio
9 d 11 h
PR fusionados (30 d)
5

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de google-deepmind/mujoco

Todos los issues de google-deepmind/mujoco

Issues similares

Más issues de C++

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.