Repository navigation
fix: Read and write regular files natively so streams release the GIL - #1
Open
christopher5106 wants to merge 1 commit into
Open
christopher5106 wants to merge 1 commit into
christopher5106 wants to merge 1 commit into
Conversation
A Stream serves every read, seek and write of the native library through a Python callback, tens of thousands per asset (one or more per PNG chunk or BMFF box), and each callback retakes the GIL. Signing or reading files from several threads is therefore slower than from one, and any busy Python thread stalls a signature for tens of seconds. When the wrapped object is a regular file on a 64-bit POSIX platform, Stream now hands the native library libc read/lseek/write with a private duplicate of the file descriptor as context: the native call never re-enters Python. The duplicate starts at the Python object's position and gives the final position back on close(), and closing the Python file meanwhile cannot hand its number to another file. In-memory streams, pipes, append-mode files and other platforms keep the Python callbacks, as does Stream.native_file_io = False. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.
Tracks the upstream fix in contentauth#349 (issue contentauth#348). Never merged here: the branch is the head of the upstream PR.
Streamhands regular files to the native library with libcread/lseek/writeon a private duplicate of their descriptor, instead of Python callbacks that retake the GIL tens of thousands of times per asset. With this change, signing from several threads scales (8 threads: 2.2 → 19.2 signatures/s on a 4K PNG, 5.8 → 36.3 on a 73 MB MP4), and a busy Python thread no longer stalls a signature for tens of seconds.Based on
upstream-mainatae8628a. Once upstream merges the PR: deletefix-native-file-streams, close this PR, and syncupstream-main.