Skip to content

Commit 2622cf2

Browse files
authored
fix C# resource dtor recursion issue (#947)
Give than the destructor function we export for a given exported resource will take care of removing the rep from the `RepTable`, we should not try to do that in the `Dispose(bool)` method; just call `WasmInterop.wasmImportResourceDrop`, which will call the host, which will call back into the guest, which will remove the entry. Signed-off-by: Joel Dice <[email protected]>
1 parent 67e1774 commit 2622cf2

File tree

2 files changed

+4
-3
lines changed

2 files changed

+4
-3
lines changed

crates/csharp/src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1390,6 +1390,9 @@ impl InterfaceGenerator<'_> {
13901390
[UnmanagedCallersOnly(EntryPoint = "{prefix}[dtor]{name}")]
13911391
public static unsafe void wasmExportResourceDtor{upper_camel}(int rep) {{
13921392
var val = ({qualified}) {qualified}.repTable.Remove(rep);
1393+
val.Handle = 0;
1394+
// Note we call `Dispose` here even though the handle has already been disposed in case
1395+
// the implementation has overridden `Dispose(bool)`.
13931396
val.Dispose();
13941397
}}
13951398
"#
@@ -1441,7 +1444,6 @@ impl InterfaceGenerator<'_> {
14411444
if (Handle != 0) {{
14421445
var handle = Handle;
14431446
Handle = 0;
1444-
repTable.Remove(WasmInterop.wasmImportResourceRep(handle));
14451447
WasmInterop.wasmImportResourceDrop(handle);
14461448
}}
14471449
}}

tests/runtime/resources/wasm.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ public static IExports.Z Add(IExports.Z a, IExports.Z b)
1313

1414
public static void Consume(IExports.X x)
1515
{
16-
// FIXME: c-sharp generator seems wrong here
17-
// x.Dispose();
16+
x.Dispose();
1817
}
1918

2019
public static Result<None, string> TestImports()

0 commit comments

Comments
 (0)