Spaces:
Sleeping
Sleeping
Harden saves: retry on commit conflict so answers are never dropped
Browse files
app.py
CHANGED
|
@@ -496,6 +496,9 @@ def _reset_save_baseline(participant_file: str, count: int) -> None:
|
|
| 496 |
entry["saved_count"] = count
|
| 497 |
|
| 498 |
|
|
|
|
|
|
|
|
|
|
| 499 |
def _save_results(participant_file: str, results: list[dict]) -> None:
|
| 500 |
if not HF_TOKEN or not results:
|
| 501 |
return
|
|
@@ -507,21 +510,34 @@ def _save_results(participant_file: str, results: list[dict]) -> None:
|
|
| 507 |
if len(snapshot) <= entry["saved_count"]:
|
| 508 |
return
|
| 509 |
frame = pd.DataFrame(snapshot, columns=RESULTS_COLUMNS)
|
| 510 |
-
|
| 511 |
-
|
| 512 |
-
|
| 513 |
-
|
| 514 |
-
|
| 515 |
-
|
| 516 |
-
|
| 517 |
-
|
| 518 |
-
|
| 519 |
-
|
| 520 |
-
|
| 521 |
-
|
| 522 |
-
|
| 523 |
-
|
| 524 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 525 |
|
| 526 |
|
| 527 |
# ---------------------------------------------------------------------------
|
|
|
|
| 496 |
entry["saved_count"] = count
|
| 497 |
|
| 498 |
|
| 499 |
+
_SAVE_MAX_RETRIES = 6
|
| 500 |
+
|
| 501 |
+
|
| 502 |
def _save_results(participant_file: str, results: list[dict]) -> None:
|
| 503 |
if not HF_TOKEN or not results:
|
| 504 |
return
|
|
|
|
| 510 |
if len(snapshot) <= entry["saved_count"]:
|
| 511 |
return
|
| 512 |
frame = pd.DataFrame(snapshot, columns=RESULTS_COLUMNS)
|
| 513 |
+
csv_bytes = frame.to_csv(index=False).encode()
|
| 514 |
+
|
| 515 |
+
# Different participants commit to the same repo concurrently, so an
|
| 516 |
+
# individual upload can still be rejected with a revision conflict.
|
| 517 |
+
# Retry with backoff so no answer is silently dropped (this was the
|
| 518 |
+
# original data-loss bug: conflicts were swallowed and never retried).
|
| 519 |
+
for attempt in range(_SAVE_MAX_RETRIES):
|
| 520 |
+
try:
|
| 521 |
+
api.upload_file(
|
| 522 |
+
path_or_fileobj=io.BytesIO(csv_bytes),
|
| 523 |
+
path_in_repo=participant_file,
|
| 524 |
+
repo_id=RESULTS_REPO,
|
| 525 |
+
repo_type="dataset",
|
| 526 |
+
commit_message=f"Update {participant_file} (n={len(snapshot)})",
|
| 527 |
+
)
|
| 528 |
+
entry["saved_count"] = len(snapshot)
|
| 529 |
+
return
|
| 530 |
+
except Exception as exc: # noqa: BLE001
|
| 531 |
+
wait = 0.5 * (2**attempt) + random.uniform(0, 0.4)
|
| 532 |
+
print(
|
| 533 |
+
f"[save] upload attempt {attempt + 1}/{_SAVE_MAX_RETRIES} "
|
| 534 |
+
f"failed for {participant_file} ({exc}); retrying in {wait:.1f}s."
|
| 535 |
+
)
|
| 536 |
+
time.sleep(wait)
|
| 537 |
+
print(
|
| 538 |
+
f"[save] ERROR: gave up saving {participant_file} after "
|
| 539 |
+
f"{_SAVE_MAX_RETRIES} attempts (n={len(snapshot)})."
|
| 540 |
+
)
|
| 541 |
|
| 542 |
|
| 543 |
# ---------------------------------------------------------------------------
|