Skip to content

Prevent file objects unexpectedly being closed by boto after upload to S3 #212

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

madewithkode
Copy link

@madewithkode madewithkode commented Apr 15, 2025

Description

As outlined in #153, it appears boto3's upload_fileobj() auto closes files when done/interrupted.

This MR utilizes a workaround discussed here to safely override this behavior so file objects are not unexpectedly closed even after an upload.

Fixes

#153

A concern worth noting

The recommended workaround uses a new class NonCloseableBufferedReader which subclasses io.BufferedReader which expects a RawIOBase unbuffered file-like object.

However, a look at the usage of _to_stream() function which is called on the file object in store_file() just before uploading shows that in some cases, it could return a BytesIO object which is already an in-memory stream and already buffered thus, a bit redundant to go ahead and wrap with NonCloseableBufferedReader.

While I can't think of any obvious harm that this can cause and it generally seems like a safe operation, I just wanted to put it out here hopefully it sparks more discussions and insights.

@madewithkode madewithkode force-pushed the 153_file_objects_are_unexpectedly_closed_after_uploading_to_s3 branch from c0970a7 to 65fa074 Compare April 15, 2025 23:31
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.

1 participant