Skip to content

Fix Monitor.Wait runtime error in WASM by replacing blocking wait patterns - #60

Merged
yamayu-dev merged 3 commits into
mainfrom
copilot/fix-runtime-monitor-error
Nov 26, 2025
Merged

Fix Monitor.Wait runtime error in WASM by replacing blocking wait patterns#60
yamayu-dev merged 3 commits into
mainfrom
copilot/fix-runtime-monitor-error

Conversation

Copilot AI commented Nov 25, 2025

Copy link
Copy Markdown
Contributor

WASM single-threaded runtime throws "Cannot wait on monitors on this runtime" when user code uses .GetAwaiter().GetResult() or .Wait() on Tasks, as these internally call Monitor.Wait.

Changes

  • WasmTask.RunSync helpers: Check IsCompleted before accessing task results, avoiding blocking waits
  • Code preprocessing: Transform blocking patterns to use WasmTask.RunSync:
    • task.GetAwaiter().GetResult()WasmHelpers.WasmTask.RunSync(task)
    • task.Wait()WasmHelpers.WasmTask.RunSync(task)
  • Regex improvements: Handle one level of nested parentheses for method calls like Method(Inner())
  • Runner update: Check task completion status instead of blocking

Example

// Before: Fails with Monitor.Wait error
static void Main()
{
    RunAsync().GetAwaiter().GetResult();
}

// After preprocessing: Works in WASM
static void Main()
{
    WasmHelpers.WasmTask.RunSync(RunAsync());
}

Combined with the existing Task.DelayWasmTask.Delay transformation, async methods complete synchronously, making the IsCompleted check succeed without blocking.

Original prompt

続き
直っていない

実行エラー: Cannot wait on monitors on this runtime.

スタックトレース:
at System.Threading.Monitor.ObjWait(Int32 millisecondsTimeout, Object obj)
at System.Threading.Monitor.Wait(Object obj, Int32 millisecondsTimeout)
at System.Threading.ManualResetEventSlim.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
at System.Threading.Tasks.Task.SpinThenBlockingWait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
at System.Threading.Tasks.Task.InternalWaitCore(Int32 millisecondsTimeout, CancellationToken cancellationToken)
at System.Threading.Tasks.Task.InternalWait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
at Program.Main()
at System.Reflection.MethodBaseInvoker.InterpretedInvoke_Method(Object obj, IntPtr* args)
at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr)


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 2 commits November 25, 2025 22:50
- Added WasmTask.RunSync helper methods to safely check task completion
- Added preprocessing to transform .GetAwaiter().GetResult() to WasmTask.RunSync()
- Added preprocessing to transform .Wait() to WasmTask.RunSync()
- Updated async method runner to check IsCompleted before accessing results

Co-authored-by: yamayu-dev <57430316+yamayu-dev@users.noreply.github.com>
Co-authored-by: yamayu-dev <57430316+yamayu-dev@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix runtime error related to monitor waiting Fix Monitor.Wait runtime error in WASM by replacing blocking wait patterns Nov 25, 2025
Copilot AI requested a review from yamayu-dev November 25, 2025 22:55

@yamayu-dev yamayu-dev left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot テストして結果を報告して下さい

Copilot AI commented Nov 25, 2025

Copy link
Copy Markdown
Contributor Author

@copilot テストして結果を報告して下さい

テスト結果を報告します:

✅ 全てのテストが成功しました

  1. コード前処理テスト - 4パターン全てPASS

    • RunAsync().GetAwaiter().GetResult()WasmHelpers.WasmTask.RunSync(RunAsync())
    • task.Wait()WasmHelpers.WasmTask.RunSync(task)
    • SomeAsync().Wait()WasmHelpers.WasmTask.RunSync(SomeAsync())
    • Outer(Inner()).GetAwaiter().GetResult() → ネストした括弧も正しく変換
  2. 実行テスト - 問題のコードが正常に動作

    • 元のエラーコード(RunAsync().GetAwaiter().GetResult() + Task.Delay)が "Hello from async!" を出力
    • .Wait() パターンも正常動作

テスト結果

Copilot AI requested a review from yamayu-dev November 25, 2025 23:08
@yamayu-dev
yamayu-dev marked this pull request as ready for review November 26, 2025 09:51
@yamayu-dev
yamayu-dev merged commit 482d60d into main Nov 26, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants