pmadinei commited on
Commit
383b834
·
verified ·
1 Parent(s): d9f7d07

Harden saves: retry on commit conflict so answers are never dropped

Browse files
Files changed (1) hide show
  1. app.py +31 -15
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
- buf = io.BytesIO()
511
- frame.to_csv(buf, index=False)
512
- buf.seek(0)
513
- try:
514
- api.upload_file(
515
- path_or_fileobj=buf,
516
- path_in_repo=participant_file,
517
- repo_id=RESULTS_REPO,
518
- repo_type="dataset",
519
- commit_message=f"Update {participant_file} (n={len(snapshot)})",
520
- )
521
- except Exception as exc: # noqa: BLE001
522
- print(f"[save] WARNING: upload failed for {participant_file} ({exc}).")
523
- return
524
- entry["saved_count"] = len(snapshot)
 
 
 
 
 
 
 
 
 
 
 
 
 
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
  # ---------------------------------------------------------------------------