Skip to content

Commit e1755dc

Browse files
committed
BridgeJS: Guard FinalizationRegistry deinit callback against Wasm teardown
1 parent bbdf7d5 commit e1755dc

27 files changed

+132
-24
lines changed

Plugins/BridgeJS/Sources/BridgeJSLink/BridgeJSLink.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,19 @@ public struct BridgeJSLink {
5050
}
5151
"""
5252

53+
// The FinalizationRegistry callback wraps state.deinit() in try/catch because the
54+
// callback can fire during process shutdown after the Wasm instance is already torn down.
55+
// Calling into Wasm at that point throws RuntimeError (memory access / table index out
56+
// of bounds). There's nothing to do but swallow it - the process is exiting anyway.
5357
let swiftHeapObjectClassJs = """
5458
const swiftHeapObjectFinalizationRegistry = (typeof FinalizationRegistry === "undefined") ? { register: () => {}, unregister: () => {} } : new FinalizationRegistry((state) => {
5559
if (state.hasReleased) {
5660
return;
5761
}
5862
state.hasReleased = true;
59-
state.deinit(state.pointer);
63+
try {
64+
state.deinit(state.pointer);
65+
} catch {}
6066
});
6167
6268
/// Represents a Swift heap object like a class instance or an actor instance.

Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/ArrayTypes.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,9 @@ export async function createInstantiator(options, swift) {
340340
return;
341341
}
342342
state.hasReleased = true;
343-
state.deinit(state.pointer);
343+
try {
344+
state.deinit(state.pointer);
345+
} catch {}
344346
});
345347

346348
/// Represents a Swift heap object like a class instance or an actor instance.

Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/DefaultParameters.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,9 @@ export async function createInstantiator(options, swift) {
277277
return;
278278
}
279279
state.hasReleased = true;
280-
state.deinit(state.pointer);
280+
try {
281+
state.deinit(state.pointer);
282+
} catch {}
281283
});
282284

283285
/// Represents a Swift heap object like a class instance or an actor instance.

Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/DictionaryTypes.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,9 @@ export async function createInstantiator(options, swift) {
235235
return;
236236
}
237237
state.hasReleased = true;
238-
state.deinit(state.pointer);
238+
try {
239+
state.deinit(state.pointer);
240+
} catch {}
239241
});
240242

241243
/// Represents a Swift heap object like a class instance or an actor instance.

Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/EnumAssociatedValue.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -972,7 +972,9 @@ export async function createInstantiator(options, swift) {
972972
return;
973973
}
974974
state.hasReleased = true;
975-
state.deinit(state.pointer);
975+
try {
976+
state.deinit(state.pointer);
977+
} catch {}
976978
});
977979

978980
/// Represents a Swift heap object like a class instance or an actor instance.

Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/EnumNamespace.Global.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,9 @@ export async function createInstantiator(options, swift) {
261261
return;
262262
}
263263
state.hasReleased = true;
264-
state.deinit(state.pointer);
264+
try {
265+
state.deinit(state.pointer);
266+
} catch {}
265267
});
266268

267269
/// Represents a Swift heap object like a class instance or an actor instance.

Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/EnumNamespace.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,9 @@ export async function createInstantiator(options, swift) {
242242
return;
243243
}
244244
state.hasReleased = true;
245-
state.deinit(state.pointer);
245+
try {
246+
state.deinit(state.pointer);
247+
} catch {}
246248
});
247249

248250
/// Represents a Swift heap object like a class instance or an actor instance.

Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/JSValue.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,9 @@ export async function createInstantiator(options, swift) {
336336
return;
337337
}
338338
state.hasReleased = true;
339-
state.deinit(state.pointer);
339+
try {
340+
state.deinit(state.pointer);
341+
} catch {}
340342
});
341343

342344
/// Represents a Swift heap object like a class instance or an actor instance.

Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/MixedGlobal.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,9 @@ export async function createInstantiator(options, swift) {
209209
return;
210210
}
211211
state.hasReleased = true;
212-
state.deinit(state.pointer);
212+
try {
213+
state.deinit(state.pointer);
214+
} catch {}
213215
});
214216

215217
/// Represents a Swift heap object like a class instance or an actor instance.

Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/MixedModules.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,9 @@ export async function createInstantiator(options, swift) {
217217
return;
218218
}
219219
state.hasReleased = true;
220-
state.deinit(state.pointer);
220+
try {
221+
state.deinit(state.pointer);
222+
} catch {}
221223
});
222224

223225
/// Represents a Swift heap object like a class instance or an actor instance.

0 commit comments

Comments
 (0)