Skip to content

Commit 5f919c9

Browse files
committed
fix(wasm): Gemini R5 — handleStart OOM, error masking, restore leak
- handleStart: move allocOutPtr inside try block so OOM frees the active handle instead of leaking it - readProgress COMPLETE: return error when isErr=1 but JSON is null instead of silently reporting success - adaptResultForDart: isError flag alone triggers error path even when parsed.error key is missing (prevents error masking) - handleRestore: add catch block to free outError on WASM trap before rethrowing
1 parent 43ef9c1 commit 5f919c9

1 file changed

Lines changed: 16 additions & 5 deletions

File tree

packages/dart_monty_wasm/js/src/worker_src.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ async function initWasm() {
4848
*/
4949
function adaptResultForDart(cabiResultJson, isError) {
5050
const parsed = JSON.parse(cabiResultJson);
51-
if (isError && parsed.error) {
52-
const err = typeof parsed.error === 'object'
51+
if (isError) {
52+
const err = (parsed.error && typeof parsed.error === 'object')
5353
? parsed.error
54-
: { message: String(parsed.error) };
54+
: { message: parsed.error ? String(parsed.error) : 'Unknown error' };
5555
return {
5656
ok: false,
5757
error: err.message || String(err),
@@ -85,6 +85,13 @@ function readProgress(id, handle, tag, errMsg) {
8585
const adapted = adaptResultForDart(json, isErr === 1);
8686
return { type: 'result', id, ...adapted, state: adapted.ok ? 'complete' : undefined };
8787
}
88+
if (isErr === 1) {
89+
return {
90+
type: 'result', id, ok: false,
91+
error: 'Execution failed (no error context)',
92+
errorType: 'MontyException',
93+
};
94+
}
8895
return { type: 'result', id, ok: true, state: 'complete', value: null };
8996
}
9097

@@ -279,12 +286,13 @@ function handleStart(id, code, extFns, limits, scriptName) {
279286

280287
activeHandle = handle;
281288

282-
const outErr = allocOutPtr();
289+
let outErr;
283290
let tag;
284291
try {
292+
outErr = allocOutPtr();
285293
tag = wasm.monty_start(handle, outErr.ptr);
286294
} catch (e) {
287-
outErr.free();
295+
if (outErr) outErr.free();
288296
activeHandle = null;
289297
wasm.monty_free(handle);
290298
self.postMessage({
@@ -592,6 +600,9 @@ function handleRestore(id, dataBase64) {
592600
let handle;
593601
try {
594602
handle = wasm.monty_restore(ptr, bytes.length, outError.ptr);
603+
} catch (e) {
604+
outError.free();
605+
throw e;
595606
} finally {
596607
wasm.monty_dealloc(ptr, bytes.length);
597608
}

0 commit comments

Comments
 (0)