Skip to content

Enable NativeAOT finalization on WASM and WASI - #136682

Merged
MichalStrehovsky merged 5 commits into
mainfrom
wasm-finalize
Nov 5, 2026
Merged

MichalStrehovsky merged 5 commits into
mainfrom
wasm-finalize

Conversation

@MichalStrehovsky

Copy link
Copy Markdown
Member

Mirror CoreCLR's host scheduling and conditional structure, with synchronous WASM finalizer waits and separate persistent-worker and callback iteration entry points.

This also has hooks for TARGET_BROWSER, but I could pull them out.

Co-authored-by: Copilot [email protected]
Copilot-Session: 8c7bee32-d29d-4640-ac49-ec1c6ee6e8b5


Stack created with GitHub Stacks CLI • Give Feedback 💬

@azure-pipelines

azure-pipelines Bot commented Oct 30, 2026 •

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
12 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @agocke, @dotnet/ilc-contrib
See info in area-owners.md if you want to be subscribed.

MichalStrehovsky and others added 2 commits November 5, 2026 06:08
Mirror CoreCLR's host scheduling and conditional structure, with synchronous WASM finalizer waits and separate persistent-worker and callback iteration entry points.

Co-authored-by: Copilot <[email protected]>
Copilot-Session: 8c7bee32-d29d-4640-ac49-ec1c6ee6e8b5
@jkotas
jkotas requested a balanced review from Copilot November 5, 2026 00:37
@jkotas

jkotas commented Nov 5, 2026

Copy link
Copy Markdown
Member

Still draft?

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Critical finalizer scheduling gaps can indefinitely block WASI finalization and let multithreaded WASM shutdown finish without delivering ProcessExit.

5 open issues
What changed in this PR

This PR enables NativeAOT finalization on WASM/WASI using host callbacks and synchronous queue draining, while aligning CoreCLR’s finalizer behavior with threading support.

Changes:

  • Includes NativeAOT’s managed finalizer implementation in WASM builds and separates persistent-worker and single-iteration entry points.
  • Adds WASI pending-finalization scheduling, browser callback hooks, and synchronous finalizer waits for single-threaded builds.
  • Replaces selected CoreCLR WASM-specific guards with FEATURE_MULTITHREADING checks.
File Description
src/​coreclr/​vm/​finalizerthread.cpp Selects finalizer signaling and waiting behavior by threading support.
src/​coreclr/​nativeaot/​System.Private.CoreLib/​src/​System.Private.CoreLib.csproj Includes the managed finalizer implementation on WASM.
src/​coreclr/​nativeaot/​Runtime/​FinalizerHelpers.cpp Adds host scheduling, callback entry points, and synchronous draining.
src/​coreclr/​nativeaot/​Runtime.Base/​src/​System/​Runtime/​__Finalizer.cs Extracts a shared finalizer iteration and exposes a WASM callback entry point.

Review effort: Balanced

Comment thread src/coreclr/vm/finalizerthread.cpp Resolved
Co-authored-by: Copilot <[email protected]>
Copilot-Session: a5403e73-fcf9-4278-b9d0-4b1dc55ca703
@MichalStrehovsky MichalStrehovsky added the arch-wasm WebAssembly architecture label Nov 5, 2026
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to 'arch-wasm': @lewing, @pavelsavara
See info in area-owners.md if you want to be subscribed.

@MichalStrehovsky

Copy link
Copy Markdown
Member Author

Still draft?

I wasn't sure about the dead TARGET_BROWSER code. The task I gave to copilot was to mirror what CoreCLR does including ifdefs (that's why it was also not using MULTITHREADING you asked about).

I removed the dead TARGET_BROWSER code from this in the last commit and pulled out of draft.

I'm not sure what to do with the copilot feedback here. It's pointing out issues in the WASI CoreCLR implementation that this just mirrors.

We either extend the scope of this and fix it on CoreCLR too, or file that that feedback off as a bug to be looked at in both implementations.

@MichalStrehovsky
MichalStrehovsky marked this pull request as ready for review November 5, 2026 02:40
@MichalStrehovsky

Copy link
Copy Markdown
Member Author

I removed the dead TARGET_BROWSER code from this in the last commit and pulled out of draft.

Oh so we do build for TARGET_BROWSER, I just never looked if any of it makes sense. My bad, reverted that.

Comment thread src/coreclr/vm/finalizerthread.cpp Resolved
@jkotas

jkotas commented Nov 5, 2026

Copy link
Copy Markdown
Member

All CoreCLR Wasm is single threaded currently. I do not think any of the feedback is actionable in this PR - I would just fix the few ifdefs for consistency.

It may be nice to open an issue about the bugs in the CoreCLR implementation.

Co-authored-by: Copilot <[email protected]>
Copilot-Session: 1d482a30-02fe-42a3-99c9-5d133801a665
Comment thread src/coreclr/vm/finalizerthread.cpp Resolved
Comment thread src/coreclr/vm/finalizerthread.cpp Resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

arch-wasm WebAssembly architecture area-NativeAOT-coreclr

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants