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 thread-safety when fetching user secret in Google Colab #1953

Merged
merged 4 commits into from
Jan 5, 2024

Conversation

Wauplin
Copy link
Contributor

@Wauplin Wauplin commented Jan 4, 2024

Should fix #1952.

It looks like google.colab.userdata uses blocking_request under the hood which states

Note: this function is not thread safe, e.g. if two threads
send blocking_request they will likely race with each other and consume
each other responses leaving another thread deadlocked.

I'm not 100% sure this is what caused the issue experienced by @prhbrt in #1952 but in any case it's better to fix it. The only change in this PR is to put the userdata.get("HF_TOKEN") call inside a global lock. This shouldn't impact much the performances/user experience given it's supposed to be much much quicker than uploading/downloading files. => I'm now sure this is the problem. More details in #1953 (comment).

@Wauplin Wauplin requested a review from LysandreJik January 4, 2024 15:47
@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

Copy link

codecov bot commented Jan 4, 2024

Codecov Report

Attention: 22 lines in your changes are missing coverage. Please review.

Comparison is base (a331e82) 82.15% compared to head (d8794e3) 82.14%.
Report is 1 commits behind head on main.

Files Patch % Lines
src/huggingface_hub/utils/_token.py 18.51% 22 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1953      +/-   ##
==========================================
- Coverage   82.15%   82.14%   -0.02%     
==========================================
  Files          66       66              
  Lines        8133     8138       +5     
==========================================
+ Hits         6682     6685       +3     
- Misses       1451     1453       +2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@not-lain
Copy link
Contributor

not-lain commented Jan 4, 2024

this fixed it for me, thank youuu

Copy link
Member

@julien-c julien-c left a comment

Choose a reason for hiding this comment

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

might be worth asking the Colab team for a super quick review?

@Wauplin
Copy link
Contributor Author

Wauplin commented Jan 5, 2024

might be worth asking the Colab team for a super quick review?

Yes we're in contact by email and slack about this specific issue (and PR).

@Wauplin
Copy link
Contributor Author

Wauplin commented Jan 5, 2024

So good news, I have been able to consistently reproduce the deadlock issue reported in #1944, #1952, huggingface/diffusers#6431, googlecolab/colabtools#4272, huggingface/diffusers#6441 and huggingface/diffusers#6446. I tested installing huggingface_hub from this branch and I can confirm that it fixed the issue.

In practice, this PR:

  • adds a global lock to avoid concurrency issues when using userdata.get
  • adds a global variable to store the google token at runtime to avoid requesting the vault on each and every request (from a suggestion from the Google Colab team)

Once this PR is approved and merged, I'll proceed with a hot-fix release (v0.20.2).


Temporary fix

In the meantime, anyone facing this deadlock issue can fix it by installing a different version of huggingface_hub. It can be either:

  • from latest stable version: !pip install huggingface_hub==0.19.4. This version won't read the token from the vault at all (=> meaning you still need to login manually)
  • from source: !pip install git+https://github.com/huggingface/huggingface_hub@1952-fix-google-colab-thread-safety => This version will read the HF_TOKEN secret automatically and in a thread-safe way
  • EDIT: hot-fix has been shipped in v0.20.2. Please upgrade huggingface_hub version to fix the deadlock issue:
# run before executing any code
!pip install -U huggingface_hub

Note: you must install huggingface_hub before importing/calling any other method. By default huggingface_hub is already installed in colab notebooks but with version 0.20.1 which is the one with concurrency issues. Once 0.20.2 will be released, the plan is to make it the default in colab notebooks. EDIT: 0.20.2 has been released.

Copy link
Contributor

@osanseviero osanseviero left a comment

Choose a reason for hiding this comment

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

Thanks!

src/huggingface_hub/utils/_token.py Outdated Show resolved Hide resolved
Co-authored-by: Omar Sanseviero <osanseviero@gmail.com>
@Wauplin
Copy link
Contributor Author

Wauplin commented Jan 5, 2024

Thanks for the review @osanseviero!

@Wauplin Wauplin merged commit 36ad912 into main Jan 5, 2024
15 of 16 checks passed
@Wauplin Wauplin deleted the 1952-fix-google-colab-thread-safety branch January 5, 2024 10:38
Wauplin added a commit that referenced this pull request Jan 5, 2024
* Fix thread-safety when fetching user secret

* unused var

* request secret only once

* Update src/huggingface_hub/utils/_token.py

Co-authored-by: Omar Sanseviero <osanseviero@gmail.com>

---------

Co-authored-by: Omar Sanseviero <osanseviero@gmail.com>
@Wauplin
Copy link
Contributor Author

Wauplin commented Jan 5, 2024

Update: hot-fix release 0.20.2 is now published (see release notes). The plan is to make this the default in google colab. In the meantime, upgrade huggingface_hub to latest version to fix the deadlock issue:

# run before executing any code
!pip install -U huggingface_hub

@Wauplin
Copy link
Contributor Author

Wauplin commented Jan 12, 2024

Update: huggingface_hub 0.20.2 is now the default version on Google Colab, meaning this issue should be fixed for everyone without having to upgrade (see googlecolab/colabtools#4272 (comment)).

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.

userdata.get('HF_TOKEN') hangs forever on Google Colab
5 participants