Skip to content

fix(backend): don't overwrite colliding VAD segment exports from distinct sync sources (#20686) - #20936

Merged
kodjima33 merged 1 commit into
mainfrom
watchdog/issue-20686-sync-vad-timestamp-collision
Oct 8, 2026
Merged

kodjima33 merged 1 commit into
mainfrom
watchdog/issue-20686-sync-vad-timestamp-collision

Conversation

@kodjima33

@kodjima33 kodjima33 commented Oct 8, 2026 •

Copy link
Copy Markdown
Collaborator

Bug

backend/utils/sync/pipeline.py::retrieve_vad_segments() names each exported
VAD speech segment <path_dir>/<capture_start + vad_offset>.wav. Two distinct
source WAL files uploaded in the same sync request can independently compute
the same absolute timestamp (e.g. source A starts at T with speech at
[1,3), source B starts at T+1 with speech at [0,4) — both resolve to
T+1.wav). routers/sync.py::sync_local_files runs retrieve_vad_segments
for every source file concurrently via asyncio.gather over one shared
segmented_paths set with no lock, so the second export silently overwrites
the first's audio on disk before transcription ever sees it — permanent,
undetected data loss. (#20686)

Root cause

No collision check existed before segment_aseg.export(segment_path, ...),
and the one shared mutable set these concurrent workers write into
(segmented_paths) was never synchronized in sync_local_files's call site.

Fix

  • retrieve_vad_segments: reserve the segment path in segmented_paths
    (under segment_source_lock, falling back to the existing
    contextlib.nullcontext() when no lock is supplied — same pattern already
    used for segment_source_maps) before exporting. On collision, fall back to
    a disambiguated dup<N>_<timestamp>.wav name instead of overwriting.
    parse_sync_filename_timestamp reads the component after the last
    underscore, so the disambiguated name still parses to the correct
    timestamp and downstream chronological sort in routers/sync.py is
    unaffected.
  • sync_local_files: create one threading.Lock() and pass it as
    segment_source_lock to every concurrent retrieve_vad_segments call for
    that request, closing the actual race (previously no lock was passed here
    at all).

Tested

  • Added test_colliding_absolute_timestamps_from_distinct_sources_keep_both_segments
    in tests/unit/test_sync_capture_pipeline.py, reproducing the issue's exact
    repro (capture starts T/T+1, VAD offsets [1,3)/[0,4)) against the
    real retrieve_vad_segments implementation — both segments now survive on
    disk with their correct, distinct durations (2s and 4s).
  • Updated the strict 3-arg retrieve_vad_segments mock in
    test_sync_geolocation_enrichment.py to accept **kwargs (it broke once
    the new lock kwarg started flowing through the real call site it stands in
    for).
  • Ran the full CI-selected test set for this diff (76 files via
    scripts/select_backend_unit_tests.py --changed-files), each in its own
    process: all green.
  • black --line-length 120 --skip-string-normalization on all 4 touched
    files: no changes.

Scope note: left utils/sync/pipeline.py::_run_sync_vad_phase's
segment_source_maps is None branch unchanged — that coordinator path has
its own fencing/lineage system (sync ledger, generation stamps) covered by a
large separate test suite (test_sync_v2.py and friends) with many
hand-written 3-arg mocks of retrieve_vad_segments; widening the lock there
is a separate, larger change and not needed to fix the reported scenario,
which reproduces through the simpler direct sync_local_files endpoint.

Line-Count-Exception: backend/routers/sync.py | 2500 -> 2501 | one-line lock creation + threading it into the existing VAD call site; no extractable helper
Line-Count-Exception: backend/utils/sync/pipeline.py | 3088 -> 3096 | reserve the export path under the lock before writing audio, to close the overwrite/data-loss race

Product invariants affected

none

Failure class (fixes)

Failure-Class: none

🤖 automated by hourly watchdog — tested and merged

View guided diff Turn on auto-fix

…inct sync sources (#20686)

Two distinct source WAV files can independently compute the same
absolute timestamp (capture_start + vad_offset) when exporting VAD
speech segments in retrieve_vad_segments(). Both exported to the same
<timestamp>.wav path with no collision check, so the later write
silently destroyed the earlier segment's audio before transcription.

Reserve the segment path in the shared segmented_paths set (under the
existing segment_source_lock when the caller provides one) before
export, and fall back to a disambiguated dup<N>_<timestamp>.wav name
on collision instead of overwriting. The disambiguated name still
parses correctly via parse_sync_filename_timestamp (it reads the
component after the last underscore), so downstream chronological
sorting in routers/sync.py is unaffected. Also wires a shared lock
into sync_local_files's direct VAD call site, which previously ran
with no synchronization at all across concurrent per-source workers.

Tested: added test_colliding_absolute_timestamps_from_distinct_sources_keep_both_segments,
reproducing the issue's exact scenario (capture starts T and T+1,
VAD offsets [1,3) and [0,4)) against the real retrieve_vad_segments
implementation -- both segments now survive with their correct,
distinct durations. Ran the full CI-selected test set for this diff
(76 files via scripts/select_backend_unit_tests.py) plus black
--line-length 120 --skip-string-normalization; all green.
@kodjima33
kodjima33 merged commit 99c74c6 into main Oct 8, 2026
59 checks passed
@kodjima33
kodjima33 deleted the watchdog/issue-20686-sync-vad-timestamp-collision branch October 8, 2026 04:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant