[browser] [jsimport] [mono] "Why are we accessing an entry that is not allocated" when using setTimeout
- Dominant language
- C#
- Stars
- 18.3k
- Forks
- 5.6k
- PR merge metrics
- PR metrics pending
Description
### Description
When building the latest mono wasm build (single threaded) on main and using it in a browser app I see the runtime failing under the following conditions:
### Reproduction Steps
- Create a wasm browser app with the following code included:
Program.cs
```csharp
using System;
using System.Runtime.InteropServices.JavaScript;
using System.Threading.Tasks;
Console.WriteLine("Main entered.");
public partial class MyClass
{
private static Task SomeTask { get; set; } = Task.FromResult((short)42);
[JSExport]
internal static void SetTask([JSMarshalAs>] Task task)
{
// task.ContinueWith(t =>
// {
// if (t.IsFaulted) Console.WriteLine($"C# Task threw: {t.Exception}");
// else Console.WriteLine($"C# Task result: {t.Result}");
// });
Console.WriteLine("SetTask() called.");
SomeTask = task;
}
[JSExport]
[return: JSMarshalAs>]
internal static Task GetTask()
{
return SomeTask;
}
}
```
main.js
```js
import { dotnet } from './_framework/dotnet.js'
const { getAssemblyExports, getConfig, runMain } = await dotnet
.withDiagnosticTracing(false)
.withApplicationArgumentsFromQuery()
.create();
console.log("runMain()");
runMain();
const config = getConfig();
const exports = await getAssemblyExports(config.mainAssemblyName);
try {
console.log("Passing Promise with overflowing short to C#...");
const promise = Promise.resolve(1 << 16);
exports.MyClass.SetTask(promise);
}
catch (err)
{
console.error("Passing promise to C# threw but it shouldnt have: " + err);
}
console.log("waiting for 1s...")
setTimeout(() => {
console.log("Continuing in setTimeout callback...");
console.log("Retrieving Promise we passed to C# before...");
exports.MyClass.GetTask().then((result) => {
console.log("Promise value returned: " + result);
}).catch((err) => {
console.error("The promise value threw (as expected): " + err);
});
}, 1000);
```
- run with `dotnet run`
### Expected behavior
No failure of the runtime at all, instead just
`The promise value threw (as expected): Error: Assert failed: Overflow: value 65536 is out of -32768 32767 range`
### Actual behavior
Run it with `dotnet run` and see the following errors
It gets even funkier when uncommenting the task continuation:
> :warning: The [MONO] Process terminated. warning takes about 10-20 second to appear!
### Regression?
_No response_
### Known Workarounds
Without the setTimeout, this works fine. (not really a great workaround though)
### Configuration
.NET 11 preview local build from main
### Other information
_No response_
Contributor guide
Assessment
This issue has not been assessed yet.