fix(stream): close the file handle when save_as fails - #3203
Open
Mohammed Alkindi (MohammedAlkindi) wants to merge 1 commit into
Open
Mohammed Alkindi (MohammedAlkindi) wants to merge 1 commit into
Mohammed Alkindi (MohammedAlkindi) wants to merge 1 commit into
Conversation
save_as closed the file only after the read loop completed, so an exception from the channel skipped the close. WritableStream.copy handles the upload direction with a context manager already.
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.
Stream.save_ascloses the file only after the read loop finishes, so an exception fromself._channel.send("read", ...)skips the close.Connection.cleanup()raisingTargetClosedErrorinto a pending callback is the ordinary way that happens.The sibling handles the upload direction properly:
WritableStream.copywraps its loop inwith open(path, "rb") as f:.The impact is narrower than it first looks, so to be precise: on CPython an uncaught exception drops the frame and the handle closes anyway. The difference shows when the caller keeps the exception, since
__traceback__retains the frame and its locals:That is the realistic shape —
except Error as e:holdingekeeps the frame — and on Windows an open handle also blocks removing or reopening the partial file.black --check,flake8andisort --checkpass on the changed file, at the versions pinned in.pre-commit-config.yaml.Could not verify: I did not run the repo's pytest suite; it needs the Node driver, which this machine cannot fetch. The measurement above reproduces the same control flow standalone.