[BUG FIX]: hf_hub_download crashes when stderr lacks a real file descriptor#4065
Open
tobocop2 wants to merge 3 commits intohuggingface:mainfrom
Open
[BUG FIX]: hf_hub_download crashes when stderr lacks a real file descriptor#4065tobocop2 wants to merge 3 commits intohuggingface:mainfrom
tobocop2 wants to merge 3 commits intohuggingface:mainfrom
Conversation
f08d2e6 to
6eff9fb
Compare
This was referenced Apr 7, 2026
6f4b2a4 to
43399d3
Compare
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit 59aa035. Configure here.
98872dc to
cf1615c
Compare
Add _SafeTqdm fallback with thread lock for environments where stderr.fileno() is invalid (Textual, Jupyter, pytest, WSGI, etc.). Wrap progress bar construction in try/except to degrade gracefully instead of crashing the download.
cf1615c to
037f4ad
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Problem
hf_hub_downloadcrashes withValueError: bad value(s) in fds_to_keepin any environment wheresys.stderris not backed by a real file descriptor.The download itself is fine. Only tqdm's progress bar initialization fails, but because
_create_progress_barhas no safety net, it takes down the entire download.Affected environments
Any runtime where
sys.stderris not backed by a real file descriptor:Root cause
_create_progress_barcallscls(disable=disable, name=name, **kwargs)with no exception handling. When stderr'sfileno()returns-1, tqdm's__init__tries to create a multiprocessing lock, which spawns a resource tracker subprocess viafork_exec(), which rejects the invalid fd.Crash Demo
Fix
Add a
_SafeTqdmsubclass that uses athreading.RLockinstead of a multiprocessing lock, and wrap bothcls()calls in_create_progress_barwithtry/except (OSError, ValueError)to fall back to_SafeTqdm.Fixes #4066
Note
Low Risk
Change is limited to progress-bar creation error handling and test coverage; it only activates on
tqdminitialization failures and otherwise preserves existing behavior.Overview
Prevents downloads from crashing when
tqdmprogress bar initialization fails (notably whensys.stderrlacks a valid file descriptor) by wrapping progress-bar construction intry/exceptand falling back to a disabled_SafeTqdmthat uses a thread lock.Adds a user warning for the default HF
tqdmpath when progress reporting is unavailable, and extends tests to cover the bad-filenoscenario and verify that both custom and HFtqdm_classfailures degrade todisable=True(without emitting pytest unraisable destructor warnings).Reviewed by Cursor Bugbot for commit 2e89815. Bugbot is set up for automated code reviews on this repo. Configure here.