Complete FinalizationRegistry#2432
Conversation
|
I'll review this properly later, I need to make sure I underhand the graph of references and what that implies about lifetimes. Specifically I'm worried that we're keeping |
|
It would be cool to do a formal analysis. On that end, it probably makes
sense, for example, for the item on the referencequeue to have a weak
reference to the finalization registry in case the microtasks don't run
after the registry goes out of scope.
…On Mon, Jun 15, 2026 at 4:34 AM Duncan MacGregor ***@***.***> wrote:
*aardvark179* left a comment (mozilla/rhino#2432)
<#2432 (comment)>
I'll review this properly later, I need to make sure I underhand the graph
of references and what that implies about lifetimes. Specifically I'm
worried that we're keeping FinalisationRegistry objects alive when they
should not remain so.
—
Reply to this email directly, view it on GitHub
<#2432?email_source=notifications&email_token=AAD7I2YKAKSYP52R3KAHZRT477NM3A5CNFSNUABFM5UWIORPF5TWS5BNNB2WEL2JONZXKZKDN5WW2ZLOOQXTINZQG42DSMZYGM32M4TFMFZW63VGMF2XI2DPOKSWK5TFNZ2KYZTPN52GK4S7MNWGSY3L#issuecomment-4707493837>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAD7I22D4RBQPXYKVLIYWCL477NM3AVCNFSNUABDKJSXA33TNF2G64TZHMYTOMZZG43DOO2JONZXKZJ3GQ3DMMRSHA4DONJYUF3AE>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
|
I have a bunch more ideas on this still, working... |
…compliance Complete rewrite using simplified, Android-compatible approach. Changes: - NativeWeakRef: ES2021-compliant WeakRef implementation - NativeFinalizationRegistry: ES2021-compliant implementation - ScriptRuntime: Register both constructors in ES6+ block - Messages.properties: Add validation error messages - test262.properties: All 76 tests passing ES2021 Standard Compliance: - Correctly allows unregistered symbols as weak targets - Rejects registered symbols created with Symbol.for per spec - Validates target/heldValue/token same-value constraints - Proper Symbol.toStringTag on prototypes - Error messages match spec requirements Technical Implementation: - Uses Java WeakReference for WeakRef, simple and direct - Uses PhantomReference + ReferenceQueue for FinalizationRegistry - No Cleaner API dependency, not available on Android - No Context.java modifications needed - Thread-safe with ConcurrentHashMap - Cleanup callbacks processed opportunistically during register calls - Follows modern LambdaConstructor pattern like NativeWeakMap Benefits: - Only 2 files instead of 5 - Self-contained, no helper classes or separate queue management - Android-compatible using Java 8 APIs only - Addresses reviewer feedback to use simpler, standard Java APIs
By default, finalization callbacks will not be called, and nothing will be enqueued on a ReferenceQueue, although the API will continue to function and appear to "register" and "unregister" callbacks to match the spec. If finalization is enabled, embedders of Rhino must somehow ensure that "processMicrotasks" is periodically called to prevent a too-large ReferenceQueue. How this is done depends on the framework in question, although microtask processing happens automatically when Promises are used.
3978ffd to
672c2e8
Compare
|
I think this is ready for others to look at now. By default, the API is enabled but objects get garbage collected normally. "cx.setFinalizationEnabled" must be called before anything will be actually enqueued to be finalized. Finalization callbacks happen in the "processMicrotasks" method (which is called at the exit of every top-level function and script) but if someone is using Rhino in an environment where that never gets called, registered objects will pile up and possibly cause memory problems unless the queue is periodically cleaned. |
This builds on the work already done in #2058
and #2431 to complete the implementation
of the FinalizationRegistry.
The all-important reference queue where finalization callbacks are called is
invoked as part of microtask processing by the Context. Applications that have
many Contexts in use at once will see finalizations happening in the one where
the registry was originally created. It uses PhantomReference and a
reference queue to ensure that finalizations happen when the JVM needs them
to happen.