Skip to content

feat: return better error if file size is too big to upload #7775

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

Merged
merged 7 commits into from
Jun 5, 2023

Conversation

Emyrk
Copy link
Member

@Emyrk Emyrk commented Jun 1, 2023

fixes: #7071

The issue was the error condition was only checked after the bytes have been written. So the coderd side was returning an api error for the content being too large before the cli could do the same client side check.

To fix this, I just check if we are going to exceed the limit with the fileInfo.Size before we write.

Comment on lines 113 to 115
if limit != 0 && totalSize >= limit {
return xerrors.Errorf("Archive too big. Must be <= %d bytes", limit)
return fileTooBigError
}
Copy link
Member Author

Choose a reason for hiding this comment

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

I kept this because this is checking the actual bytes written. I can't imagine the fileInfo.Size() being incorrect, as the comment on Size is

// length in bytes for regular files; system-dependent for others

But the others is why I kept this here. Idk how sym links or other edge cases are handled, and then the question of "what does windows do" is not something I care to look into. Doing the check twice is super cheap though, so I can't see this being bad.

@Emyrk Emyrk requested a review from mafredri June 1, 2023 15:29
Copy link
Member

@mafredri mafredri left a comment

Choose a reason for hiding this comment

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

Does this actually fix the underlying problem? AFAICT we're not measuring actual tar size? So there's a small range of inputs where total file size < limit but add tar overhead (prefix, file names) and it exceeds the limit? Or does Write take that into account?

@@ -106,7 +111,7 @@ func Tar(w io.Writer, directory string, limit int64) error {
}
totalSize += wrote
if limit != 0 && totalSize >= limit {
Copy link
Member

@mafredri mafredri Jun 1, 2023

Choose a reason for hiding this comment

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

Either the error or this (+ the other) check are wrong? Error says <=, this says >=, who's right?

Another observation is that this doesn't account for tar overhead, AFAICT. So we might end up with an archive that's too large anyway at which point we would hit the original issue again (bad message from API)?

Copy link
Member

Choose a reason for hiding this comment

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

Perhaps we can wrap w (wc := writeCounter{w}) before passing it on to tar writer, and count bytes written to it? Then we don't need to track wrote here and just check wc.written.

Copy link
Member Author

Choose a reason for hiding this comment

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

You are right about the headers. Honestly I didn't look too deep at the code and just assumed the totalSize was being tracked correctly 🤦.

As for the <= vs >=, I'll change the conditional to > on the check to be consistent with the api.

Copy link
Member Author

Choose a reason for hiding this comment

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

@mafredri good catch. I am using a limit writer now

Copy link
Member Author

Choose a reason for hiding this comment

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

Writing a test or two

Copy link
Member

@mafredri mafredri left a comment

Choose a reason for hiding this comment

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

Nice improvement 👍

@Emyrk Emyrk enabled auto-merge (squash) June 5, 2023 10:59
@Emyrk Emyrk merged commit bbecff2 into main Jun 5, 2023
@Emyrk Emyrk deleted the stevenmasley/template_file_size branch June 5, 2023 11:19
@github-actions github-actions bot locked and limited conversation to collaborators Jun 5, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Show better error when template is too big
2 participants