Skip to content

fix: ModuleNotFoundError caused by distributed race condition in ci/cd test - #45787

Closed
rigen1048 wants to merge 3 commits into
huggingface:mainfrom
rigen1048:bug_fix
Closed

rigen1048 wants to merge 3 commits into
huggingface:mainfrom
rigen1048:bug_fix

Conversation

@rigen1048

@rigen1048 rigen1048 commented May 5, 2026 •

Copy link
Copy Markdown
Contributor

What does this PR do?

This PR fixes a distributed race condition in dynamic_module_utils.py that causes intermittent ModuleNotFoundError: No module named 'transformers_modules' failures when running tests with pytest-xdist, ffaced in #45687 pr ci/cd test

Root Cause

When running in parallel (e.g., -n 8), each worker process has an isolated sys.path.

Worker A downloads a dynamic model and initializes the cache (init_hf_modules()), updating its own sys.path.
Worker B requires the same model, sees the files already exist on disk, and skips initialization.
Worker B fails to import the model because its sys.path does not contain the cache directory, preventing it from resolving the transformers_modules namespace used in relative imports.

fix:

I have added a call to init_hf_modules() at the start of get_class_in_module. This ensures every worker process correctly configures its environment before attempting an import, regardless of which worker performed the initial download.

Code Agent Policy

The Transformers repo is currently being overwhelmed by a large number of PRs and issue comments written by
code agents. We are currently bottlenecked by our ability to review and respond to them. As a result,
we ask that new users do not submit pure code agent PRs at this time.
You may use code agents in drafting or to help you diagnose issues. We'd also ask autonomous "OpenClaw"-like agents
not to open any PRs or issues for the moment.

PRs that appear to be fully agent-written will probably be closed without review, and we may block users who do this
repeatedly or maliciously.

This is a rapidly-evolving situation that's causing significant shockwaves in the open-source community. As a result,
this policy is likely to be updated regularly in the near future. For more information, please read CONTRIBUTING.md.

  • [X ] I confirm that this is not a pure code agent PR.
  • Ai was used to understand the problem, create a reproducible script & bring clarity in the pr

Before submitting

  • This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
  • Did you read the contributor guideline,
    Pull Request section?
  • Was this discussed/approved via a Github issue or the forum? Please add a link
    to it if that's the case.
  • Did you make sure to update the documentation with your changes? Here are the
    documentation guidelines, and
    here are tips on formatting docstrings.
  • Did you write any new necessary tests?

Who can review?

Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.

@Rocketknight1
@Cyrilvallez

@Rocketknight1

Copy link
Copy Markdown
Member

cc @ydshieh @tarekziade - I'm not sure we want to modify the actual code for this, but maybe there's some way we can fix it in the tests?

@tarekziade

Copy link
Copy Markdown
Collaborator

I don't think this change will help, this call is a no-op for the worker once sys.path is updated, the real bug is the conditional invalidate_caches()

The cleanest place to neutralize this is conftest.py because it's loaded once per xdist worker process before any test runs, so we can pre-warm each worker's environment.

so we could call :

from transformers.dynamic_module_utils import init_hf_modules
import importlib

init_hf_modules()
importlib.invalidate_caches()

at the top of conftest

WDYT @ydshieh

@rigen1048

Copy link
Copy Markdown
Contributor Author

@tarekziade Thanks for the valuable feedback. My original solution worked, but it seems it violates architectural purity. I tested your suggestion against my reproducible script/test and it worked well.
Thanks to @Rocketknight1 for forwarding the pr to the right people. We should wait for @ydshieh ’s insight on this — it can help us move us forward to the right direction

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.

3 participants