-
Notifications
You must be signed in to change notification settings - Fork 85
Description
Hi 👋
I'm making an app when i do some bluetooth stuff - and i want to periodically run the code in background, with workmanager plugin
The plugin opens a new flutterEngine in a worker, which has it's own separate Java thread (as far as i know), and runs Dart there. Sometimes, tasks are canceled, and then engine gets closed, without any callback to Dart about it, so there is no good way to gracefull close/release anything 😒 (but this may be fixed in future)
(This case, where it closed without letting my functions finish nicely, gave me especially lot of headaches, but i'm not sure if that's because of Jni or my own code problems)
- creates the engine: https://github.com/fluttercommunity/flutter_workmanager/blob/4ce065135dc1b91fee918f81596b42a56850391d/workmanager/android/src/main/kotlin/dev/fluttercommunity/workmanager/BackgroundWorker.kt#L66-L111
- stops the engine: https://github.com/fluttercommunity/flutter_workmanager/blob/4ce065135dc1b91fee918f81596b42a56850391d/workmanager/android/src/main/kotlin/dev/fluttercommunity/workmanager/BackgroundWorker.kt#L117-L142
For very long time, i had a problem that sometimes when i opened my app, it just freezed entirely and didn't got past splash screen, without force-stoping it later - presumably, because of my tasks (with their Jni stuff) running before (maybe while) app opening, and somehow breaking everything 💀
I suspect that Jni may not like being run twice etc, so I did my best to double-triple-quadriple check if there's any Dart already running by registering it in IsolateNameServer and later checking if someone's already registered (if it is, then do not run any Jni-related code):
- checking for isolate: https://github.com/TheLastGimbus/FreeBuddy/blob/fb400c95214e9bb502dbb4d98d71e336bea3ae80/lib/headphones/cubit/headphones_connection_cubit.dart#L68-L76
- registering isolate: https://github.com/TheLastGimbus/FreeBuddy/blob/fb400c95214e9bb502dbb4d98d71e336bea3ae80/lib/headphones/cubit/headphones_connection_cubit.dart#L224-L238
I got this to work pretty well, but sometimes, i still get very deadly ANRs, probably when the worker and MainActivity start and end in some specific orders... I honestly don't know if, in those cases, any of my Jni code (like getAppContext()) actually runs, or it may be some problems with garbage collectors etc (i don't know if adding jni to my app while not running a line from it actually makes it autmatically run some inits and releases on it's own)
Here is an init() function from my libarary that uses Jni. I do not have any dispose()/close() function, because... i don't know if i should put any .release()s there or smth(?)
Summarising all of this, it would be nice if I just... you know, could safely run them twice...
If i find some time, i may write a simple test app to make capturing this easier - for now, you can compile https://github.com/TheLastGimbus/FreeBuddy/tree/fb400c95214e9bb502dbb4d98d71e336bea3ae80 if you want, the errors should be present without actually having headphones 😂 - you could force the execution of background task by copy-pasting what's in BluetoothDeviceConnectedReceiver - https://github.com/TheLastGimbus/FreeBuddy/blob/fb400c95214e9bb502dbb4d98d71e336bea3ae80/android/app/src/main/kotlin/com/lastgimbus/the/freebuddy/BluetoothDeviceConnectedReceiver.kt#L58-L66 - and triggering it with some generic broadcast receiver (that could be triggered from adb), for example 👍
// All of this mayyy be somehow related to #970 ?
Metadata
Metadata
Assignees
Type
Projects
Status