Skip to content

Fix Task.Delay runtime error in WebAssembly C# runner - #59

Merged
yamayu-dev merged 3 commits into
mainfrom
copilot/fix-async-method-error
Nov 25, 2025
Merged

Fix Task.Delay runtime error in WebAssembly C# runner#59
yamayu-dev merged 3 commits into
mainfrom
copilot/fix-async-method-error

Conversation

Copilot AI commented Nov 25, 2025

Copy link
Copy Markdown
Contributor

Task.Delay compiles against reference assemblies but fails at runtime in single-threaded WASM: "Method not found: System.Threading.Tasks.Task System.Threading.Tasks.Task.Delay(int)"

Changes

  • WASM-compatible shim: Added WasmHelpers.WasmTask.Delay that returns Task.CompletedTask immediately (all 4 overloads)
  • Code preprocessing: Replace Task.DelayWasmHelpers.WasmTask.Delay before compilation
  • Parameter validation: Matches original API behavior (ArgumentOutOfRangeException for invalid values)
  • UI docs: Note that Task.Delay runs but doesn't actually delay

Example

Code that previously crashed:

async Task Main()
{
    Console.WriteLine("Start");
    await Task.Delay(1000);  // Was: Method not found
    Console.WriteLine("Done");
}

Now runs successfully (delay is a no-op in WASM).

Original prompt

続き
修正してもらったが、

実行エラー: Method not found: System.Threading.Tasks.Task System.Threading.Tasks.Task.Delay(int)

スタックトレース:
at System.Runtime.CompilerServices.AsyncMethodBuilderCore.Start[d__1](d__1& )
at Program.RunAsync()
at Program.Main()
at System.Reflection.MethodBaseInvoker.InterpretedInvoke_Method(Object obj, IntPtr* args)
at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(Object , BindingFlags )


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 2 commits November 25, 2025 11:19
…lementation

Co-authored-by: yamayu-dev <57430316+yamayu-dev@users.noreply.github.com>
…y behavior

Co-authored-by: yamayu-dev <57430316+yamayu-dev@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix execution error with missing Task.Delay method Fix Task.Delay runtime error in WebAssembly C# runner Nov 25, 2025
Copilot AI requested a review from yamayu-dev November 25, 2025 11:25
@yamayu-dev

Copy link
Copy Markdown
Owner

@copilot 説明して下さい

Copilot AI commented Nov 25, 2025

Copy link
Copy Markdown
Contributor Author

@copilot 説明して下さい

問題

Task.Delayはコンパイル時にはリファレンスアセンブリで正常にコンパイルされますが、WebAssembly実行時に「Method not found」エラーが発生していました。これは、Task.Delayがスレッディング機能に依存しており、シングルスレッドのWASM環境では実装が存在しないためです。

解決策

  1. WasmHelpers.WasmTaskクラスを追加: Task.CompletedTaskを即座に返すWASM互換のDelayメソッドを4つのオーバーロードで実装
  2. PreprocessCodeメソッド: コンパイル前にTask.DelayWasmHelpers.WasmTask.Delayに置換
  3. パラメータ検証: 元のTask.Delayと同じバリデーション(負の値でArgumentOutOfRangeException

動作

修正後:

await Task.Delay(1000);  // エラーなしで実行(ただし実際の遅延はなし)
Console.WriteLine("Done");

注意: WASM環境では実際の遅延は発生しません(no-op)。UIドキュメントにもこの制限を追記しました。

@yamayu-dev
yamayu-dev marked this pull request as ready for review November 25, 2025 22:26
@yamayu-dev
yamayu-dev merged commit 2f7f863 into main Nov 25, 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