Skip to content

[browser] Preserve reacquired JSObject proxies during finalization - #137654

Merged
pavelsavara merged 1 commit into
mainfrom
pavelsavara/browser-coreclr-jsobject-finalization
Nov 25, 2026
Merged

pavelsavara merged 1 commit into
mainfrom
pavelsavara/browser-coreclr-jsobject-finalization

Conversation

@pavelsavara

Copy link
Copy Markdown
Member

Summary

The JSObject identity cache uses resurrection-tracking weak references. After a collection queues a proxy's finalizer, marshaling can reacquire that same proxy before finalization runs. The queued finalizer then disposes a proxy that is live again, causing ObjectDisposedException in a subsequent import.

Keep both a long and a short weak reference in a named readonly JSObjectReference struct owned by the proxy context. Reacquisition refreshes the short reference when necessary. Finalization checks that signal under the existing context lock and re-registers instead of releasing a reacquired proxy. Explicit disposal remains immediate, and the finalizer continues through DisposeImpl.

Both weak-reference objects are rooted in the cache: storing the short reference only on the finalizable proxy allows its weak handle to be reclaimed before reacquisition.

The regression theory covers repeated reacquisition, identity preservation, eventual collection and JS-handle cleanup, and explicit disposal after deferred finalization.

Validation

CoreCLR browser, Debug, Chrome 151:

  • Fail-first: both new theory cases fail against the unchanged product because the reacquired proxy becomes disposed.
  • Final full interop suite: 511 run, 509 passed, 0 failed, 2 skipped. Build: 0 warnings, 0 errors.
.\dotnet.cmd build -bl /p:TargetOS=browser /p:TargetArchitecture=wasm /p:Configuration=Debug /p:RuntimeFlavor=CoreCLR /t:Test /p:Scenario=WasmTestOnChrome /p:InstallChromeForTests=false /p:EnableHotReloadInRuntimeConfigDevFile=false .\src\libraries\System.Runtime.InteropServices.JavaScript\tests\System.Runtime.InteropServices.JavaScript.UnitTests\System.Runtime.InteropServices.JavaScript.Tests.csproj

Result: passed with the counts above, using a matching locally cached Chrome/ChromeDriver pair. The hot-reload property avoids the known local WasmAppHost static-web-assets tooling failure.

Mono single-threaded and multithreaded execution was not run. The deterministic regression is CoreCLR single-threaded because it relies on synchronous finalizer draining. The shared implementation retains the existing context lock for threaded builds.

Explicit analyzer/style checks were attempted but did not complete cleanly: unchanged System.Threading.Channels reference sources report IDE0073; scoped formatting also reports existing import ordering and workspace-reference-load warnings. No unrelated files were changed to suppress these diagnostics.

Cost

One additional weak-reference object and weak handle per cached proxy, plus a short-reference check on cache hits. A Windows x64 .NET 10 BenchmarkDotNet model of the cache fragment measured 2.081 ns before versus 2.716 ns after, with no allocations on hits. A separate construction measurement added 24 managed bytes (48 B to 72 B); this excludes dictionary growth and unmanaged weak-handle storage. These are isolated bookkeeping measurements, not browser end-to-end results.

Regression context

The affected identity-cache logic predates #134124. The same test, exception, and helper call were reported in #93347 in 2023. A full before/after build or bisect of #134124 was not performed, so an effect on failure frequency is not excluded.

Resolves #137467

Note

This pull request was created with assistance from GitHub Copilot.

Track short and long weak references in the proxy context so a queued
finalizer can distinguish a reacquired proxy from an unreachable one.
Defer finalization without changing explicit disposal semantics.

Cover repeated reacquisition, proxy identity, explicit disposal, and
eventual cleanup with a fail-first CoreCLR browser regression theory.

Resolves #137467

Co-authored-by: Copilot <[email protected]>
@azure-pipelines

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 'arch-wasm': @lewing, @pavelsavara
See info in area-owners.md if you want to be subscribed.

@pavelsavara
pavelsavara merged commit 925a9e9 into main Nov 25, 2026
85 of 89 checks passed
@pavelsavara
pavelsavara deleted the pavelsavara/browser-coreclr-jsobject-finalization branch November 25, 2026 13:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

arch-wasm WebAssembly architecture area-System.Runtime.InteropServices.JavaScript os-browser Browser variant of arch-wasm

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Test Failure]: System.Runtime.InteropServices.JavaScript.Tests.JSImportTest.JsImportTaskTypes

2 participants