Skip to content

Commit e0649ad

Browse files
committed
fix(kb): address PR review - finally block race, contentHash propagation, resourceName
- Replace DB-read finally block with local syncExitedCleanly flag to avoid race condition - Propagate fullDoc.contentHash during deferred content hydration - Add resourceName to file restore audit record
1 parent 420ef9d commit e0649ad

File tree

2 files changed

+20
-14
lines changed

2 files changed

+20
-14
lines changed

apps/sim/app/api/workspaces/[id]/files/[fileId]/restore/route.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export async function POST(
3838
action: AuditAction.FILE_RESTORED,
3939
resourceType: AuditResourceType.FILE,
4040
resourceId: fileId,
41+
resourceName: fileId,
4142
description: `Restored workspace file`,
4243
request,
4344
})

apps/sim/lib/knowledge/connectors/sync-engine.ts

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,8 @@ export async function executeSync(
279279
startedAt: new Date(),
280280
})
281281

282+
let syncExitedCleanly = false
283+
282284
try {
283285
const externalDocs: ExternalDocument[] = []
284286
let cursor: string | undefined
@@ -441,15 +443,20 @@ export async function executeSync(
441443
const hydrated = await Promise.allSettled(
442444
deferredOps.map(async (op) => {
443445
const fullDoc = await connectorConfig.getDocument(
444-
accessToken,
446+
accessToken!,
445447
sourceConfig,
446448
op.extDoc.externalId,
447449
syncContext
448450
)
449451
if (!fullDoc?.content.trim()) return null
450452
return {
451453
...op,
452-
extDoc: { ...op.extDoc, content: fullDoc.content, contentDeferred: false },
454+
extDoc: {
455+
...op.extDoc,
456+
content: fullDoc.content,
457+
contentHash: fullDoc.contentHash ?? op.extDoc.contentHash,
458+
contentDeferred: false,
459+
},
453460
}
454461
})
455462
)
@@ -593,6 +600,7 @@ export async function executeSync(
593600
)
594601

595602
logger.info('Sync completed', { connectorId, ...result })
603+
syncExitedCleanly = true
596604
return result
597605
} catch (error) {
598606
if (error instanceof ConnectorDeletedException) {
@@ -624,6 +632,7 @@ export async function executeSync(
624632
}
625633

626634
result.error = 'Connector deleted during sync'
635+
syncExitedCleanly = true
627636
return result
628637
}
629638

@@ -663,15 +672,11 @@ export async function executeSync(
663672
}
664673

665674
result.error = errorMessage
675+
syncExitedCleanly = true
666676
return result
667677
} finally {
668-
try {
669-
const [row] = await db
670-
.select({ status: knowledgeConnector.status })
671-
.from(knowledgeConnector)
672-
.where(eq(knowledgeConnector.id, connectorId))
673-
.limit(1)
674-
if (row?.status === 'syncing') {
678+
if (!syncExitedCleanly) {
679+
try {
675680
await db
676681
.update(knowledgeConnector)
677682
.set({
@@ -681,12 +686,12 @@ export async function executeSync(
681686
})
682687
.where(eq(knowledgeConnector.id, connectorId))
683688
logger.warn('Reset stale syncing status in finally block', { connectorId })
689+
} catch (finallyError) {
690+
logger.warn('Failed to reset syncing status in finally block', {
691+
connectorId,
692+
error: finallyError instanceof Error ? finallyError.message : String(finallyError),
693+
})
684694
}
685-
} catch (finallyError) {
686-
logger.warn('Failed to check/reset syncing status in finally block', {
687-
connectorId,
688-
error: finallyError instanceof Error ? finallyError.message : String(finallyError),
689-
})
690695
}
691696
}
692697
}

0 commit comments

Comments
 (0)