Skip to content

Release the GIL when freeing a codec context - #2480

Merged
WyattBlue merged 1 commit into
masterfrom
patch
Oct 25, 2026
Merged

WyattBlue merged 1 commit into
masterfrom
patch

Conversation

@WyattBlue

Copy link
Copy Markdown
Member

Fixes a deadlock when a frame-threaded decoder (thread_type = "AUTO") is freed while its frame threads are still decoding and logging is enabled via av.logging.set_level(...).

CodecContext.__dealloc__ held the GIL in avcodec_free_context, which waits for every frame thread to finish. A frame thread logging an AV_LOG_ERROR blocks in log_callback waiting for the GIL, so neither thread could continue. The free now runs with the GIL released, matching Container.__dealloc__. The hwaccel get_format callback is nogil and only reads _private_data, which stays valid during __dealloc__.

Verified with the reproducer from #2440 (HEVC, 2 worker threads, set_level(FATAL)): it hung 2/2 runs before the change and finished 3/3 after.

Closes #2440

Freeing a frame-threaded decoder waits for its worker threads to finish.
With logging enabled, `log_callback` takes the GIL for `AV_LOG_ERROR`,
so a worker logging an error while `__dealloc__` held the GIL inside
`avcodec_free_context` deadlocked the process. Free it with the GIL
released, as `Container.__dealloc__` already does.

Closes #2440
@WyattBlue
WyattBlue merged commit 482efea into master Oct 25, 2026
9 checks passed
@WyattBlue
WyattBlue deleted the patch branch October 25, 2026 20:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Deadlock: freeing a frame-threaded decoder holds the GIL while FFmpeg waits for frame threads blocked in log_callback

1 participant