Skip to content

.unwrap() panic in AiService — backend crash on session race condition #94

@EVWorth

Description

@EVWorth

Summary

A bare .unwrap() in the AI service will crash the entire Tauri backend when a race condition occurs between session creation and cancellation.

Location

src-tauri/crates/mas-ai/src/service.rs:361

let sessions = self.sessions.read().await;
sessions.get(conversation_id).unwrap().session.clone()  // PANIC

Root Cause

The code checks need_new_session at line 190 with a read lock, then releases it. The else branch at line 359 reacquires a read lock and calls .unwrap() on sessions.get(). Between these two operations, a cancel or session removal (from a different concurrent request) can delete the session, causing the .unwrap() to panic and crash the entire backend process.

Impact

  • Any concurrent cancel + send operation can crash the app
  • The user sees the app vanish without any error message
  • All unsaved work is lost

Fix

Replace .unwrap() with proper error handling:

let session = sessions
    .get(conversation_id)
    .ok_or_else(|| AiError::SessionNotFound)?
    .session
    .clone();

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