Repository navigation
Preserve codec identity after failed open and allow safe retries - #2503
Open
Robolightning wants to merge 1 commit into
Open
Robolightning wants to merge 1 commit into
Robolightning wants to merge 1 commit into
Conversation
Robolightning
marked this pull request as ready for review
November 2, 2026 17:39
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
After avcodec_open2 rejects an invalid audio configuration, FFmpeg clears AVCodecContext.codec during cleanup. PyAV subsequently reads that pointer for codec identity and retry, so a second encode/open can crash the interpreter instead of raising an exception.
Keep the selected const AVCodec pointer independently of the mutable FFmpeg context and use it for open, static codec metadata/capabilities, parser selection and profile guards. The selected implementation is retained, including libopus, rather than looking up a default codec by ID.
The subprocess regression performs three actual AAC open failures, checks codec identity, restores the full valid audio configuration (FFmpeg also clears ch_layout), then encodes, muxes and decodes on the same context. A native crash or hang fails the parent test.
Validation: clean CPython 3.15t Windows/Linux builds of all extensions against the official FFmpeg 9.0.2-2 shared SDK; canonical suite 543 passed, 31 SDK capability skips on each platform, plus real libopus/Ogg retries and recovery, every native module import and lossless encoding across native threads with natural GIL OFF. Ruff, configured isort and mypy passed. These local native results apply to the matching runtime-source change on the StableKite branch; this PR preserves newer upstream changes including boolean property results and GIL release when freeing a codec context. Upstream CI is the separate validation of this PR on current master.