mirror of
https://github.com/jamiepine/voicebox.git
synced 2026-09-20 07:10:40 -07:00
Review feedback on the initial fix surfaced two real issues:
1. ``os.environ`` toggles alone don't flip offline mode.
``huggingface_hub.constants.HF_HUB_OFFLINE`` is read once at import
time into a module-level bool; ``transformers.utils.hub._is_offline_mode``
mirrors that bool at its own import time. The hot paths
(``_http._default_backend_factory`` in huggingface_hub,
``is_offline_mode`` in transformers) read the cached bools — not the
env — so mutating only ``os.environ`` was a no-op.
2. Race condition on concurrent inference. Two threads running inside
``force_offline_if_cached`` via ``asyncio.to_thread`` could have
thread A's ``finally`` strip thread B's offline protection mid-run.
Rewrite the helper to:
- mutate ``huggingface_hub.constants.HF_HUB_OFFLINE`` and
``transformers.utils.hub._is_offline_mode`` directly
- refcount concurrent users under a single ``threading.RLock`` so a
shared offline window is restored only when the last caller exits
- still write ``os.environ`` for anything that reads it dynamically
Also addresses the unused-variable ruff flag on the Whisper transcribe
path (``audio, sr`` → ``audio, _sr``).
New unit tests cover the cached-constant mutation, env propagation,
no-op on ``is_cached=False``, nested contexts, and a threaded race
where a slow thread must retain offline mode after a peer exits.
Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
110 lines
3.2 KiB
Python
110 lines
3.2 KiB
Python
"""
|
|
Unit tests for the ``force_offline_if_cached`` helper.
|
|
|
|
Verifies that the helper mutates the cached module constants in
|
|
``huggingface_hub.constants`` and ``transformers.utils.hub`` — not just
|
|
``os.environ`` — and that concurrent users are refcount-coordinated so
|
|
one thread's exit can't strip another thread's offline protection.
|
|
"""
|
|
|
|
import os
|
|
import sys
|
|
import threading
|
|
import time
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
sys.path.insert(0, str(Path(__file__).parent.parent))
|
|
|
|
from utils.hf_offline_patch import force_offline_if_cached # noqa: E402
|
|
|
|
|
|
def _hf_const():
|
|
import huggingface_hub.constants as hf_const
|
|
|
|
return hf_const
|
|
|
|
|
|
def _tf_hub():
|
|
import transformers.utils.hub as tf_hub
|
|
|
|
return tf_hub
|
|
|
|
|
|
def test_mutates_cached_huggingface_hub_constant():
|
|
original = _hf_const().HF_HUB_OFFLINE
|
|
with force_offline_if_cached(True, "t"):
|
|
assert _hf_const().HF_HUB_OFFLINE is True
|
|
assert _hf_const().HF_HUB_OFFLINE == original
|
|
|
|
|
|
def test_mutates_cached_transformers_constant():
|
|
original = _tf_hub()._is_offline_mode
|
|
with force_offline_if_cached(True, "t"):
|
|
assert _tf_hub()._is_offline_mode is True
|
|
assert _tf_hub()._is_offline_mode == original
|
|
|
|
|
|
def test_sets_env_variable():
|
|
original = os.environ.get("HF_HUB_OFFLINE")
|
|
with force_offline_if_cached(True, "t"):
|
|
assert os.environ.get("HF_HUB_OFFLINE") == "1"
|
|
assert os.environ.get("HF_HUB_OFFLINE") == original
|
|
|
|
|
|
def test_noop_when_not_cached():
|
|
before = _hf_const().HF_HUB_OFFLINE
|
|
with force_offline_if_cached(False, "t"):
|
|
assert _hf_const().HF_HUB_OFFLINE == before
|
|
|
|
|
|
def test_nested_contexts_respect_refcount():
|
|
original = _hf_const().HF_HUB_OFFLINE
|
|
with force_offline_if_cached(True, "outer"):
|
|
assert _hf_const().HF_HUB_OFFLINE is True
|
|
with force_offline_if_cached(True, "inner"):
|
|
assert _hf_const().HF_HUB_OFFLINE is True
|
|
# inner exit must not restore while outer is still active
|
|
assert _hf_const().HF_HUB_OFFLINE is True
|
|
assert _hf_const().HF_HUB_OFFLINE == original
|
|
|
|
|
|
def test_concurrent_threads_share_offline_window():
|
|
"""A slow thread must keep seeing offline mode even if a peer exits first."""
|
|
original = _hf_const().HF_HUB_OFFLINE
|
|
observations: list[bool] = []
|
|
errors: list[Exception] = []
|
|
barrier = threading.Barrier(2)
|
|
|
|
def slow():
|
|
try:
|
|
with force_offline_if_cached(True, "slow"):
|
|
barrier.wait() # sync with fast
|
|
time.sleep(0.15) # fast will exit during this sleep
|
|
observations.append(_hf_const().HF_HUB_OFFLINE)
|
|
except Exception as exc: # noqa: BLE001
|
|
errors.append(exc)
|
|
|
|
def fast():
|
|
try:
|
|
with force_offline_if_cached(True, "fast"):
|
|
barrier.wait()
|
|
except Exception as exc: # noqa: BLE001
|
|
errors.append(exc)
|
|
|
|
t_slow = threading.Thread(target=slow)
|
|
t_fast = threading.Thread(target=fast)
|
|
t_slow.start()
|
|
t_fast.start()
|
|
t_slow.join(timeout=5)
|
|
t_fast.join(timeout=5)
|
|
|
|
assert not errors, errors
|
|
assert observations == [True], "slow thread lost offline protection"
|
|
assert _hf_const().HF_HUB_OFFLINE == original
|
|
|
|
|
|
if __name__ == "__main__":
|
|
pytest.main([__file__, "-v"])
|