From 57f024981a37f35e7d04795c5eec591d9f1ac01d Mon Sep 17 00:00:00 2001 From: WyattBlue Date: Sun, 25 Oct 2026 16:36:25 -0400 Subject: [PATCH] Release the GIL when freeing a codec context 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 --- av/codec/context.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/av/codec/context.py b/av/codec/context.py index 99a07fe3c..8a27d979f 100644 --- a/av/codec/context.py +++ b/av/codec/context.py @@ -442,7 +442,10 @@ def open(self, strict: cython.bint = True): def __dealloc__(self): if self.ptr: lib.av_freep(cython.address(self.ptr.extradata)) - lib.avcodec_free_context(cython.address(self.ptr)) + # Release the GIL: freeing a frame-threaded decoder waits for its worker + # threads, which may be blocked in log_callback waiting for the GIL. + with cython.nogil: + lib.avcodec_free_context(cython.address(self.ptr)) if self.parser: lib.av_parser_close(self.parser)