viewer.launch_passive: segfault (or hang) at interpreter exit after the `with` block closes the viewer
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
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.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.)
- Lenguaje dominante
- C++
- Estrellas
- 18.3k
- Forks
- 2.2k
- Merge medio
- 9 d 11 h
- PR fusionados (30 d)
- 5
Preparar el entorno
- Sin Dockerfile ni archivo de Docker Compose
- Sin plantilla de pull request
- Leer la guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de google-deepmind/mujoco
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
google-deepmind/mujoco#3932 ·
Los mantenedores suelen responder en 6 días
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
google-deepmind/mujoco#3929 ·
Los mantenedores suelen responder en 6 días
-
`import mujoco.mjx` fails with TypeError when warp-lang is too oldPosiblemente ocupada @moah0911 la tomó hace 22 días. Abiertobug
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
google-deepmind/mujoco#3738 ·
Los mantenedores suelen responder en 6 días
-
Unity 6.6 supportQuizá libre de nuevo @Balint-H la tomó hace 53 días y no hay ningún pull request abierto. Abiertoenhancement
Dificultad 2/5 1-3 horas Aptitud para principiantes 75/100
google-deepmind/mujoco#3615 · 1 comentario · 1 asignado ·
Los mantenedores suelen responder en 6 días
-
Filament: `filament.ao.bilateral_threshold` and six other model options are ignored since 4.0.1, which changed the default ambient occlusionPosiblemente ocupada @jeongseok-meta la tomó hoy. Abierto
Dificultad 4/5 3-5 días Aptitud para principiantes 56/100
google-deepmind/mujoco#3933 ·
Los mantenedores suelen responder en 6 días
Todos los issues de google-deepmind/mujoco
Issues similares
-
Dificultad 1/5 Menos de una hora Aptitud para principiantes 91/100
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 69/100
maplibre/maplibre-native#5072 ·
Los mantenedores suelen responder en 1 día
-
[UBSan] spawn_simulator passes null child_options.args to uv_spawn (execvp nonnull violation)Abierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 79/100
tenstorrent/tt-umd#4017 ·
Los mantenedores suelen responder en 3 días
-
difficulty:easy func:gui prio:low type:bug
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
NatronGitHub/Natron#1538 ·
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
hyprwm/hyprsunset#103 · 1 comentario ·