Skip to content

Commit ad11b16

Browse files
fix: prevent bg analysis from crashing drill end flow
Two fixes: 1. Wrap each chain step in try/catch so a single error doesn't reject the entire promise chain 2. Don't await the chain when stopping — just signal cancellation and stop stockfish, then proceed. Awaiting was hanging when a step was stuck (e.g., Maia ONNX inference or stockfish generator) Also added more detailed [bg] console logs at each stage. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 5ae65b7 commit ad11b16

1 file changed

Lines changed: 28 additions & 12 deletions

File tree

src/hooks/useOpeningDrillController/useOpeningDrillController.ts

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -949,29 +949,45 @@ export const useOpeningDrillController = (
949949
if (bgAnalyzedFensRef.current.has(node.fen)) continue
950950
bgAnalyzedFensRef.current.add(node.fen)
951951

952-
// Chain this node's analysis onto the promise chain
952+
// Chain this node's analysis onto the promise chain.
953+
// Wrapped in try/catch so one failure doesn't break the whole chain.
953954
bgChainRef.current = bgChainRef.current.then(async () => {
954-
if (bgCancelledRef.current) return
955-
console.log('[bg] analyzing:', node.fen.split(' ')[0].slice(-20))
956-
await ensureMaiaRef.current(node)
957-
if (bgCancelledRef.current) return
958-
await ensureStockfishRef.current(node)
959-
console.log('[bg] done, sf depth:', node.analysis.stockfish?.depth ?? 0)
955+
try {
956+
if (bgCancelledRef.current) return
957+
console.log('[bg] maia start:', node.san || node.move || '?')
958+
await ensureMaiaRef.current(node)
959+
const hasMaia = !!(
960+
node.analysis.maia &&
961+
MAIA_MODELS.every((m) => node.analysis.maia?.[m])
962+
)
963+
console.log('[bg] maia done:', hasMaia, '| sf start')
964+
if (bgCancelledRef.current) return
965+
await ensureStockfishRef.current(node)
966+
console.log(
967+
'[bg] sf done, depth:',
968+
node.analysis.stockfish?.depth ?? 0,
969+
)
970+
} catch (error) {
971+
console.error('[bg] error analyzing node:', error)
972+
}
960973
})
961974
}
962975
}, [currentDrillGame, gameTree, isAnalyzingDrill, treeController.currentNode])
963976

964-
// Helper: stop background analysis and wait for it to fully settle
965-
const stopBackgroundAnalysis = useCallback(async () => {
977+
// Stop background analysis. Signals cancellation and stops stockfish so
978+
// ensureDrillAnalysis can use stockfish immediately. The chain's remaining
979+
// .then() callbacks will see the cancelled flag and return quickly.
980+
// Does NOT await the chain — avoids hanging if a step is stuck.
981+
const stopBackgroundAnalysis = useCallback(() => {
966982
bgCancelledRef.current = true
967983
stockfish.stopEvaluation()
968-
await bgChainRef.current
969984
}, [stockfish])
970985

971986
const ensureDrillAnalysis = useCallback(
972987
async (drillGame: OpeningDrillGame): Promise<boolean> => {
973-
// Stop background loop and wait for it to fully exit before using stockfish
974-
await stopBackgroundAnalysis()
988+
// Signal background to stop and give the generator a tick to clean up
989+
stopBackgroundAnalysis()
990+
await delay(50)
975991

976992
const mainLine = drillGame.tree.getMainLine()
977993
const startingNode = drillGame.openingEndNode || mainLine[0]

0 commit comments

Comments
 (0)