Skip to content

Fix VAD gate keepalive: 20s → 5s to prevent DG 1011 disconnect - #5235

Merged
beastoin merged 2 commits into
mainfrom
fix/vad-keepalive-interval-5234
Feb 28, 2026
Merged

beastoin merged 2 commits into
mainfrom
fix/vad-keepalive-interval-5234

Conversation

@beastoin

Copy link
Copy Markdown
Collaborator

Summary

  • Reduces VAD_GATE_KEEPALIVE_SEC from 20s to 5s to match Deepgram's documented 10s idle timeout
  • Updates comment from "30s idle timeout" to "10s idle timeout"
  • Adds regression tests that reproduce and verify the fix

Problem

Deepgram's WebSocket idle timeout is 10 seconds (docs), not 30s as originally assumed. Our 20s keepalive interval left a window where DG received no audio or keepalive for 10+ seconds, risking a 1011 (NET-0001) disconnect.

Affected users: Those WITHOUT speech profile in quiet environments. Gate starts in active mode, classifies initial silence, sends nothing to DG → 1011 disconnect at 10s.

Unaffected: Users WITH speech profile (gate starts in shadow mode, sends all audio for first ~15-25s).

Bug Reproduction

Two regression tests reproduce the bug with the old 20s interval:

  1. test_reproduce_1011_gap_initial_silence — 10s of initial silence, DG gets nothing
  2. test_keepalive_fires_within_dg_timeout_after_speech_ends — silence after speech, keepalive too late

Both tests FAILED before the fix, PASS after.

Changes

  • backend/utils/stt/vad_gate.py: VAD_GATE_KEEPALIVE_SEC = 20 → 5, comment fix
  • backend/tests/unit/test_vad_gate.py: 2 new regression tests + updated boundary tests to use constant

Test plan

  • Bug reproduced: both tests FAIL with 20s interval
  • Bug fixed: both tests PASS with 5s interval
  • All 107 VAD gate tests pass
  • No behavior change for shadow/off modes
  • Codex review confirms 5s is within DG's recommended 3-5s range

Risk

  • Low: KeepAlive is lightweight (~12 msgs/min during silence vs ~3/min before)
  • No impact on speech-profile users (shadow mode doesn't send keepalives)
  • No impact on audio quality (keepalive is a control message, not audio)

Fixes #5234
Related: #4931 (VAD gate implementation)

🤖 Generated with Claude Code

beastoin and others added 2 commits February 28, 2026 03:22
Deepgram's idle timeout is 10s (not 30s as originally assumed).
Our 20s keepalive interval left a gap where DG received nothing
for 10+ seconds during initial silence, risking 1011 disconnect
for users without speech profile in quiet environments.

Fixes #5234

Co-Authored-By: Claude Opus 4.6 <[email protected]>
- Add TestDG1011KeepaliveGap: reproduces the 10s gap bug and verifies
  keepalive fires within DG's documented 10s idle timeout
- Update TestKeepaliveBoundary to use VAD_GATE_KEEPALIVE_SEC constant
  instead of hardcoded 19.99/20.0 values

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@greptile-apps

greptile-apps Bot commented Feb 28, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR fixes a critical bug where users without speech profiles could experience Deepgram WebSocket disconnects (error 1011 / NET-0001) in quiet environments. The root cause was a mismatch between the keepalive interval (20s) and Deepgram's documented idle timeout (10s).

Key Changes:

  • Reduces VAD_GATE_KEEPALIVE_SEC from 20s to 5s, providing a 5s safety margin below the 10s timeout
  • Updates the comment to reflect the correct 10s idle timeout (was incorrectly stated as 30s)
  • Adds two comprehensive regression tests that reproduce the bug with the old interval and verify the fix
  • Updates the boundary test to use the constant instead of hardcoded values for better maintainability

Technical Correctness:
The fix is mathematically sound and aligns with Deepgram's recommended keepalive range (3-5s). The 5s interval increases keepalive frequency from ~3/min to ~12/min during silence, which is a lightweight trade-off for preventing disconnects.

Test Coverage:
The regression tests thoroughly verify:

  1. Initial silence scenario (users connecting in quiet environments)
  2. Post-speech silence scenario (silence after hangover expires)

Both tests failed with the old 20s interval and pass with the new 5s interval, confirming the fix resolves the issue.

Confidence Score: 5/5

  • This PR is safe to merge with no risk - it fixes a critical bug with minimal code changes and comprehensive test coverage
  • The fix is simple, well-tested, and mathematically correct. The constant change from 20s to 5s is supported by Deepgram's documentation, the comment update is accurate, and the regression tests provide strong verification. The change has low performance impact and no behavior change for users with speech profiles.
  • No files require special attention

Important Files Changed

Filename Overview
backend/utils/stt/vad_gate.py Reduces keepalive interval from 20s to 5s to align with Deepgram's 10s idle timeout, updates comment from 30s to 10s
backend/tests/unit/test_vad_gate.py Adds two comprehensive regression tests for DG 1011 disconnect scenarios, updates boundary test to use constant instead of hardcoded value

Sequence Diagram

sequenceDiagram
    participant Client
    participant VADGate
    participant GatedDGSocket
    participant Deepgram
    
    Note over VADGate,Deepgram: Initial Silence Scenario (No Speech Profile)
    Client->>GatedDGSocket: send(silence_audio, t=0s)
    GatedDGSocket->>VADGate: process_audio()
    VADGate-->>GatedDGSocket: audio_to_send=empty (silence gated)
    Note over VADGate: last_send_time=None<br/>ref_time=first_audio_time(0s)
    
    Client->>GatedDGSocket: send(silence_audio, t=5s)
    GatedDGSocket->>VADGate: needs_keepalive(5s)?
    VADGate-->>GatedDGSocket: True (5s >= 5s)
    GatedDGSocket->>Deepgram: keep_alive()
    Note over Deepgram: Traffic received at 5s<br/>✅ Within 10s timeout
    GatedDGSocket->>VADGate: record_keepalive(5s)
    Note over VADGate: last_send_time=5s
    
    Client->>GatedDGSocket: send(silence_audio, t=10s)
    GatedDGSocket->>VADGate: needs_keepalive(10s)?
    VADGate-->>GatedDGSocket: True (5s elapsed)
    GatedDGSocket->>Deepgram: keep_alive()
    Note over Deepgram: ✅ No disconnect
    
    Note over VADGate,Deepgram: OLD BEHAVIOR (20s interval)
    rect rgb(255, 230, 230)
        Note over VADGate: With 20s keepalive interval
        Note over Deepgram: 10s passes with no traffic
        Note over Deepgram: ❌ DG disconnects with 1011<br/>(NET-0001 idle timeout)
    end
Loading

Last reviewed commit: 5f8bab5

@beastoin

Copy link
Copy Markdown
Collaborator Author

Live Backend Test Results

Setup: Local dev backend with VAD_GATE_MODE=active, keepalive interval = 5s (the fix). Real Omi BLE device connected via Pixel 7a.

Test flow:

  1. Device connected → gate initialized in shadow mode (speech profile)
  2. Speech profile sent (16.88s of audio) → DG nova-3 model confirmed
  3. Gate activated shadow→active at cursor=15040ms
  4. 4+ minutes of continuous silence — no audio sent to DG, only keepalive messages

Results:

Check Status
DG 1011 disconnect during extended silence PASS — zero disconnects
DG connection alive after 4+ min silence PASS — conversations processing normally
Error count 0 — no errors in full session
Gate activation (shadow→active) PASS — activated after speech profile
Conversation lifecycle PASS — stub conversations created/processed normally

Key evidence: With the old 20s keepalive, DG would have disconnected at ~10s of silence (1011 NET-0001). With the fixed 5s keepalive, the connection survived 4+ minutes of uninterrupted silence.

Log excerpts:

VAD gate initialized mode=shadow preseconds=15 codec=opus
VADGate activated shadow->active cursor=15040.0ms
Metadata: duration=16.88, model=general-nova-3 2026-01-27.9249
# No Connection Closed on main socket
# No 1011 errors
# Conversations cycling normally (no content = expected during silence)

by AI for @beastoin

@beastoin beastoin left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@beastoin
beastoin merged commit af79c26 into main Feb 28, 2026
2 checks passed
@beastoin
beastoin deleted the fix/vad-keepalive-interval-5234 branch February 28, 2026 02:41
@beastoin

Copy link
Copy Markdown
Collaborator Author

Prod Deployment — Confirmed

Deploy: SUCCESS (02:55 UTC, Feb 28)

Check Result
1011 errors post-deploy 0 (down from ~50/hour baseline)
VAD gate state transitions PASS — flowing normally
Rollout backend, backend-sync, backend-listen, backend-integration

The keepalive fix eliminated the pre-existing ~50/hour 1011 error rate entirely. The old 20s interval was causing DG disconnects in production that went unnoticed — now confirmed and fixed.

Bonus insight from prod metrics (100 sessions):

  • Avg speech ratio: 11% speech / 89% silence
  • 91% of sessions have <20% speech
  • Confirms the gate is correctly identifying and skipping the vast majority of audio as silence

by AI for @beastoin

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.

VAD gate keepalive interval too long — risks DG 1011 disconnect

1 participant