Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 0d052c95 authored by Greg Kaiser's avatar Greg Kaiser
Browse files

ContextHubService: Update caches on nanoapp unload

When we unloaded a nanoapp, we weren't updating the cache of
nanoapp information at the ContextHubServer java layer.  Also,
we were leaking the handle.

We fix both of these by invoking the shared delete_app_instance()
function which properly handles all of this.

Furthermore, we allow delete_app_instance() to accept a nullptr
for the JNIEnv, and fix invalidateNanoApps() so that it won't
crash if it's unable to connect to the JVM.

Note that our Java communication in general here should be
cleaned up, but we're too late in the release cycle for
that, and leave that for b/30961119.

Bug: 30951974, 30968860
Change-Id: Ibb07666a8d54618bddaa3eaf36f9578926eb2b0f
parent 4ecc9d2d
Loading
Loading
Loading
Loading
+27 −13
Original line number Diff line number Diff line
@@ -386,24 +386,30 @@ static int add_app_instance(const hub_app_info *appInfo, uint32_t hubHandle,
}

int delete_app_instance(int id, JNIEnv *env) {
    if (!db.appInstances.count(id)) {
        ALOGW("Cannot find App id : %d", id);
        return -1;
    }
    bool fullyDeleted = true;

    return_id(id);
    if (db.appInstances.count(id)) {
        db.appInstances.erase(id);
    if (env->CallIntMethod(db.jniInfo.jContextHubService,
    } else {
        ALOGW("Cannot delete App id (%d) from the JNI C++ cache", id);
        fullyDeleted = false;
    }
    return_id(id);

    if ((env == nullptr) ||
        (env->CallIntMethod(db.jniInfo.jContextHubService,
                       db.jniInfo.contextHubServiceDeleteAppInstance,
                       id) != 0) {
        ALOGW("Could not delete App id : %d", id);
        return -1;
                       id) != 0)) {
        ALOGW("Cannot delete App id (%d) from Java cache", id);
        fullyDeleted = false;
    }

    if (fullyDeleted) {
        ALOGI("Deleted App id : %d", id);

        return 0;
    }
    return -1;
}

static int startLoadAppTxn(uint64_t appId, int hubHandle) {
    app_instance_info_s *txnInfo = (app_instance_info_s *)malloc(sizeof(app_instance_info_s));
@@ -613,7 +619,14 @@ void closeUnloadTxn(bool success) {

    if (success && fetchTxnData(&txnId, &txnData) == 0 &&
        txnId == CONTEXT_HUB_UNLOAD_APP) {
        db.appInstances.erase(*(uint32_t *)txnData);
        JNIEnv *env;
        if ((db.jniInfo.vm)->AttachCurrentThread(&env, nullptr) != JNI_OK) {
            ALOGW("Could not attach to JVM !");
            env = nullptr;
        }
        // TODO(b/30806218): Stop treating 'int' and uint32_t as the same type.
        int handle = *(uint32_t *)txnData;
        delete_app_instance(handle, env);
    } else {
        ALOGW("Could not unload the app successfully ! success %d, txnData %p", success, txnData);
    }
@@ -668,6 +681,7 @@ static void invalidateNanoApps(uint32_t hubHandle) {

    if ((db.jniInfo.vm)->AttachCurrentThread(&env, nullptr) != JNI_OK) {
        ALOGW("Could not attach to JVM !");
        env = nullptr;
    }

    auto end = db.appInstances.end();