[jnigen] Migrate callbacks to Dart_CObject_kNativePointer#3452
Open
liamappelbe wants to merge 11 commits into
Open
[jnigen] Migrate callbacks to Dart_CObject_kNativePointer#3452liamappelbe wants to merge 11 commits into
Dart_CObject_kNativePointer#3452liamappelbe wants to merge 11 commits into
Conversation
PR HealthBreaking changes ✔️
This check can be disabled by tagging the PR with
API leaks
|
| Package | Leaked API symbol | Leaking sources |
|---|---|---|
| jni | $JCollection | core_bindings.dart::JCollection::implementIn::$impl core_bindings.dart::JCollection::implement::$impl |
| jni | $JIterator | core_bindings.dart::JIterator::implementIn::$impl core_bindings.dart::JIterator::implement::$impl |
| jni | $JList | core_bindings.dart::JList::implementIn::$impl core_bindings.dart::JList::implement::$impl |
| jni | $JMap$JEntry | core_bindings.dart::JMap$JEntry::implementIn::$impl core_bindings.dart::JMap$JEntry::implement::$impl |
| jni | $JMap | core_bindings.dart::JMap::implementIn::$impl core_bindings.dart::JMap::implement::$impl |
| jni | $JSet | core_bindings.dart::JSet::implementIn::$impl core_bindings.dart::JSet::implement::$impl |
This check can be disabled by tagging the PR with skip-leaking-check.
Changelog Entry ✔️
| Package | Changed Files |
|---|
Changes to files need to be accounted for in their respective changelogs.
This check can be disabled by tagging the PR with skip-changelog-check.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
There are 2 buggy callback cases we're trying to fix:
In both cases, the arguments being sent are leaked. For blocking callbacks, both cases also block the Java thread forever waiting for the response. 50331f3 contains tests for this buggy behavior.
To fix case 1 we just have to check the return value of
Dart_PostCObject_DL. If it returns false we clean up the args and mark the blocking callback result as ready.To fix case 2 we have to migrate the message
Dart_PostCObject_DLsends fromDart_CObject_kInt64toDart_CObject_kNativePointer. This allows us to attach a finalizer that will be called if the message sends ok, but the target isolate shuts down before the message is handled. I've verified that the finalizer is not called if the message fails to send (Dart_PostCObject_DLreturns false), or if the message is sent and handled successfully.The full design of this solution also involved allowing the developer to include a custom cleanup function. We're not tackling that in this PR. We'll do that later.
There was a similar bug in
PortContinuation, so I also applied the fix there.Also, JNI bundles the callback args into a Java array. Before this PR the Dart wrapper around that array was allowed to go out of scope after the callback, eventually to be GC'd. Instead we now eagerly release it. Note that this doesn't release the args themselves, just the wrapper array ref. This makes the tests more deterministic. Alternatively we could have triggered a Dart GC during the test, but eagerly releasing the array is better for performance.
Part of #3265