Skip to content

Commit e4863fc

Browse files
committed
fix: address race condition and duplicate hash call in DirectoryScanner
- Wrap totalBlockCount and currentBatchFileInfos updates in mutex lock to prevent race conditions - Cache isNewFile result to avoid duplicate cacheManager.getHash() calls - Ensures thread-safe batch processing in concurrent file parsing
1 parent 128f45d commit e4863fc

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

src/services/code-index/processors/scanner.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ export class DirectoryScanner implements IDirectoryScanner {
124124

125125
// Check against cache
126126
const cachedFileHash = this.cacheManager.getHash(filePath)
127+
const isNewFile = !cachedFileHash
127128
if (cachedFileHash === currentFileHash) {
128129
// File is unchanged
129130
skippedCount++
@@ -180,12 +181,17 @@ export class DirectoryScanner implements IDirectoryScanner {
180181

181182
// Add file info once per file (outside the block loop)
182183
if (addedBlocksFromFile) {
183-
totalBlockCount += fileBlockCount
184-
currentBatchFileInfos.push({
185-
filePath,
186-
fileHash: currentFileHash,
187-
isNew: !this.cacheManager.getHash(filePath),
188-
})
184+
const release = await mutex.acquire()
185+
try {
186+
totalBlockCount += fileBlockCount
187+
currentBatchFileInfos.push({
188+
filePath,
189+
fileHash: currentFileHash,
190+
isNew: isNewFile,
191+
})
192+
} finally {
193+
release()
194+
}
189195
}
190196
} else {
191197
// Only update hash if not being processed in a batch

0 commit comments

Comments
 (0)