Skip to content

Call jni_utils::init to avoid panic because of missing classes in classcache #428

Open
@Erik1000

Description

@Erik1000

I have just done a lovely 4 hour debugging session to find a panic. Because java + rust + tokio sucks ass, backtraces were invalid.
Anyhow, I do not know how nobody had this problem yet, because as soon as you connect to a Peripheral, one would have noticed that it panics inside jni-utils.
To be more precise this unwrap on a None value:
https://github.com/deviceplug/jni-utils-rs/blob/1737fd69e8d980ec045df6daafef250f2dd07ebd/rust/future.rs#L42
JFuture::from_env is being called here:

JFuture::from_env(self.env, future_obj)

The classcache gets populated here:
https://github.com/deviceplug/jni-utils-rs/blob/1737fd69e8d980ec045df6daafef250f2dd07ebd/rust/ops.rs#L382-L393
and this ops::init function is being called automatically if jni_utls::init(env) is called.

Note that btleplugs droidplug uses one of these classes in connect:
Lio/github/gedgygedgy/rust/future/Future

BUT droidplug never called jni_utils::init. In the droidplug::init function and the following call to self::jni only the btleplug classes get added to the classcache:

jni_utils::classcache::find_add_class(
env,
"com/nonpolynomial/btleplug/android/impl/Peripheral",
)?;
jni_utils::classcache::find_add_class(
env,
"com/nonpolynomial/btleplug/android/impl/ScanFilter",
)?;
jni_utils::classcache::find_add_class(
env,
"com/nonpolynomial/btleplug/android/impl/NotConnectedException",
)?;
jni_utils::classcache::find_add_class(
env,
"com/nonpolynomial/btleplug/android/impl/PermissionDeniedException",
)?;
jni_utils::classcache::find_add_class(
env,
"com/nonpolynomial/btleplug/android/impl/UnexpectedCallbackException",
)?;
jni_utils::classcache::find_add_class(
env,
"com/nonpolynomial/btleplug/android/impl/UnexpectedCharacteristicException",
)?;
jni_utils::classcache::find_add_class(
env,
"com/nonpolynomial/btleplug/android/impl/NoSuchCharacteristicException",

But jni_utils::init is never called and therefore the classcache is missing all these classes:
https://github.com/deviceplug/jni-utils-rs/blob/1737fd69e8d980ec045df6daafef250f2dd07ebd/rust/ops.rs#L384-L393

... which leads to total failure. To fix this, droidplug::init only has to also call jni_utils::init and everything works fine....

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions