Skip to content

[jni] Fix interface implementation deadlock on the main thread #2032

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

Merged
merged 2 commits into from
Feb 27, 2025

Conversation

HosseinYousefi
Copy link
Member

Closes #1908 using the proposed method in #1908 (comment).

I tested this locally, but we don't have the setup to automate Android tests on CI: #1155.

Copy link

PR Health

Breaking changes ✔️
Package Change Current Version New Version Needed Version Looking good?
jni Non-Breaking 0.14.0 0.14.1-wip 0.14.1 ✔️
Changelog Entry ✔️
Package Changed Files

Changes to files need to be accounted for in their respective changelogs.

API leaks ⚠️

The following packages contain symbols visible in the public API, but not exported by the library. Export these symbols or remove them from your publicly visible API.

Package Leaked API symbols
jni _ReferenceType
_opaque_pthread_mutex_t
_opaque_pthread_cond_t
_Dart_FinalizableHandle

This check can be disabled by tagging the PR with skip-leaking-check.

License Headers ✔️
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
Files
no missing headers

All source files should start with a license header.

Unrelated files missing license headers
Files
pkgs/jni/lib/src/third_party/generated_bindings.dart
pkgs/objective_c/lib/src/ns_input_stream.dart

@coveralls
Copy link

coveralls commented Feb 26, 2025

Coverage Status

coverage: 87.43%. remained the same
when pulling dde2231 on interface-deadlock
into ae32a51 on main.

//
// When the current isolate is `null`, enter the main isolate that is pinned
// to the main thread first before invoking the `functionPtr`.
bool mustEnterIsolate = Dart_CurrentIsolate_DL() == NULL && mayEnterIsolate;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Though I just noticed another possible edge case.

If we're on the owner thread (mayEnterIsolate is true), and not entered into the target isolate, but instead entered into a different isolate. Then we should exit that other isolate, enter the target isolate, run the callback, exit the target isolate, then enter the other isolate. Of course, there's always the possibility that the other isolate is not owned by this thread, so could be entered by another thread while we're in the target isolate. What a mess.

Anyway, none of that is possible ATM, but could come up in future. Maybe just add an assert that Dart_CurrentIsolate_DL() is either null or isolateId?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@HosseinYousefi HosseinYousefi merged commit cde3a28 into main Feb 27, 2025
27 checks passed
@HosseinYousefi HosseinYousefi deleted the interface-deadlock branch February 27, 2025 12:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

JObject.implement deadlocks
3 participants