Repository navigation
Close path-owned upload files and retain retry support - #410
Conversation
shcheklein
left a comment
There was a problem hiding this comment.
AI-generated review of 4e18085b6550675b170fb6f80304cfb0c6aad5ee.
Very clean and right-sized for #20—keeps SetContentFile backwards-compatible without over-engineering or changing caller-owned stream behavior (GDriveFileSystem._gdrive_upload_fobj and GetContentString are unaffected). Only one small regression (del file.content) and one minor cleanup note inline.
shcheklein
left a comment
There was a problem hiding this comment.
AI-generated re-review of 60968b989479890f11e5182b1f7bddb8f03b3e7a (from scratch).
Looks clean and ready to merge. Removing the redundant with file.content: in test_file.py and switching the retry test to echo_request_body made the diff even tighter. No new bugs or scope issues (@content.deleter for del file.content remains an optional minor nice-to-have, not a blocker).
|
I checked the proposal to keep only the absolute path and defer the first open until This PR keeps that public behavior. It closes owned handles on replacement and after every upload attempt, reopens the path for an explicit retry, and leaves caller-supplied streams under caller control. That fixes the leak without adding a Drive request. A lazy upload API would need its own compatibility decision. SetContentFile documentation |
|
I checked the downstream upload path. dvc-gdrive 3.0.1 passes caller-owned streams to PyDrive2's filesystem The direct PyDrive2 API still exposes an open file immediately after |
There was a problem hiding this comment.
🟡 Changes recommended
Using abspath() can alter symlink traversal for paths containing '..', causing uploads to read a different file than requested; relative paths need anchoring without component normalization.
1 open issue
What changed in this PR
Addresses the file-handle ownership portion of #20 by closing files opened by SetContentFile() while preserving upload retries and caller-owned streams. The proposed public stream setter remains out of scope.
Changes:
- Tracks ownership and closes path-owned files on replacement and after upload attempts.
- Reopens files for retries and clears uploaded content to allow later metadata-only updates.
- Adds lifecycle tests and removes manual file cleanup from an integration test.
| File | Description |
|---|---|
| pydrive2/test/test_files_unit.py | Tests file closure, retries, directory changes, stream ownership, and metadata-only updates. |
| pydrive2/test/test_file.py | Removes manual upload-file cleanup now handled by Upload(). |
| pydrive2/files.py | Implements content ownership, automatic closure, and retry reopening. |
Review effort: Balanced

Addresses the file-handle bug in #20. That issue also proposes a new public stream setter, which remains open.
SetContentFile()opens the local file immediately. A successfulUpload()closes it. A failed upload leaves it open so callingUpload()again reads from the same file, even if the working directory changes. Replacing the content also closes the file PyDrive2 opened. Callers who abandon a failed upload can release it immediately withfile.content.close(). Streams assigned directly tocontentstay under the caller’s control.Tests cover failures while starting the upload and while sending the body, the bytes sent on retry, and ownership when content is replaced through a shallow copy. Request-count tests cover file creation, updates, retries, and a subsequent metadata-only update.