emdash icon indicating copy to clipboard operation
emdash copied to clipboard

[bug]: UI locks up - workspace file index deletes full-scan the FTS5 table on every reindex

Open kchung opened this issue 4 months ago โ€ข 12 comments

Bug Description

The UI hangs for seconds at a time โ€” and can completely lock up โ€” once the workspace file index grows large. Every reindex runs DELETE FROM workspace_file_index WHERE workspace_id = ? synchronously on the main process, and since workspace_id is an UNINDEXED FTS5 column, SQLite full-scans the entire trigram index to find the rows. The delete-all + reinsert pattern in WorkspaceFileIndexService.crawl() also bloats the database โ€” mine hit 2.5 GB (966k rows across 24 workspaces), at which point the scan became disk-bound and reindexes went from stutters to multi-second freezes that make the app unusable.

Steps to Reproduce

  1. Use emdash regularly with multiple projects/tasks so workspace_file_index accumulates rows (24 workspaces ร— ~40k files each, in my case)
  2. Run agents that write files โ€” each burst of fs watch events triggers a full recrawl after a 3s debounce
  3. Observe the UI hang during each reindex, escalating to complete lockups as the DB grows

Actual vs Expected Behavior

Actual: every recrawl blocks the main process (UI, IPC, terminals) while SQLite scans ~966k FTS rows off disk. Because recrawls re-trigger on every watch-event burst, the freezes come back-to-back and the app can appear completely locked up. Sampling the main process during a hang shows 100% of the window inside one better-sqlite3 .run():

Statement::JS_run โ†’ sqlite3_step โ†’ sqlite3VdbeExec
  โ”œโ”€ fts5NextMethod        (stepping every FTS row; ~44% of samples in pread)
  โ”œโ”€ fts5ColumnMethod + sqlite3MemCompare   (materializing rows to compare workspace_id)
  โ””โ”€ fts5UpdateMethod โ†’ sqlite3Fts5StorageDelete

Expected: reindexing a workspace's files should be O(that workspace) and never block the UI. Suggested fix: use an external-content FTS5 table backed by a regular workspace_files table with a B-tree index on workspace_id (deletes resolve by rowid instead of scanning), have crawl() diff against the existing index instead of delete-all + reinsert, and bump FILE_INDEX_VERSION to rebuild existing bloated indexes.

emdash Version

1.1.39

Operating System

macOS 26.5.2 (Apple Silicon)

Additional Context

Of the 2.5 GB file, workspace_file_index_data was 927 MB and workspace_file_index_content 228 MB; the rest was mostly free pages from churn.

Workaround โ€” quit emdash and reset the index (rebuilt on next launch); took my DB from 2.5 GB โ†’ 116 MB and stopped the hangs:

sqlite3 ~/Library/Application\ Support/emdash/emdash4.db \
  "DROP TABLE IF EXISTS workspace_file_index;
   DROP TABLE IF EXISTS workspace_file_index_meta;
   DELETE FROM kv WHERE key='file_index_version';
   VACUUM;"
Main-thread call graph from sample Emdash during a hang (2292 samples @ 1ms)
2292 Thread_4392865   DispatchQueue_1: com.apple.main-thread  (serial)
  2292 uv_run โ†’ uv__io_poll โ†’ uv__work_done
    2292 node::MakeLibuvRequestCallback<uv_fs_s, ...>::Wrapper(uv_fs_s*)
      2292 node::fs::AfterScanDir(uv_fs_s*)
        2292 ... (JS frames) ...
          2292 Statement::JS_run(v8::FunctionCallbackInfo<v8::Value> const&)  (in better_sqlite3.node)
            2292 sqlite3_step
              1898 sqlite3VdbeExec
              ! 1883 fts5NextMethod
              ! : 1849 sqlite3_step
              ! : | 1097 sqlite3VdbeExec
              ! : | + 1068 sqlite3BtreeNext
              ! : | + ! 1067 moveToChild
              ! : | + ! : 1063 getAndInitPage
              ! : | + ! : | 1021 getPageNormal
              ! : | + ! : | + 1009 readDbPage
              ! : | + ! : | + ! 1008 unixRead
              ! : | + ! : | + ! : 1008 pread  (in libsystem_kernel.dylib)
              ! : | 297 sqlite3VdbeExec + 42824
              ! : | + 270 _platform_memmove
              ! : | 49 sqlite3VdbeExec + 43380 (getCellInfo / btreeParseCellPtr)
              229 sqlite3VdbeExec + 30984
              ! 174 fts5ColumnMethod
              ! : 149 sqlite3_result_value โ†’ sqlite3VdbeMemMakeWriteable โ†’ vdbeMemAddTerminator โ†’ sqlite3VdbeMemGrow โ†’ memmove/malloc
              44 sqlite3VdbeExec + 30588 (sqlite3MemCompare / binCollFunc / _platform_memcmp)
              2 sqlite3VdbeExec + 31824
              ! 2 fts5UpdateMethod โ†’ sqlite3Fts5StorageDelete โ†’ sqlite3_step โ†’ sqlite3BtreeTableMoveto โ†’ readDbPage โ†’ pread
              1 sqlite3VdbeExec + 13756 โ†’ sqlite3VtabSavepoint โ†’ fts5SavepointMethod โ†’ sqlite3Fts5FlushToDisk โ†’ fts5IndexFlush โ†’ fts5IndexMergeLevel

(Condensed to the dominant branches; all other threads were idle in kevent/mach_msg waits.)

Full raw sample output (all threads + binary images, ~300 KB โ€” too large for an issue body): https://gist.github.com/kchung/f5d3f5b879a8805d77a32262b38485eb

kchung avatar Jul 15 '26 23:07 kchung

Hi @kchung - thanks for flagging this.

And thank you for also sharing the workaround - very helpful. We're looking into it and keep you up to date here!

arnestrickmann avatar Jul 15 '26 23:07 arnestrickmann

We're on a fix, @kchung

arnestrickmann avatar Jul 15 '26 23:07 arnestrickmann

hi @kchung , thanks for reporting, just merged a hot fix in #2883 . proper fix will land next week!

jschwxrz avatar Jul 16 '26 06:07 jschwxrz

Thanks all!

kchung avatar Jul 16 '26 16:07 kchung

Still reproduces on v1.1.40 (macOS 26.5, Apple Silicon), which already contains the #2883 hotfix. Same root cause, but the blocking call reaches SQLite by a different path than the original report, so I think the hotfix closed one entry point and left another open.

Different entry point: timer, not fs-watch

The original report has the blocking .run() arriving via a directory-scan completion:

uv_run โ†’ uv__io_poll โ†’ uv__work_done โ†’ node::fs::AfterScanDir โ†’ Statement::JS_run

Mine consistently arrives via uv__run_timers instead โ€” three separate sample runs over ~15 minutes, all identical:

uv_run โ†’ uv__run_timers โ†’ node::Environment::RunTimers โ†’ v8::Function::Call
  โ†’ Statement::JS_run  (in better_sqlite3.node)
    โ†’ sqlite3_step
      โ†’ sqlite3VdbeExec
        โ†’ fts5NextMethod
          โ†’ sqlite3BtreeNext โ†’ moveToChild โ†’ getAndInitPage
            โ†’ getPageNormal โ†’ readDbPage โ†’ unixRead โ†’ pread

~83% of samples were in pread under fts5NextMethod, so it was disk-bound stepping the trigram index, same as before. It is JS_run (a write), matching the DELETE ... WHERE workspace_id = ? full-scan you identified.

Scale and timing

workspace_file_index rows 944,358
workspaces indexed 58
emdash4.db 1.16 GB
WAL 105 MB, byte-identical across commits (frame recycling, not growth)
app uptime when it locked ~1d 21h

The UI was fully unresponsive, not stuttering. The local hook server kept its listening socket open but never answered: 45 consecutive POSTs to /hook timed out at 3s during the freeze, and Claude Code hook curls spawned by the app hung for 6+ minutes. Agent PTYs stopped receiving IPC entirely.

I logged WAL commits every 15s to check whether it was deadlocked or just slow. It was progressing, but barely โ€” 3 commits in 12 minutes, with gaps of 6m12s, 1m34s, then 4m22s+ still running when I killed it. CPU stayed pinned at 97โ€“99% on one core the whole time, dipping briefly between statements (28%, 20%) but never long enough to serve a 3s HTTP request. So from the user's side it is indistinguishable from a hard hang.

Worth noting for the proper fix

Reducing worktree count does not help, which follows from the diagnosis: the delete is O(entire index) regardless of how many workspaces exist. I removed 21 worktrees (67 โ†’ 46 directories) and the DB stayed at 1.16 GB with the same freeze behaviour. Fewer workspaces means fewer reindexes, not cheaper ones.

The timer-driven path also means this fires without any agent writing files โ€” an idle app with a large index still locks up on schedule.

Workaround confirmed

The index reset from the original report worked here too: 1.16 GB โ†’ 7.1 MB, WAL to 0, pragma integrity_check ok, all projects/tasks/workspaces/conversations intact. Took about a minute including the VACUUM.

gemueselasagne avatar Jul 27 '26 14:07 gemueselasagne

I've also ran into this bug. I've put up a PR with a potential fix.

piter10p avatar Jul 28 '26 10:07 piter10p

@piter10p I've been running your build the last day and it's been working great, no lock ups on my end! cc @arnestrickmann @jschwxrz

kchung avatar Jul 30 '26 19:07 kchung

@piter10p thank you for your contribution! Greatly appreciated! We'll look into it. Thank you, @kchung for confirming that the build works for you! ๐Ÿ™

rabanspiegel avatar Jul 31 '26 00:07 rabanspiegel

Any update @rabanspiegel ?

piter10p avatar Aug 11 '26 06:08 piter10p

the issue was fixed as part of #2833 and will be in the next release! sorry this took so long

jschwxrz avatar Aug 11 '26 19:08 jschwxrz

Any chance this is released soon? I find this quite annoying

ytausch avatar Aug 27 '26 13:08 ytausch

yess, we just released v1.2.0

jschwxrz avatar Aug 28 '26 11:08 jschwxrz