From ea33101a949ae9b873f8e6e964fdf0a1c0a55d18 Mon Sep 17 00:00:00 2001 From: Matthew Slight Date: Wed, 4 Jun 2025 23:38:53 +0400 Subject: [PATCH] Terminate engine worker when useStockfish unmounts --- src/hooks/useStockfish.ts | 4 ++++ src/lib/StockfishEngine.ts | 1 + src/pages/drills/components/DrillList.tsx | 6 +++++- 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/hooks/useStockfish.ts b/src/hooks/useStockfish.ts index ee056ed..c002a28 100644 --- a/src/hooks/useStockfish.ts +++ b/src/hooks/useStockfish.ts @@ -72,6 +72,10 @@ export function useStockfish( return () => { didCancel = true; + // When the engine instance is replaced or the component unmounts, + // ensure any ongoing search is halted and the worker is terminated. + engine.stop(); + engine.quit(); }; }, [engine]); diff --git a/src/lib/StockfishEngine.ts b/src/lib/StockfishEngine.ts index 0cfbb80..b2e19a7 100644 --- a/src/lib/StockfishEngine.ts +++ b/src/lib/StockfishEngine.ts @@ -189,5 +189,6 @@ export class StockfishEngine { public quit() { if (DEBUG) console.log('[StockfishEngine] quit'); this.send('quit'); + this.worker.terminate(); } } diff --git a/src/pages/drills/components/DrillList.tsx b/src/pages/drills/components/DrillList.tsx index a61eb42..3dcde1d 100644 --- a/src/pages/drills/components/DrillList.tsx +++ b/src/pages/drills/components/DrillList.tsx @@ -27,7 +27,11 @@ export default function DrillList({ drills, loading, onStartDrill }: Props) {
{drills.map((d) => { return ( - onStartDrill(d.id)} /> + onStartDrill(d.id)} + /> ); })}