Set filter graph threads to 1 for thread safety #2065
Closed
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.
Summary
Set the filter graph thread count to 1 in
Graph.__cinit__to prevent potential thread safety issues.Changes
lib.av_opt_set_int(self.ptr, "threads", 1, 0)afteravfilter_graph_alloc()inav/filter/graph.pyxMotivation
By default, FFmpeg's filter graph may use multiple threads for parallel filter execution. However, this can cause race conditions or unexpected behavior when the filter graph is accessed from Python code that isn't designed for concurrent access.
We encountered this issue in a production environment where multi-threaded filter graph execution caused intermittent crashes and data corruption.
Setting
threads=1ensures deterministic, single-threaded filter execution, which is safer for typical PyAV usage patterns where Python's GIL provides thread safety assumptions.Testing
This change has been running in production without issues. The modification is minimal and only affects the threading behavior of filter graphs.