VoiceInk icon indicating copy to clipboard operation
VoiceInk copied to clipboard

State Corruption - PowerMode session can start while previous session is active

Open ebrindley opened this issue 1 month ago • 0 comments

State Corruption - PowerMode session can start while previous session is active

Labels: bug, state-management, medium-priority

Description

PowerModeSessionManager.beginSession() can be called multiple times without properly ending the previous session, causing nested sessions that corrupt user settings and prevent proper state restoration.

User Impact

  • Settings not restored correctly after app switching

  • Wrong AI prompts applied due to session conflicts

  • Confusing behavior when rapidly switching between apps with PowerMode configs

  • State corruption requiring app restart to recover

Technical Details

File: VoiceInk/PowerMode/PowerModeSessionManager.swift

Lines: 38-66

Current code allows nested sessions:


func beginSession(with config: PowerModeConfig) async {

    // No check if session already active!

    guard !isApplyingPowerModeConfig else { return }

 

    let snapshot = ApplicationState(...)

    currentSession = Session(...)

    saveSession()  // Overwrites previous session!

    // ...

}

If user rapidly switches apps with different PowerMode configurations, the second beginSession() overwrites the first session's state snapshot, causing incorrect settings to be restored when the session ends.

Reproduction

  1. Configure PowerMode for Safari and Chrome with different settings

  2. Start recording in Safari (PowerMode activates)

  3. Quickly switch to Chrome before stopping (PowerMode tries to activate again)

  4. Original Safari settings are lost

  5. When recording ends, wrong settings are restored

Recommended Fix

Add guard at the beginning of beginSession() (after line 42):


func beginSession(with config: PowerModeConfig) async {

    guard let whisperState = whisperState, let enhancementService = enhancementService else {

        print("SessionManager not configured.")

        return

    }

 

    // Prevent nested sessions - end current session first

    if loadSession() != nil {

        print("⚠️ PowerMode session already active. Ending previous session.")

        await endSession()

    }

 

    guard !isApplyingPowerModeConfig else { return }

 

    // ... rest of existing implementation

}

Testing

  1. Rapidly switch between apps with different PowerMode configs

  2. Verify settings are correctly restored

  3. Check console logs for warning messages about nested sessions

  4. Confirm no session nesting occurs

  5. Verify state restoration works correctly in all scenarios

ebrindley avatar Nov 12 '25 18:11 ebrindley