Skip to content

Commit fab2e85

Browse files
The-Obstacle-Is-The-WayCodeRabbit
andcommitted
refactor: improve _wait_for_repo_ready per CodeRabbit review
Improvements: - Add comprehensive docstring explaining the race condition fix - Use time.monotonic() for stricter wall-clock timing - Narrow exception handling to HfHubHTTPError instead of bare Exception Co-authored-by: CodeRabbit <support@coderabbit.ai>
1 parent b6490dc commit fab2e85

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

tests/test_upstream_hub.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -268,15 +268,30 @@ def test_push_dataset_dict_to_hub_with_multiple_commits(self, temporary_repo):
268268
assert num_commits_after_push - num_commits_before_push > 1
269269

270270
def _wait_for_repo_ready(self, repo_id, max_wait=30):
271-
"""Wait for repository to be in a consistent state."""
272-
for _ in range(max_wait):
271+
"""Wait for repository to be in a consistent state after push operations.
272+
273+
This helper addresses race conditions where rapid successive push_to_hub calls
274+
don't wait for Hub's LFS object propagation between pushes, causing errors like:
275+
"LFS pointer pointed to a file that does not exist"
276+
277+
Args:
278+
repo_id: The repository ID to check.
279+
max_wait: Maximum time in seconds to wait for repository readiness.
280+
281+
Raises:
282+
TimeoutError: If repository is not ready within max_wait seconds.
283+
"""
284+
from huggingface_hub.errors import HfHubHTTPError
285+
286+
start_time = time.monotonic()
287+
while (time.monotonic() - start_time) < max_wait:
273288
try:
274289
# Verify we can list files (repo is consistent)
275290
self._api.list_repo_files(repo_id, repo_type="dataset", token=self._token)
276-
# Small delay to ensure LFS objects are propagated
291+
# Small delay to ensure LFS objects are fully propagated
277292
time.sleep(1)
278293
return
279-
except Exception:
294+
except HfHubHTTPError:
280295
time.sleep(1)
281296
raise TimeoutError(f"Repository {repo_id} not ready after {max_wait}s")
282297

0 commit comments

Comments
 (0)