Commit 6f27677
fix(index): serialize concurrent Index/EnsureFresh calls to prevent duplicate key errors
Two concurrent MCP search queries on the same unindexed project both
received the same *Indexer from the cache, then both passed the stale
check and entered indexWithTree simultaneously, causing UNIQUE constraint
violations on chunk inserts.
Add sync.Mutex to Indexer and lock around the hash-check + indexing
critical section in both Index and EnsureFresh. The merkle tree build
stays outside the lock (it is read-only and can be slow for large repos);
the second goroutine that acquires the lock will find the hash already
up to date and return early without re-indexing.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>1 parent d1666d1 commit 6f27677
1 file changed
Lines changed: 11 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
22 | 22 | | |
23 | 23 | | |
24 | 24 | | |
| 25 | + | |
25 | 26 | | |
26 | 27 | | |
27 | 28 | | |
| |||
55 | 56 | | |
56 | 57 | | |
57 | 58 | | |
| 59 | + | |
58 | 60 | | |
59 | 61 | | |
60 | 62 | | |
| |||
85 | 87 | | |
86 | 88 | | |
87 | 89 | | |
| 90 | + | |
88 | 91 | | |
89 | 92 | | |
90 | 93 | | |
91 | 94 | | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
92 | 99 | | |
93 | 100 | | |
94 | 101 | | |
| |||
105 | 112 | | |
106 | 113 | | |
107 | 114 | | |
| 115 | + | |
108 | 116 | | |
109 | 117 | | |
110 | 118 | | |
111 | 119 | | |
112 | 120 | | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
113 | 124 | | |
114 | 125 | | |
115 | 126 | | |
| |||
0 commit comments