Repository navigation
fix(backend): don't overwrite colliding VAD segment exports from distinct sync sources (#20686) - #20936
Merged
Conversation
…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
deleted the
watchdog/issue-20686-sync-vad-timestamp-collision
branch
October 8, 2026 04:49
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Bug
backend/utils/sync/pipeline.py::retrieve_vad_segments()names each exportedVAD speech segment
<path_dir>/<capture_start + vad_offset>.wav. Two distinctsource WAL files uploaded in the same sync request can independently compute
the same absolute timestamp (e.g. source A starts at
Twith speech at[1,3), source B starts atT+1with speech at[0,4)— both resolve toT+1.wav).routers/sync.py::sync_local_filesrunsretrieve_vad_segmentsfor every source file concurrently via
asyncio.gatherover one sharedsegmented_pathsset with no lock, so the second export silently overwritesthe 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 insync_local_files's call site.Fix
retrieve_vad_segments: reserve the segment path insegmented_paths(under
segment_source_lock, falling back to the existingcontextlib.nullcontext()when no lock is supplied — same pattern alreadyused for
segment_source_maps) before exporting. On collision, fall back toa disambiguated
dup<N>_<timestamp>.wavname instead of overwriting.parse_sync_filename_timestampreads the component after the lastunderscore, so the disambiguated name still parses to the correct
timestamp and downstream chronological sort in
routers/sync.pyisunaffected.
sync_local_files: create onethreading.Lock()and pass it assegment_source_lockto every concurrentretrieve_vad_segmentscall forthat request, closing the actual race (previously no lock was passed here
at all).
Tested
test_colliding_absolute_timestamps_from_distinct_sources_keep_both_segmentsin
tests/unit/test_sync_capture_pipeline.py, reproducing the issue's exactrepro (capture starts
T/T+1, VAD offsets[1,3)/[0,4)) against thereal
retrieve_vad_segmentsimplementation — both segments now survive ondisk with their correct, distinct durations (2s and 4s).
retrieve_vad_segmentsmock intest_sync_geolocation_enrichment.pyto accept**kwargs(it broke oncethe new lock kwarg started flowing through the real call site it stands in
for).
scripts/select_backend_unit_tests.py --changed-files), each in its ownprocess: all green.
black --line-length 120 --skip-string-normalizationon all 4 touchedfiles: no changes.
Scope note: left
utils/sync/pipeline.py::_run_sync_vad_phase'ssegment_source_maps is Nonebranch unchanged — that coordinator path hasits own fencing/lineage system (sync ledger, generation stamps) covered by a
large separate test suite (
test_sync_v2.pyand friends) with manyhand-written 3-arg mocks of
retrieve_vad_segments; widening the lock thereis a separate, larger change and not needed to fix the reported scenario,
which reproduces through the simpler direct
sync_local_filesendpoint.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