Skip to content

Commit

Permalink
lf.eval.v2: Handle potential race condition when saving 'index.html'.
Browse files Browse the repository at this point in the history
There might be race conditions when 'index.html` is written by the background thread and from the thread that completes an example.

PiperOrigin-RevId: 711879944
  • Loading branch information
daiyip authored and langfun authors committed Jan 4, 2025
1 parent 8994735 commit bc4c7dd
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions langfun/core/eval/v2/reporting.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ def _on_bound(self):
self._update_thread = None
self._stop_update = False
self._stop_update_experiment_ids = set()
self._experiment_index_lock = None

def on_run_start(
self,
Expand All @@ -64,6 +65,7 @@ def on_run_start(
self._update_thread = threading.Thread(
target=self._update_thread_func, args=(runner,)
)
self._experiment_index_lock = {}
self._update_thread.start()

def on_run_complete(
Expand Down Expand Up @@ -100,6 +102,7 @@ def on_experiment_start(
experiment: Experiment
) -> None:
if experiment.is_leaf:
self._experiment_index_lock[experiment.id] = threading.Lock()
self._maybe_update_experiment_html(runner, experiment)

def on_experiment_complete(
Expand Down Expand Up @@ -170,7 +173,8 @@ def _save():
card_view=False,
),
)
html.save(index_html_path)
with self._experiment_index_lock[experiment.id]:
html.save(index_html_path)
experiment.info(
f'Updated {index_html_path!r} in {t.elapse:.2f} seconds.',
)
Expand All @@ -185,11 +189,11 @@ def _save():
time.time() - self._last_experiment_report_time[experiment.id]
> self.experiment_report_interval
):
self._last_experiment_report_time[experiment.id] = time.time()
if background:
runner.background_run(_save)
else:
_save()
self._last_experiment_report_time[experiment.id] = time.time()

def _save_example_html(
self, runner: Runner, experiment: Experiment, example: Example
Expand Down

0 comments on commit bc4c7dd

Please sign in to comment.