Skip to content
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

[ffigen] Allow blocks and protocols to keep their isolates alive #2017

Merged
merged 6 commits into from
Feb 19, 2025

Conversation

liamappelbe
Copy link
Contributor

@liamappelbe liamappelbe commented Feb 19, 2025

Add a keepIsolateAlive parameter to the block constructors. When true, this creates a RawReceivePort and stores it adjacent to the closure in the block closure registry. When the block is GC'd, the port is closed.

Similarly, add a keepIsolateAlive parameter to the protocol constructor. This is also implemented using a RawReceivePort, but in this case the port holds a reference to itself, and closes itself when it receives any message. That message is sent by DOBJCDartProxy's dispose method.

Fixes #447

Copy link

github-actions bot commented Feb 19, 2025

PR Health

Breaking changes ✔️
Package Change Current Version New Version Needed Version Looking good?
objective_c Breaking 6.0.0 7.0.0-wip 7.0.0 ✔️
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
objective_c _Version

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 19, 2025

Coverage Status

coverage: 87.804% (+0.2%) from 87.64%
when pulling 54364d0 on blockalive
into 63b2a88 on main.

@liamappelbe liamappelbe marked this pull request as ready for review February 19, 2025 04:05
@liamappelbe liamappelbe changed the title [ffigen] Allow blocks to keep their isolates alive [ffigen] Allow blocks and protocols to keep their isolates alive Feb 19, 2025
Copy link
Member

@HosseinYousefi HosseinYousefi left a comment

Choose a reason for hiding this comment

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

To keep the APIs similar:

In JNIgen I use one port per implemented interface, I don't store the port anywhere (should I?) but it does reference itself and when it receives a message from Java it closes itself:

(_$impls is a map from native ports to the implementation object that contains the Dart functions)

    late final jni$_.RawReceivePort $p;
    $p = jni$_.RawReceivePort(($m) {
      if ($m == null) {
        _$impls.remove($p.sendPort.nativePort);
        $p.close();
        return;
      }
      final $i = jni$_.MethodInvocation.fromMessage($m);
      final $r = _$invokeMethod($p.sendPort.nativePort, $i);
      jni$_.ProtectedJniExtensions.returnResult($i.result, $r);
    });

If I want to add keepIsolateAlive, do I have to simply set it for $p like $p.keepIsolateAlive = keepIsolateAlive?

We also need to add the mechanism we talked about with the finalizers so the objc/java side knows that the callback is no longer valid.

/// If `keepIsolateAlive` is true, this block will keep this isolate alive
/// until it is garbage collected by both Dart and ObjC.
static $blockType listener(${func.dartType} fn,
{bool keepIsolateAlive = false}) {
Copy link
Member

Choose a reason for hiding this comment

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

Isn't it safer to have this default to true?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That would be a breaking change, so I was avoiding it. But thinking about it again, you're right, it should default to true.

@liamappelbe
Copy link
Contributor Author

In JNIgen I use one port per implemented interface, I don't store the port anywhere (should I?) but it does reference itself and when it receives a message from Java it closes itself:

Yeah, that's how I implemented it for protocols too.

If I want to add keepIsolateAlive, do I have to simply set it for $p like $p.keepIsolateAlive = keepIsolateAlive?

Yep, I think that's probably all you need.

We also need to add the mechanism we talked about with the finalizers so the objc/java side knows that the callback is no longer valid.

Yeah, it's on my list.

@liamappelbe liamappelbe merged commit 350488f into main Feb 19, 2025
22 checks passed
@liamappelbe liamappelbe deleted the blockalive branch February 19, 2025 23:11
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.

Allow ObjCBlocks to keep their isolate alive
3 participants