Skip to content

Commit

Permalink
Report error and close on WebAssembly.RuntimeError
Browse files Browse the repository at this point in the history
  • Loading branch information
georgestagg committed Aug 5, 2024
1 parent 1af7342 commit 25d05c4
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 3 deletions.
11 changes: 10 additions & 1 deletion src/webR/chan/channel-postmessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,16 @@ export class PostMessageChannelWorker {
this.#dispatch(msg);
}
} catch (e) {
// Don't break the REPL loop on any other Wasm issues
// Close on unrecoverable error
if (e instanceof WebAssembly.RuntimeError) {
this.writeSystem({ type: 'console.error', data: e.message });
this.writeSystem({
type: 'console.error',
data: "An unrecoverable WebAssembly error has occurred, the webR worker will be closed.",
});
this.writeSystem({ type: 'close' });
}
// Don't break the REPL loop on other Wasm `Exception` errors
if (!(e instanceof (WebAssembly as any).Exception)) {
throw e;
}
Expand Down
14 changes: 13 additions & 1 deletion src/webR/chan/channel-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,19 @@ export class ServiceWorkerChannelWorker implements ChannelWorker {
}

run(args: string[]) {
Module.callMain(args);
try{
Module.callMain(args);
} catch (e) {
if (e instanceof WebAssembly.RuntimeError) {
this.writeSystem({ type: 'console.error', data: e.message });
this.writeSystem({
type: 'console.error',
data: "An unrecoverable WebAssembly error has occurred, the webR worker will be closed.",
});
this.writeSystem({ type: 'close' });
}
throw e;
}
}

setInterrupt(interrupt: () => void) {
Expand Down
14 changes: 13 additions & 1 deletion src/webR/chan/channel-shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,19 @@ export class SharedBufferChannelWorker implements ChannelWorker {
}

run(args: string[]) {
Module.callMain(args);
try{
Module.callMain(args);
} catch (e) {
if (e instanceof WebAssembly.RuntimeError) {
this.writeSystem({ type: 'console.error', data: e.message });
this.writeSystem({
type: 'console.error',
data: "An unrecoverable WebAssembly error has occurred, the webR worker will be closed.",
});
this.writeSystem({ type: 'close' });
}
throw e;
}
}

setInterrupt(interrupt: () => void) {
Expand Down
3 changes: 3 additions & 0 deletions src/webR/webr-main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,9 @@ export class WebR {
case 'console.error':
console.error(msg.data);
break;
case 'close':
this.#chan.close();
break;
default:
throw new WebRError('Unknown system message type `' + msg.type + '`');
}
Expand Down

0 comments on commit 25d05c4

Please sign in to comment.