Skip to content

Can't use async callback #10

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

Closed
WonyoungChoi opened this issue Dec 6, 2021 · 5 comments · Fixed by #19
Closed

Can't use async callback #10

WonyoungChoi opened this issue Dec 6, 2021 · 5 comments · Fixed by #19

Comments

@WonyoungChoi
Copy link
Contributor

WonyoungChoi commented Dec 6, 2021

Due to the thread problem, asynchronous callbacks are currently unavailable.

example of a problem situation

  Pointer<NativeFunction<device_changed_cb>> _batteryChangedCallback =
      Pointer.fromFunction<device_changed_cb>(_batteryChanged);

   int ret = tizen.device_add_callback(
        device_callback_e.DEVICE_CALLBACK_BATTERY_CHARGING,
        _batteryChangedCallback,
        Pointer.fromAddress(0));
    if (ret != device_error_e.DEVICE_ERROR_NONE) {
      throw Exception('Failed to register battery callback');
    }
  }

  static void _batteryChanged(
      int type, Pointer<Void> value, Pointer<Void> userData) {
    print('batteryChanged');
  }

If the charging status is changed, following error will be raised.
error: Cannot invoke native callback outside an isolate.

sample code

https://github.com/WonyoungChoi/tizen_interop_async_test

repro with sample code

  1. git clone https://github.com/WonyoungChoi/tizen_interop_async_test
  2. cd tizen_interop_async_test/example
  3. flutter_tizen run (on emulator)
  4. open "Control Panel" of the emulator and choose "Batter".
  5. Change "Charger" state to "on "or "off".
  6. Application will be terminated with following error:
[E] ../../third_party/dart/runtime/vm/runtime_entry.cc: 3621: error: Cannot invoke native callback outside an isolate.
version=2.16.1 (stable) (Tue Feb 8 12:02:33 2022 +0100) on "linux_ia32"
pid=29992, thread=29992, isolate_group=(nil)((nil)), isolate=(nil)((nil))
isolate_instructions=0, vm_instructions=a65d5620
  pc 0xa672441e fp 0xbfbe4c38 /proc/self/fd/24/lib/libflutter_engine.so+0x9c641e
[E]   pc 0xa6a15cd1 fp 0xbfbe4c58 /proc/self/fd/24/lib/libflutter_engine.so+0xcb7cd1
  pc 0xa65d57d6 fp 0xbfbe4c88 /proc/self/fd/24/lib/libflutter_engine.so+0x8777d6
  pc 0xa67730be fp 0xbfbe4cc8 /proc/self/fd/24/lib/libflutter_engine.so+0xa150be
  pc 0xa67730f0 fp 0xbfbe4ce8 /proc/self/fd/24/lib/libflutter_engine.so+0xa150f0

reason

The above code is executed on the Flutter UI thread, but most of the tizen native callbacks come from the main-loop of the main thread. Dart does not support calls between isolates.
See more: dart-lang/sdk#37022

solution?

@swift-kim
Copy link
Member

It seems not all callback-based APIs have the same issue. e.g. path_provider_tizen and shared_preferences_tizen use callback-based APIs (maybe they are just synchronous callbacks).

@roc2539
Copy link

roc2539 commented Jul 5, 2022

Have you solved your problem?

@swift-kim
Copy link
Member

@WonyoungChoi has started working on the solution but it may take quite a while to finish.

@WonyoungChoi
Copy link
Contributor Author

@roc2539 It's still going on. We are currently checking the feasibility with several solutions internally.
As soon as it goes further, you will be able to check the work situation.

@swift-kim
Copy link
Member

A related functionality (dart-lang/sdk@13ec074) will be added in Dart 3.1 but it has the following limitation:

The return type of the function passed to 'NativeCallable.listener' must be 'void'

NativeCallable.listener creates a native callable that can be invoked
from any thread. The native code that invokes the callable sends a message
back to the isolate that created the callable, and doesn't wait for a
response. So it isn't possible to return a result from the callable.

https://github.com/dart-lang/sdk/blob/af8fb2cd73c3b5ec96a39c59e85f470efe78d8a4/pkg/analyzer/tool/diagnostics/diagnostics.md#L13440-L13453

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants