@@ -20,7 +20,14 @@ import React from "react";
2020import * as Sentry from "@sentry/react" ;
2121import { makeAtomicsChannel , makeServiceWorkerChannel } from "sync-message" ;
2222
23- const workerWrapper = Comlink . wrap ( new Worker ( ) ) ;
23+ let worker , workerWrapper ;
24+
25+ function initWorker ( ) {
26+ worker = new Worker ( ) ;
27+ workerWrapper = Comlink . wrap ( worker ) ;
28+ }
29+
30+ initWorker ( ) ;
2431
2532const channelPromise = ( async ( ) => {
2633if ( typeof SharedArrayBuffer !== "undefined" ) {
@@ -93,14 +100,30 @@ export const runCode = async ({code, source}) => {
93100expected_output :questionWizard . expectedOutput ,
94101} ;
95102
96- interrupt ( ) ;
97103let interrupted = false ;
104+ let interruptResolver ;
105+ const interruptPromise = new Promise ( r => interruptResolver = r ) ;
106+ interrupt = ( ) => {
107+ doInterrupt ( ) ;
108+ interrupted = true ;
109+ }
98110
99- if ( typeof SharedArrayBuffer !== "undefined" ) {
111+ let doInterrupt ;
112+ if ( typeof SharedArrayBuffer === "undefined" ) {
113+ doInterrupt = ( ) => {
114+ worker . terminate ( ) ;
115+ initWorker ( ) ;
116+ interruptResolver ( {
117+ interrupted :true ,
118+ error :null ,
119+ passed :false ,
120+ messages :[ ] ,
121+ } ) ;
122+ }
123+ } else {
100124interruptBuffer = new Int32Array ( new SharedArrayBuffer ( Int32Array . BYTES_PER_ELEMENT * 1 ) ) ;
101- interrupt = ( ) => {
125+ doInterrupt = ( ) => {
102126interruptBuffer [ 0 ] = 2 ;
103- interrupted = true ;
104127}
105128}
106129
@@ -120,13 +143,16 @@ export const runCode = async ({code, source}) => {
120143}
121144}
122145
123- const data = await workerWrapper . runCode (
124- entry ,
125- ( await channelPromise ) . channel ,
126- interruptBuffer ,
127- Comlink . proxy ( outputCallback ) ,
128- Comlink . proxy ( inputCallback ) ,
129- ) ;
146+ const data = await Promise . race ( [
147+ interruptPromise ,
148+ workerWrapper . runCode (
149+ entry ,
150+ ( await channelPromise ) . channel ,
151+ interruptBuffer ,
152+ Comlink . proxy ( outputCallback ) ,
153+ Comlink . proxy ( inputCallback ) ,
154+ ) ,
155+ ] ) ;
130156
131157awaitingInput = false ;
132158