-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make weak handles created via Dart_NewWeakPersistentHandle not auto-delete. #42312
Comments
There is still a danger that |
Because we have both use cases that require weak handles (being able to dereference them) and finalizers we should then have two types in the API distinguishing these handles.
|
Yes, the |
Progress update on exploring splitting the API between There are a couple of caveats with the split:
Alternatives: A.
B. Add an
Both alternatives don't give any static checking for the problem described in this issue. So, IMO, they are not good alternatives. |
What about: We introduce |
|
Introduces Dart_NewFinalizableHandle which does auto delete itself, but does not allow accessing the weak referenced object. Issue: #42312 Change-Id: I24ea732925122c453213c4fa3f629761c352f838 Cq-Include-Trybots:dart/try:vm-ffi-android-debug-arm-try,vm-ffi-android-debug-arm64-try,app-kernel-linux-debug-x64-try,vm-kernel-linux-debug-ia32-try,vm-kernel-win-debug-x64-try,vm-kernel-win-debug-ia32-try,vm-kernel-precomp-linux-debug-x64-try,vm-dartkb-linux-release-x64-abi-try,vm-kernel-precomp-android-release-arm64-try,vm-kernel-asan-linux-release-x64-try,vm-kernel-msan-linux-release-x64-try,vm-kernel-precomp-msan-linux-release-x64-try,vm-kernel-linux-release-simarm-try,vm-kernel-linux-release-simarm64-try,vm-kernel-precomp-android-release-arm_x64-try,vm-kernel-precomp-obfuscate-linux-release-x64-try,dart-sdk-linux-try,analyzer-analysis-server-linux-try,analyzer-linux-release-try,front-end-linux-release-x64-try,vm-kernel-precomp-win-release-x64-try,vm-kernel-mac-debug-x64-try,vm-precomp-ffi-qemu-linux-release-arm-try,vm-kernel-nnbd-linux-debug-x64-try,analyzer-nnbd-linux-release-try,front-end-nnbd-linux-release-x64-try Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/154695 Reviewed-by: Ryan Macnak <rmacnak@google.com> Reviewed-by: Martin Kustermann <kustermann@google.com>
Changes Dart_NewWeakPersistentHandle to no longer auto delete the weak persistent handle. Changes the signatures of WeakPersistentHandleFinalizers to no longer have access to the handle. Flutter PR: flutter/engine#19843 g3 presubmit: cl/318028238 Issue: #42312 TEST=runtime/vm/dart_api_impl_test.cc Change-Id: I3f77db9954d9486759f903b78c03a494f73c68ba Cq-Include-Trybots:dart/try:vm-ffi-android-debug-arm-try,vm-ffi-android-debug-arm64-try,app-kernel-linux-debug-x64-try,vm-kernel-linux-debug-ia32-try,vm-kernel-win-debug-x64-try,vm-kernel-win-debug-ia32-try,vm-kernel-precomp-linux-debug-x64-try,vm-kernel-precomp-android-release-arm64-try,vm-kernel-asan-linux-release-x64-try,vm-kernel-msan-linux-release-x64-try,vm-kernel-precomp-msan-linux-release-x64-try,vm-kernel-linux-release-simarm-try,vm-kernel-linux-release-simarm64-try,vm-kernel-precomp-android-release-arm_x64-try,vm-kernel-precomp-obfuscate-linux-release-x64-try,dart-sdk-linux-try,analyzer-analysis-server-linux-try,analyzer-linux-release-try,front-end-linux-release-x64-try,vm-kernel-precomp-win-release-x64-try,vm-kernel-mac-debug-x64-try,vm-precomp-ffi-qemu-linux-release-arm-try,vm-kernel-nnbd-linux-debug-x64-try,analyzer-nnbd-linux-release-try,front-end-nnbd-linux-release-x64-try Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/151525 Commit-Queue: Daco Harkes <dacoharkes@google.com> Reviewed-by: Ryan Macnak <rmacnak@google.com> Reviewed-by: Martin Kustermann <kustermann@google.com>
I believe everything we wanted to do here has been implemented. Closing. |
Right now the VM's weak handle API exposed to embedders is a bit fragile to use: After creation of a weak persistent handle via
Dart_NewWeakPersistentHandle
it is only save to use it (e.g. viaDart_HandleFromWeakPersistent
,Dart_DeleteWeakPersistentHandle
) if one knows that the object itself is still alive.Once the object in the weak persistent handle is unreachable any following Garbage Collection can reclaim the object and in doing so will delete the weak persistent handle as well.
See the code in scavenger.cc:
which eventually calls dart_api_impl.cc:FinalizablePersistentHandle::Finalize
... state->FreeWeakPersistentHandle(handle);
=> The only usage pattern when this is safe is when the weak handle is only accessed if the code is guaranteed to have a strong reference to it (i.e. the object in the weak handle is still alive).
If the object is being deleted by the GC and native code tries to delete the weak handle on another thread (or get the object out of the weak handle) by calling the Dart API then the two threads are racing and it might lead to undefined behavior. It is not possible to prevent this via simple locking because that can lead to a deadlock.
Safe usage pattern
One usage pattern which is safe is e.g. this:
class _File extends NativeFieldWrapperClass1 { ... }
Dart_NewWeakPersistentHandle
from the file object handle and set the native field of the file to be this weak handle viaDart_SetNativeInstanceField
.file.close()
will call native code and get the weak handle from the file object and deletes it viaDart_DeleteWeakPersistentHandle
.Notice that step 3 guarantees that the file object is still alive when the weak handle gets deleted.
In case a user forgets to explicitly close the file, the GC will invoke the weak handle callback and automatically deletes the handle afterwards.
Proposed changes
We should consider changing our existing weak handle API to not auto-delete and migrate our embedders.
We can then introduce a new API to support finalization and safe auto-delete of finalizable handles by requiring the user to proof he/she has access to the object being eagerly finalized, e.g.
By forcing the programmer to provide the original object to
Dart_DeleteFinalizableHandle
we know it's still alive./cc @rmacnak-google @mraleph @dcharkes @alexmarkov
The text was updated successfully, but these errors were encountered: