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

fix(renterd): first file download no bucket error #853

Merged

Conversation

alexfreska
Copy link
Member

@alexfreska alexfreska commented Dec 12, 2024

  • Fixed an issue where the first attempt to download a file would show a bucket not found error.

Notes

  • Strange issue where buckets.data is still undefined in the closure even though the data has been fetched. Adding useCallback does the trick, oddly even just console logging buckets.data above downloadFiles fixes the issue.

Copy link

vercel bot commented Dec 12, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
renterd ✅ Ready (Inspect) Visit Preview 💬 Add feedback Dec 13, 2024 4:54pm
4 Skipped Deployments
Name Status Preview Comments Updated (UTC)
explorer ⬜️ Ignored (Inspect) Visit Preview Dec 13, 2024 4:54pm
explorer-zen ⬜️ Ignored (Inspect) Visit Preview Dec 13, 2024 4:54pm
hostd ⬜️ Ignored (Inspect) Visit Preview Dec 13, 2024 4:54pm
website ⬜️ Ignored (Inspect) Visit Preview Dec 13, 2024 4:54pm

Copy link

changeset-bot bot commented Dec 12, 2024

🦋 Changeset detected

Latest commit: 2bb17b5

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
renterd Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link
Member Author

alexfreska commented Dec 12, 2024

@alexfreska alexfreska marked this pull request as ready for review December 12, 2024 22:01
@alexfreska alexfreska force-pushed the 12-12-refactor_renterd_min_account_expiry_and_min_price_table_validity_units branch from 2d9acb0 to 0f8b3ab Compare December 12, 2024 22:19
@alexfreska alexfreska force-pushed the 12-12-fix_renterd_first_file_download_no_bucket_error branch from 089cf88 to a4690a8 Compare December 12, 2024 22:19
@alexfreska alexfreska mentioned this pull request Dec 12, 2024
@alexfreska alexfreska force-pushed the 12-12-refactor_renterd_min_account_expiry_and_min_price_table_validity_units branch from 0f8b3ab to d9c5154 Compare December 12, 2024 22:22
@alexfreska alexfreska force-pushed the 12-12-fix_renterd_first_file_download_no_bucket_error branch from a4690a8 to 4dd4474 Compare December 12, 2024 22:22
@alexfreska alexfreska force-pushed the 12-12-refactor_renterd_min_account_expiry_and_min_price_table_validity_units branch from d9c5154 to 1b892c0 Compare December 12, 2024 22:24
@alexfreska alexfreska force-pushed the 12-12-fix_renterd_first_file_download_no_bucket_error branch from 4dd4474 to d463b3a Compare December 12, 2024 22:24
Copy link
Contributor

@telestrial telestrial left a comment

Choose a reason for hiding this comment

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

Strange issue where buckets.data is still undefined in the closure even though the data has been fetched. Adding useCallback does the trick, oddly even just console logging buckets.data above downloadFiles fixes the issue.

I've been mulling this over a bit because it is an interesting issue! On its face, it could appear to treat a symptom and not the cause. That's what I initially thought, but after thinking a bit, here's what I believe is going on:

When you define a function like downloadFiles, it captures the state of buckets.data at that moment. React doesn’t create a new function reference on subsequent renders unless explicitly instructed to (as with useCallback), which is why you're seeing a stale reference. The closure retains the value of buckets.data as it was when the function was first defined, without updating automatically.

The reason console.log has the correct value is that it forces the runtime to evaluate the current value of buckets.data at the time the log statement is executed. So console.log() is the current render and downloadFiles is likely the first render.

All that said, I believe this is exactly the right answer to the problem. It's probably more of a use case for useCallback than most other common uses.

@alexfreska alexfreska force-pushed the 12-12-refactor_renterd_min_account_expiry_and_min_price_table_validity_units branch from 1b892c0 to 06dc7dc Compare December 13, 2024 15:16
@alexfreska alexfreska force-pushed the 12-12-fix_renterd_first_file_download_no_bucket_error branch from d463b3a to 3308af9 Compare December 13, 2024 15:16
Copy link
Member Author

alexfreska commented Dec 13, 2024

@telestrial it has to be something with a stale closure but when downloadFiles is not wrapped in useCallback React does create a new function reference every re-render, for example if I were to try to use it as a dependency for another hook deps you would get a warning like:

'downloadFiles' function makes the dependencies of useEffect Hook (at line 153) change on every render. To fix this, wrap the definition of 'downloadFiles' in its own useCallback() Hook.

The behaviour you describe should be when you do a useCallback with empty deps []. I am really not sure what is causing this - definitely an interesting Advent of Bugs problem haha.

Copy link
Member Author

Single test failing due to: SiaFoundation/renterd#1747

Copy link
Member Author

alexfreska commented Dec 13, 2024

Merge activity

  • Dec 13, 11:43 AM EST: A user started a stack merge that includes this pull request via Graphite.
  • Dec 13, 11:49 AM EST: Graphite rebased this pull request as part of a merge.
  • Dec 13, 11:50 AM EST: A user merged this pull request with Graphite.

@alexfreska alexfreska changed the base branch from 12-12-refactor_renterd_min_account_expiry_and_min_price_table_validity_units to graphite-base/853 December 13, 2024 16:45
@alexfreska alexfreska changed the base branch from graphite-base/853 to main December 13, 2024 16:47
@alexfreska alexfreska force-pushed the 12-12-fix_renterd_first_file_download_no_bucket_error branch from 3308af9 to 2bb17b5 Compare December 13, 2024 16:48
@alexfreska alexfreska merged commit 95f51d3 into main Dec 13, 2024
23 of 34 checks passed
@alexfreska alexfreska deleted the 12-12-fix_renterd_first_file_download_no_bucket_error branch December 13, 2024 16:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

4 participants