@@ -55,6 +55,10 @@ export function WorkerProvider({
5555 const interruptBuffer = useRef < Uint8Array | null > ( null ) ;
5656 const capabilities = useRef < WorkerCapabilities | null > ( null ) ;
5757 const commandHistory = useRef < string [ ] > ( [ ] ) ;
58+
59+ // Track pending promises for restart-based interruption
60+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
61+ const pendingPromises = useRef < Set < ( reason : any ) => void > > ( new Set ( ) ) ;
5862
5963 const initializeWorker = useCallback ( async ( ) => {
6064 if ( ! mutex . isLocked ( ) ) {
@@ -96,6 +100,30 @@ export function WorkerProvider({
96100 const [ doInit , setDoInit ] = useState ( false ) ;
97101 const init = useCallback ( ( ) => setDoInit ( true ) , [ ] ) ;
98102
103+ // Helper function to wrap worker API calls and track pending promises
104+ // This ensures promises are rejected when the worker is terminated
105+ const trackPromise = useCallback (
106+ < T , > ( promise : Promise < T > ) : Promise < T > => {
107+ return new Promise ( ( resolve , reject ) => {
108+ // Store the reject function
109+ pendingPromises . current . add ( reject ) ;
110+
111+ promise
112+ . then ( ( result ) => {
113+ // Remove reject function on success
114+ pendingPromises . current . delete ( reject ) ;
115+ resolve ( result ) ;
116+ } )
117+ . catch ( ( error ) => {
118+ // Remove reject function on error
119+ pendingPromises . current . delete ( reject ) ;
120+ reject ( error ) ;
121+ } ) ;
122+ } ) ;
123+ } ,
124+ [ ]
125+ ) ;
126+
99127 // Initialization effect
100128 useEffect ( ( ) => {
101129 if ( doInit ) {
@@ -123,6 +151,11 @@ export function WorkerProvider({
123151 }
124152 break ;
125153 case "restart" : {
154+ // Reject all pending promises
155+ const error = "Worker interrupted" ;
156+ pendingPromises . current . forEach ( ( reject ) => reject ( error ) ) ;
157+ pendingPromises . current . clear ( ) ;
158+
126159 // Terminate the worker
127160 workerRef . current ?. terminate ( ) ;
128161 workerRef . current = null ;
@@ -169,8 +202,8 @@ export function WorkerProvider({
169202 }
170203
171204 try {
172- const { output, updatedFiles } = await workerApiRef . current . runCode (
173- code
205+ const { output, updatedFiles } = await trackPromise (
206+ workerApiRef . current . runCode ( code )
174207 ) ;
175208
176209 writeFile ( updatedFiles ) ;
@@ -191,18 +224,18 @@ export function WorkerProvider({
191224 return [ { type : "error" , message : String ( error ) } ] ;
192225 }
193226 } ,
194- [ ready , writeFile , mutex ]
227+ [ ready , writeFile , mutex , trackPromise ]
195228 ) ;
196229
197230 const checkSyntax = useCallback (
198231 async ( code : string ) : Promise < SyntaxStatus > => {
199232 if ( ! workerApiRef . current || ! ready ) return "invalid" ;
200233 const { status } = await mutex . runExclusive ( ( ) =>
201- workerApiRef . current ! . checkSyntax ( code )
234+ trackPromise ( workerApiRef . current ! . checkSyntax ( code ) )
202235 ) ;
203236 return status ;
204237 } ,
205- [ ready , mutex ]
238+ [ ready , mutex , trackPromise ]
206239 ) ;
207240
208241 const runFiles = useCallback (
@@ -233,15 +266,14 @@ export function WorkerProvider({
233266 interruptBuffer . current [ 0 ] = 0 ;
234267 }
235268 return mutex . runExclusive ( async ( ) => {
236- const { output, updatedFiles } = await workerApiRef . current ! . runFile (
237- filenames [ 0 ] ,
238- files
269+ const { output, updatedFiles } = await trackPromise (
270+ workerApiRef . current ! . runFile ( filenames [ 0 ] , files )
239271 ) ;
240272 writeFile ( updatedFiles ) ;
241273 return output ;
242274 } ) ;
243275 } ,
244- [ ready , writeFile , mutex ]
276+ [ ready , writeFile , mutex , trackPromise ]
245277 ) ;
246278
247279 return (
0 commit comments