Skip to content

Conversation

@buptliuhs
Copy link
Contributor

@buptliuhs buptliuhs commented Aug 6, 2021

Summary

The calculation of Buffer size is incorrect (in both v2 and v3).

Motivation

When using OneDriveLargeFileUploadTask to upload small files (usually < 8KB, Buffer.poolSize), buffer size and slice are not calculated correctly, which causes upload failure.

Example of the issue

When uploading a tiny jpeg file whose size is 747 bytes.
Buffer passed in OneDriveLargeFileUploadTask is:
{ byteOffset: 5808, byteLength: 747, length: 747, poolSize: 8192 }

In this case, byteOffset is 5808 and byteLength is 747.
The old code calculates size = byteLength - byteOffset, which is obviously incorrect.

Because of this, the HTTP header that is used later to upload slices will be like:

{
    "content-length": "0",
    "content-range": "bytes 0--5062/-5061",
    ....
}

And it causes a 400 response.

It's likely to happen when the buffer size is less than the pollSize. When buffer is large, byteLength is usually 0.

Test plan

Upload small files (< 8KB) using OneDriveLargeFileUploadTask.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist

  • I have read the CONTRIBUTING document.
  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@nikithauc nikithauc self-requested a review August 6, 2021 05:17
@nikithauc
Copy link
Contributor

@buptliuhs Thank you for the contribution!

@nikithauc
Copy link
Contributor

@buptliuhs

  1. Can you please provide the error and the steps to reproduce the error?
  2. Would it possible to write a unit test identifying this change?

@buptliuhs
Copy link
Contributor Author

buptliuhs commented Aug 6, 2021

hi @nikithauc,

What I did to reproduce the issue is that uploading a tiny jpeg file whose size is 747 bytes.
Buffer passed in OneDriveLargeFileUploadTask is:
{ byteOffset: 5808, byteLength: 747, length: 747, poolSize: 8192 }

In this case, byteOffset is 5808 and byteLength is 747.
The old code calculate size = byteLength - byteOffset, which is obviously incorrect.

Because of this, the http header will be like:

{
    "content-length": "0",
    "content-range": "bytes 0--5062/-5061",
    ....
}

It's likely to happen when the buffer size is less than the pollSize. When buffer is large, byteLength is usually 0.

I'll see how to add a unit test to cover this change.

Cheers,
Tony

@ghost
Copy link

ghost commented Aug 7, 2021

CLA assistant check
All CLA requirements met.

@buptliuhs
Copy link
Contributor Author

buptliuhs commented Aug 7, 2021

@nikithauc I had to move some code out of the create method to make the change testable. Hope it is okay.

@buptliuhs
Copy link
Contributor Author

hi @nikithauc , are you happy with the change?

nikithauc
nikithauc previously approved these changes Aug 10, 2021
@nikithauc nikithauc changed the base branch from v2/dev to dev August 10, 2021 22:54
@nikithauc nikithauc dismissed their stale review August 10, 2021 22:54

The base branch was changed.

@nikithauc
Copy link
Contributor

@buptliuhs the changes look good. I changed the base to dev for this PR.

Can you please resolve this?

I should have noticed the base setting before. I apologize for the inconvenience.

nikithauc
nikithauc previously approved these changes Aug 10, 2021
@buptliuhs
Copy link
Contributor Author

buptliuhs commented Aug 11, 2021

Hi @nikithauc, I was initially looking for a fix in v2, which branch should the change be based on? I can create another PR to propagate the fix to dev too to fix the same issue in v3.

@buptliuhs buptliuhs requested a review from nikithauc August 11, 2021 11:35
@nikithauc
Copy link
Contributor

@buptliuhs Can you please branch off dev? The fix will be reflected in 3.x versions.

@buptliuhs buptliuhs force-pushed the v2-fix-wrong-buffer-size-calculation branch from 6fca47e to ca737ac Compare August 11, 2021 20:42
@buptliuhs
Copy link
Contributor Author

buptliuhs commented Aug 11, 2021

Hi @nikithauc , I've done the rebase on the branch and now the branch is based on dev. Please review.
There are some auto-changes as following, hope they are okay:

  • package-lock.json: auto-updated when running npm install
  • samples/*: auto-updated when running git commit

Again, I'm also looking for the same fix on 2.x since 3.x is still in the preview stage. Is 2.x still in maintenance mode? If yes, which branch should I target? Thanks!

Cheers,
Tony

@nikithauc
Copy link
Contributor

@buptliuhs 3.0.0 is released - https://www.npmjs.com/package/@microsoft/microsoft-graph-client/v/3.0.0. 2.x version will no longer be seeing any changes.

@buptliuhs buptliuhs force-pushed the v2-fix-wrong-buffer-size-calculation branch from ca737ac to bf5ef40 Compare August 12, 2021 02:16
@buptliuhs buptliuhs force-pushed the v2-fix-wrong-buffer-size-calculation branch from 492b0a7 to 5487143 Compare August 12, 2021 02:25
@buptliuhs
Copy link
Contributor Author

Just did a final tidy-up.

@nikithauc
Copy link
Contributor

@buptliuhs Thank you for this contribution! Merging this right now :)

@nikithauc nikithauc merged commit e17274b into microsoftgraph:dev Aug 12, 2021
@buptliuhs buptliuhs deleted the v2-fix-wrong-buffer-size-calculation branch August 12, 2021 02:52
@buptliuhs
Copy link
Contributor Author

@nikithauc hello again! Do we have a plan for a new release, e.g. 3.0.1, to include the fixes for 3.0.0? Thanks!

@nikithauc
Copy link
Contributor

@buptliuhs yes. Please expect 3.0.1 soon

@nikithauc nikithauc mentioned this pull request Nov 15, 2021
@nikithauc
Copy link
Contributor

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