Skip to content
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

Ensure that file is open prior to reading from it #939

Closed
wants to merge 1 commit into from

Conversation

ohenrik
Copy link

@ohenrik ohenrik commented Sep 30, 2015

Fixes an issue where Aws::Plugins::S3Md5s::Handler.md5 would crash upon receiving a closed file.

We're not sure whether this is aws-sdk-ruby's responsibility, but seeing as how body is treated very much like a file (we're calling read and rewind on it already) it seems sensible to check that it's actually open prior to reading from it.

Fixes refile/refile-s3#10

Fixes an issue where Aws::Plugins::S3Md5s::Handler.md5 would crash upon receiving a closed file.
@kcollignon
Copy link

+1

@trevorrowe
Copy link
Member

I've been considering how to deal with this issue. I am not generally opposed, but as you point out, I don't know if this should be the SDK's responsibility. It seems reasonable, so I'm inclined to explore how to make this happen.

The MD5 handler is not the only place that the file is read. There are three places that come to mind, in the following order:

  • The file is read & rewound to compute a MD5 hash
  • The file is read & rewound to compute a version 4 signature, this happens for certain endpoints.
  • The file is read as it is streamed over the HTTP connection.

MD5 computation is optional, sigv4 is too. If the goal is to accept possibly closed files, we would need to do a closed check before each of these. Either that or we need a pre-check that happens up-front and let the other handlers continue to assume that the IO object is ready to be read.

Thoughts?

@trevorrowe
Copy link
Member

I was taking another look at this. I ran into an issue during testing. The #open method on File is private. I'm looking at another solution.

@trevorrowe
Copy link
Member

This should go out some time next week. I added logic to the incoming request parameter converter. If it receives a closed file or tempfile it will open a new file in read-binary mode. Thanks for your patience.

awood45 added a commit that referenced this pull request Oct 22, 2015
@Senjai
Copy link

Senjai commented Oct 26, 2015

Shouldn't we raise an exception here? In what scenarios would we passing a closed file to aws be okay?

@trevorrowe
Copy link
Member

@Senjai This issue has cropped up multiple times. Yes, it should be reasonable to require an open file. There are however many different tools and libraries that use the SDK for uploading files. It is not always straight forward for a user to track down the tool that is "responsible" for closing the file.

To make things easier for the user, this patch makes it easier on the end user:
https://github.com/aws/aws-sdk-ruby/blob/master/aws-sdk-core/lib/aws-sdk-core/param_converter.rb#L113-L121

The SDK will not attempt to open the closed file, instead is uses the file to get a path. This is the same as if the user had passed in the path to the file as a string.

@Senjai
Copy link

Senjai commented Oct 26, 2015

@trevorrowe Ah, gotcha. 👍 on that approach.

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.

Error when uploading file from Remote URL
4 participants