Skip to content

Fix: Multiple WebRTC Sessions Created on Rapid Button Clicks #5

@hamidihekmat

Description

@hamidihekmat

Issue Description

When clicking the start broadcast button multiple times in quick succession, multiple WebRTC sessions are created simultaneously, leading to:

  • Multiple audio streams
  • Resource leaks
  • Inconsistent state management
  • Potential memory leaks

Current Behavior

The current implementation allows multiple sessions to be created before previous sessions are properly terminated.

Code Fix

// Add these state variables
const [isConnecting, setIsConnecting] = useState(false);
const [isSessionActive, setIsSessionActive] = useState(false);

// Replace the current startSession function with this protected version
async function startSession() {
  // Prevent multiple connection attempts
  if (isConnecting || isSessionActive) {
    console.warn('Session already active or connecting');
    return;
  }
  try {
    setIsConnecting(true);
    setStatus('Requesting microphone access...');
    // ... rest of the existing connection logic ...
  } catch (err) {
    console.error('startSession error:', err);
    setStatus(`Error: ${err}`);
    stopSession();
  } finally {
    setIsConnecting(false);
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions