Skip to content

fix(app) prevent Bad state: No element crash in SD card WAL sync - #6803

Merged
mdmohsin7 merged 1 commit into
mainfrom
fix/sdcard-wal-sync-bad-state
Apr 19, 2026
Merged

mdmohsin7 merged 1 commit into
mainfrom
fix/sdcard-wal-sync-bad-state

Conversation

@krushnarout

Copy link
Copy Markdown
Member

Fixes #6656

_wals.where((w) => w == wal).toList().first throws Bad state: No element when the WAL is no longer present in _wals (race condition). Replaced with .firstOrNull + early return.

🤖 Generated with Claude Code

@greptile-apps

greptile-apps Bot commented Apr 18, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR fixes a Bad state: No element crash in SDCardWalSyncImpl.syncWal caused by a race condition: the WAL could be removed from _wals (e.g. by a concurrent setDevice() reset) between the time syncWal is called and when the lookup runs. The fix swaps .toList().first for .firstOrNull and returns null early, which is safe because the return type is already SyncLocalFilesResponse? and all callers handle a null result.

Confidence Score: 5/5

Safe to merge — minimal, targeted fix with no side effects on existing call sites.

The change is a single-function, two-line fix. The return type was already nullable, all callers handle null, and the null path adds a debug log. No new logic is introduced.

No files require special attention.

Important Files Changed

Filename Overview
app/lib/services/wals/sdcard_wal_sync.dart Replaces crashing .toList().first with .firstOrNull + early null return to handle race condition where WAL is removed from _wals before sync begins; callers already handle nullable response.

Sequence Diagram

sequenceDiagram
    participant Caller as SyncProvider
    participant SW as syncWal()
    participant Wals as _wals list
    participant Async as Concurrent setDevice()

    Caller->>SW: syncWal(wal)
    Note over Async,Wals: Race: setDevice() can clear _wals
    Async->>Wals: _getMissingWals() replaces _wals
    SW->>Wals: _wals.where(w == wal).firstOrNull
    alt WAL still present
        Wals-->>SW: walToSync (non-null)
        SW->>SW: proceed with sync
        SW-->>Caller: SyncLocalFilesResponse
    else WAL already removed (race)
        Wals-->>SW: null
        SW->>SW: log + return null (no crash)
        SW-->>Caller: null (handled safely)
    end
Loading

Reviews (1): Last reviewed commit: "fix(app) prevent Bad state: No element c..." | Re-trigger Greptile

@krushnarout
krushnarout requested a review from mdmohsin7 April 18, 2026 09:14
@mdmohsin7
mdmohsin7 merged commit b9fa958 into main Apr 19, 2026
2 of 3 checks passed
@mdmohsin7
mdmohsin7 deleted the fix/sdcard-wal-sync-bad-state branch April 19, 2026 14:19
Glucksberg pushed a commit to Glucksberg/omi-local that referenced this pull request Apr 28, 2026
…edHardware#6803)

Fixes BasedHardware#6656

`_wals.where((w) => w == wal).toList().first` throws `Bad state: No
element` when the WAL is no longer present in `_wals` (race condition).
Replaced with `.firstOrNull` + early return.

🤖 Generated with [Claude Code](https://claude.com/claude-code)
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.

Bug: SD card sync transfer fails with 'Bad state: No element'

2 participants