You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit ca1f976
Browse filesBrowse the repository at this point in the historyBrowse files
Defer WASI finalization to WasiEventLoop instead of running it inline
EnableFinalization is called from inside the GC. The previous WASI
implementation ran FinalizerThreadWorkerIteration synchronously from
there, which is unsafe:
- FinalizerThreadWorkerIteration declares GC_TRIGGERS, so finalizers
can request another GC while we are mid-collection.
- It transitions thread mode via EnablePreemptiveGC and back to
cooperative, which is not legal from inside a GC.
- Finalizer-triggered allocations can recurse into EnableFinalization
with unbounded stack depth.
- Tests that rely on the semantics of finalizers running on a
dedicated context (GC.WaitForPendingFinalizers and friends) observe
the wrong behavior.
Mirror the browser pattern: native EnableFinalization calls
SystemJS_ScheduleFinalization, which on WASI dispatches to a function
pointer that managed code installed at startup. The managed callback
queues SystemJS_ExecuteFinalizationCallback through
ThreadPool.UnsafeQueueUserWorkItem so it runs from WasiEventLoop's
ThreadPoolWorkQueue.Dispatch() pump, at a point where mode transitions
and re-entry into the GC are safe.
Pieces:
- src/coreclr/vm/finalizerthread.cpp
- Hoist SystemJS_ExecuteFinalizationCallback out of the
TARGET_BROWSER block so WASI can call back into it.
- Add WasiFinalizerScheduler_Register QCall that stores the managed
callback pointer, and a SystemJS_ScheduleFinalization
implementation for WASI that forwards to it.
- Replace the synchronous FinalizerThreadWorkerIteration call in
EnableFinalization with SystemJS_ScheduleFinalization.
- src/coreclr/vm/wasm/entrypoints.h, src/coreclr/vm/qcallentrypoints.cpp
- Declare and register the new WASI entry points.
- src/libraries/System.Private.CoreLib/src/System/Threading/Wasi/WasiFinalizerScheduler.cs
- New managed-side bridge. ModuleInitializer-driven Register call
installs the function pointer; the [UnmanagedCallersOnly] callback
queues the work onto ThreadPool.
Followups:
- Rename SystemJS_* to a host-neutral prefix; the JS naming is
inherited from the browser path and is misleading for WASI.
- Consider exposing SystemJS_ExecuteFinalizationCallback under a
WASI-specific name once the rename lands.
Co-authored-by: Copilot <[email protected]>
0 commit comments