Skip to content

[vm/ffi] Enable sending NativeFinalizers over Isolate.exit? #55050

Closed
@dcharkes

Description

@dcharkes

After https://dart-review.googlesource.com/c/sdk/+/354902, we will allow sending deeply immutable Finalizables to other isolates in the same group. However, if a NativeFinalizer lives in an isolate that exits, but deeply immutable Finalizables are still alive in other isolates, the native resources are prematurely finalized. Therefore, we should enable sending NativeFinalizers to another isolate via Isolate.exit.

Currently, sending NativeFinalizerss is disallowed:

HANDLE_ILLEGAL_CASE(NativeFinalizer)

// TODO(http://dartbug.com/47777): Send and exit support.

Sending a NativeFinalizer to another isolate should have the effect of changing the isolate pointer to the receiving isolate.

Some open questions:

  • What is the state in between that the message is send and that it is received? Running finalizers has the side effect of cleaning up the entries in the Dart heap via a message to the isolate. If there is temporarily no isolate while the finalizer is in flight, where should the clean up message be sent? (Presumably we can run the native finalizer while it is in flight from one isolate to another, we do have access to the heap to access the address of the native callback and the token.)
  • What is the state if the exit message is never handled?

If we do decide to support this, maybe we should add a NativeFinalizer.adopt(NativeFinalizer other) that will check if the native callback function is equal and then merge all the entries from the other finalizer. This will enable storing a finalizer in a static field and on isolate shutdown sending it to another isolate and merging it into the finalizer that's in the "same" static field already. (The alternative is that one might have a collection of adopted finalizers and can never clean up that collection.)

If we do not decide to support it, probably the best practise is that NativeFinalizers should live on, and Finalizables should be created on, an isolate that will stay alive until the end of the program (the main isolate or platform isolate). cc @liamappelbe @HosseinYousefi Do you have use cases where you want to create a Finalizable on a helper isolate that is subsequently going to exit after you've sent the Finalizable to the main isolate?

cc @mkustermann @mraleph

(Things would be much easier if we could just mark the NativeFinalizer static field as shared across all isolates in the group, a la dart-lang/language#3531, shared final NativeFinalizer = .... We would not need to worry about sending it to another isolate on exit.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3A lower priority bug or feature requestarea-core-librarySDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries.area-vmUse area-vm for VM related issues, including code coverage, and the AOT and JIT backends.library-ffitriagedIssue has been triaged by sub teamtype-enhancementA request for a change that isn't a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions