manim icon indicating copy to clipboard operation
manim copied to clipboard

self.play(...) hangs the process forever when an exception is raised during the play loop

Open HamdiBarkous opened this issue 4 months ago • 2 comments

Description of bug / unexpected behavior

Any exception raised during a self.play(...) call hangs the process forever instead of exiting. The traceback prints immediately, but the process never terminates and has to be killed manually (or by a timeout/CI harness).

This is not specific to any animation, mobject, or rate function. The trigger is irrelevant: an updater that raises, a broken rate_func, a custom Animation whose interpolate_mobject throws, etc. all produce the same hang. "Exception during play" is the whole bug.

Expected behavior

When an animation raises, Manim should print the traceback and exit with a non-zero status, like it does for exceptions raised outside the play loop. It should not hang.

How to reproduce

# repro_manim_hang.py
from manim import RIGHT, Dot, Scene


class HangDuringPlay(Scene):
    def construct(self):
        dot = Dot()

        def bad_updater(_mob):
            raise RuntimeError("boom during play")

        dot.add_updater(bad_updater)
        self.add(dot)
        self.play(dot.animate.shift(RIGHT), run_time=1)
timeout 15 manim -ql repro_manim_hang.py HangDuringPlay ; echo "exit: $?"

Observed: the traceback prints within a second, then the process sits until timeout kills it, giving exit code 124. A normal scene exits 0 almost immediately.

Root cause

When self.play() begins an animation, SceneFileWriter.open_partial_movie_stream() spins up a background encoder thread:

# manim/scene/scene_file_writer.py  (open_partial_movie_stream)
self.writer_thread = Thread(target=self.listen_and_write, args=())
self.writer_thread.start()

This thread is not a daemon. It blocks on queue.get() in listen_and_write() and is only ever stopped by a sentinel (-1, None) sent from close_partial_movie_stream(), which is reached only via end_animation() after the play loop completes.

The play loop in Scene.play_internal() runs updaters/animations with no try/finally. If anything raises there, end_animation() is skipped, the sentinel is never sent, and the encoder thread stays parked forever. Manim then exits with a non-zero code, but CPython's interpreter shutdown joins all non-daemon threads before terminating, so the still-parked thread keeps the process alive indefinitely.

Suggested fix

Make the encoder thread a daemon:

self.writer_thread = Thread(target=self.listen_and_write, args=(), daemon=True)

This is minimal and does not affect the success path: on a normal render, close_partial_movie_stream() still sends the sentinel and join()s the thread first, so the daemon flag only matters during an abnormal interpreter shutdown, where it lets the process actually exit (non-zero) instead of hanging. A crashed render's partial .mp4 is abandoned, which is the correct outcome for a failed render.

(An alternative or complement would be a try/finally around the animation lifecycle to flush the stream gracefully on error, but that risks turning a mid-frame failure into a second failure during flush/close, for the marginal benefit of salvaging output from a render that already failed.)

System specifications

  • Manim version: latest main (commit 6f20425e)
  • Python: 3.14.3
  • OS: Linux (WSL2)

Question

I have a fix ready (the daemon=True change above) and can verify the repro then exits non-zero while normal renders still produce valid video. Would you like me to open a PR for this?

HamdiBarkous avatar Jul 30 '26 00:07 HamdiBarkous

I'd like to take a look at this issue , kindly assign

Akulbanxal avatar Jul 30 '26 16:07 Akulbanxal

No longer reproducible on main, fixed via #4899.

behackl avatar Aug 08 '26 10:08 behackl