Skip to content

Close path-owned upload files and retain retry support - #410

Merged
shcheklein merged 9 commits into
mainfrom
fix/upload-file-ownership
Nov 25, 2026
Merged

shcheklein merged 9 commits into
mainfrom
fix/upload-file-ownership

Conversation

@shcheklein

@shcheklein shcheklein commented Nov 22, 2026 •

Copy link
Copy Markdown
Member

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 successful Upload() closes it. A failed upload leaves it open so calling Upload() 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 with file.content.close(). Streams assigned directly to content stay 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.

@shcheklein shcheklein left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread pydrive2/files.py Resolved
Comment thread pydrive2/files.py Resolved

@shcheklein shcheklein left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

@shcheklein

Copy link
Copy Markdown
Member Author

I checked the proposal to keep only the absolute path and defer the first open until Upload(). SetContentFile() documents that it opens the local file, and the public file.content stream is available immediately. A lazy open would move missing-file errors to upload time. If another process replaced the path between the two calls, it could also change the bytes sent on the first attempt.

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

@shcheklein

Copy link
Copy Markdown
Member Author

I checked the downstream upload path. dvc-gdrive 3.0.1 passes caller-owned streams to PyDrive2's filesystem upload_fobj. It does not stage these uploads with SetContentFile. Against a simulated Drive, a warm DVC upload used one upload-session POST and one body PUT on both fsspec versions tested.

The direct PyDrive2 API still exposes an open file immediately after SetContentFile. Staging a large batch thus retains those descriptors. Keeping eager opening here preserves the first upload's original file if the path is replaced. Lazy opening is a separate API decision. Both the base branch and this PR raise AttributeError for del file.content.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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

Comment thread pydrive2/files.py Resolved
Comment thread pydrive2/files.py Resolved
Comment thread pydrive2/files.py Resolved
Comment thread pydrive2/files.py Resolved
@shcheklein
shcheklein merged commit b8dc9f4 into main Nov 25, 2026
28 checks passed
@shcheklein
shcheklein deleted the fix/upload-file-ownership branch November 25, 2026 05:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants