Skip to content

Reject an hf_xet that is only an empty namespace package - #11205

Merged
danielhanchen merged 2 commits into
mainfrom
fix-hf-xet-namespace-shell
Sep 17, 2026
Merged

danielhanchen merged 2 commits into
mainfrom
fix-hf-xet-namespace-shell

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Follow-up to #11167, from a Codex item on that PR that arrived in the same minute it merged, so it never got triaged. It is a real gap and it is on main now.

The hole

An hf_xet/ directory with no __init__.py and no compiled extension in it, sitting beside an intact hf_xet-*.dist-info, is a namespace package. That is a reachable shape: an interrupted pip uninstall, an antivirus quarantine of the .pyd, a partially synced network or OneDrive folder.

fix_broken_hf_xet_wheel got the suspicion right (the extension really is missing) and then threw it away. The confirm step imported the package, a namespace package always imports, so the guard cleared itself and did nothing.

huggingface_hub never imports the package. It imports names out of it:

from hf_xet import PyXetDownloadInfo, download_files   # file_download.py

so is_xet_available() stayed True and every download still went to Xet.

Reproduced

Against huggingface_hub 0.36.2 in a throwaway venv, package contents deleted and the directory left behind:

find_spec            namespace
extension_is_missing True
bare import          succeeds
env                  (unset)
routes_to_xet        True
user_error           ImportError: cannot import name 'PyXetDownloadInfo' from 'hf_xet' (unknown location)

which is precisely the misleading failure #11167 exists to prevent.

The fix

A namespace package has __file__ None and carries no code, so requiring a real __file__ separates it from an installed package without pinning a symbol list that moves between huggingface_hub versions. The shell is also dropped from sys.modules rather than left for the next importer.

Same scenario after:

env                  1
routes_to_xet        False

Nothing else moved

The no-fire scenarios were re-run against a real huggingface_hub, not mocks:

scenario routes to Xet fix fired
healthy yes no
wheel tag says another cpu, extension loads yes no
hf_xet already imported yes no
hf_xet absent no no

78 tests pass. test_fires_when_hf_xet_is_only_an_empty_namespace_package fails with the change reverted.

One test-helper correction rides along: the stand-in for a successful import returned a bare object(), which has no __file__, so it modelled the very shell the confirm step now rejects. It returns a module now, which is what a real import gives.

What I could not prove

  • No Windows on ARM box here; this scenario is filesystem shaped, not architecture shaped, so it reproduces identically on Linux, but the ARM64 case that motivated Fall back to HTTPS when hf_xet is installed but cannot be imported #11167 is still only modelled.
  • huggingface_hub 1.x is not covered; measured on 0.36.2.
  • No file went on the wire. The check is huggingface_hub's own routing question, is_xet_available() and not constants.HF_HUB_DISABLE_XET, plus its real from hf_xet import ....

Follow-up to #11167. An hf_xet/ directory with no __init__.py and no extension
in it, beside an intact hf_xet-*.dist-info, is a NAMESPACE package. The
suspicion was raised correctly (the extension really is missing), but the
confirm step imported the package, that import succeeded because a namespace
package always does, and the fix cleared itself and did nothing.

huggingface_hub does not import the package, it imports names out of it
(from hf_xet import PyXetDownloadInfo, download_files), so it still routed
every download to Xet. Reproduced against huggingface_hub 0.36.2 with the
package contents deleted and the directory left behind:

    find_spec            namespace
    extension_is_missing True
    bare import          succeeds
    routes_to_xet        True
    user_error           ImportError: cannot import name 'PyXetDownloadInfo'
                         from 'hf_xet' (unknown location)

which is the exact misleading failure the original change exists to prevent.

A namespace package has __file__ None and carries no code, so requiring a real
__file__ is enough to tell it from an installed one, without pinning a symbol
list that moves between huggingface_hub versions. Same scenario after:
routes_to_xet False, and the shell is dropped from sys.modules rather than left
for the next importer.

The healthy, wrong-tag, already-imported and absent scenarios were re-run
against a real huggingface_hub and are unchanged. The stand-in for a successful
import in the test helper returned a bare object(), which has no __file__; it
now returns a module, which is what a real import gives.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 17, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-09-17T13:55:40.479724Z 4f06ced Manual request
🔒 Security Review ✅ Completed 2026-09-17T13:13:01.787944Z 9e050bf PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 9e050bfe66

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread unsloth/import_fixes.py
Comment on lines +1682 to +1684
module = importlib.import_module("hf_xet")
if getattr(module, "__file__", None) is not None:
return

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Validate a cached namespace package before returning

When the broken namespace package is already cached in sys.modules, this new validation is never reached because the earlier if "hf_xet" in sys.modules: return still assumes every cached module is functional. This occurs naturally if a Hugging Face download previously executed from hf_xet import ...: the missing-symbol import fails but leaves the successfully imported namespace shell cached. Importing Unsloth afterward therefore leaves Xet enabled, and subsequent downloads continue to fail. Apply the __file__ check to the cached module before short-circuiting as well.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Genuine, and the mechanism is exactly as you describe. Reproduced before fixing, with the Hub used before unsloth on huggingface_hub 0.36.2:

hub attempt              ImportError: cannot import name 'PyXetDownloadInfo' from 'hf_xet' (unknown location)
shell_left_in_sys_modules True
shell __file__            None
env                       (unset)
routes_to_xet             True

from hf_xet import ... leaves the package cached because the package import itself succeeded and only the attribute lookup failed, so the presence of a cached module proved nothing. Fixed in 4f06ced: the cached object now gets the same test as a freshly imported one, and the shell is evicted rather than trusted. Same scenario after: env 1, routes_to_xet False.

Both call sites share one _hf_xet_module_is_real helper so they cannot drift apart.

Worth recording that this also exposed a bad stand-in in my own test: test_an_imported_hf_xet_short_circuits_before_any_lookup used a bare types.ModuleType, which has no __file__ and so was modelling the very shell this rejects. It failed when the check went in, correctly, and now models a real module. The healthy, wrong-tag, already-imported and absent scenarios were re-run against a real huggingface_hub and are unchanged; 79 tests pass, and the new one fails with the short-circuit restored.

danielhanchen added a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Sep 17, 2026
huggingface_hub's own `from hf_xet import PyXetDownloadInfo, download_files`
leaves hf_xet in sys.modules even when those symbols are missing: the PACKAGE
imported fine and only the attribute lookup failed. So for a user whose first
download happens before `import unsloth` (a notebook cell ordering, or any
script that reaches the Hub first), the shell was already cached and the
`if "hf_xet" in sys.modules: return` short circuit skipped every check.

Reproduced against huggingface_hub 0.36.2, Hub used first:

    hub attempt   ImportError: cannot import name 'PyXetDownloadInfo'
                  from 'hf_xet' (unknown location)
    cached        True, __file__ None
    env           (unset)
    routes_to_xet True

A cached module is not proof of a working one, so the cached object gets the
same test as a freshly imported one and the shell is evicted rather than
trusted. Same scenario after: env 1, routes_to_xet False.

The test that pins the short circuit used a bare types.ModuleType as its stand
in for a working hf_xet, which has no __file__ and is therefore exactly the
shell this rejects; it now models a real module. The healthy, wrong-tag,
already-imported and absent scenarios were re-run against a real
huggingface_hub and are unchanged.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Breezy!

Reviewed commit: 4f06ceddd3

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen
danielhanchen merged commit 0175e7a into main Sep 17, 2026
3 of 29 checks passed
@danielhanchen
danielhanchen deleted the fix-hf-xet-namespace-shell branch September 17, 2026 14:03
shaojun pushed a commit to shaojun/unsloth that referenced this pull request Sep 29, 2026
…1205)

* Reject an hf_xet that is only an empty namespace package

Follow-up to unslothai#11167. An hf_xet/ directory with no __init__.py and no extension
in it, beside an intact hf_xet-*.dist-info, is a NAMESPACE package. The
suspicion was raised correctly (the extension really is missing), but the
confirm step imported the package, that import succeeded because a namespace
package always does, and the fix cleared itself and did nothing.

huggingface_hub does not import the package, it imports names out of it
(from hf_xet import PyXetDownloadInfo, download_files), so it still routed
every download to Xet. Reproduced against huggingface_hub 0.36.2 with the
package contents deleted and the directory left behind:

    find_spec            namespace
    extension_is_missing True
    bare import          succeeds
    routes_to_xet        True
    user_error           ImportError: cannot import name 'PyXetDownloadInfo'
                         from 'hf_xet' (unknown location)

which is the exact misleading failure the original change exists to prevent.

A namespace package has __file__ None and carries no code, so requiring a real
__file__ is enough to tell it from an installed one, without pinning a symbol
list that moves between huggingface_hub versions. Same scenario after:
routes_to_xet False, and the shell is dropped from sys.modules rather than left
for the next importer.

The healthy, wrong-tag, already-imported and absent scenarios were re-run
against a real huggingface_hub and are unchanged. The stand-in for a successful
import in the test helper returned a bare object(), which has no __file__; it
now returns a module, which is what a real import gives.

* Check a cached hf_xet too, not just a freshly imported one

huggingface_hub's own `from hf_xet import PyXetDownloadInfo, download_files`
leaves hf_xet in sys.modules even when those symbols are missing: the PACKAGE
imported fine and only the attribute lookup failed. So for a user whose first
download happens before `import unsloth` (a notebook cell ordering, or any
script that reaches the Hub first), the shell was already cached and the
`if "hf_xet" in sys.modules: return` short circuit skipped every check.

Reproduced against huggingface_hub 0.36.2, Hub used first:

    hub attempt   ImportError: cannot import name 'PyXetDownloadInfo'
                  from 'hf_xet' (unknown location)
    cached        True, __file__ None
    env           (unset)
    routes_to_xet True

A cached module is not proof of a working one, so the cached object gets the
same test as a freshly imported one and the shell is evicted rather than
trusted. Same scenario after: env 1, routes_to_xet False.

The test that pins the short circuit used a bare types.ModuleType as its stand
in for a working hf_xet, which has no __file__ and is therefore exactly the
shell this rejects; it now models a real module. The healthy, wrong-tag,
already-imported and absent scenarios were re-run against a real
huggingface_hub and are unchanged.
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.

1 participant