Skip to content

_activeSessionName lacks volatile — stale reads on ARM from bridge/restore threads #348

@btessiau

Description

@btessiau

Problem

_activeSessionName is a plain string? field on CopilotService accessed from multiple threads:

Thread Operation Location
UI thread Write (SetActiveSession, CloseSession, RenameSession, CreateSession) CopilotService.cs
WsBridge background Read + conditional write CopilotService.Bridge.cs:532
Restore background Conditional write (??=) CopilotService.Persistence.cs:380
SDK event handler Read comparison CopilotService.Events.cs:876
SaveActiveSessionsToDisk Read CopilotService.Persistence.cs:457

On ARM (iOS/Android), without volatile, a write from the UI thread may sit in a CPU store buffer and never become visible to reads on other cores. This can cause:

  • WsBridge syncing a stale active session name
  • SaveActiveSessionsToDisk persisting the wrong active session
  • Events handler comparing against a stale value

Proposed Fix

Mark the field volatile — ensures memory barriers on every read/write. All compound patterns (??=, compare-then-swap) are either on the UI thread or benign (idempotent/first-write-wins).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions