Skip to content

Commit

Permalink
[android] Fix deadlock in DNS-SD callback (#11186)
Browse files Browse the repository at this point in the history
  • Loading branch information
Damian-Nordic authored Oct 29, 2021
1 parent 2475f10 commit 69423bc
Showing 1 changed file with 21 additions and 11 deletions.
32 changes: 21 additions & 11 deletions src/platform/android/DnssdImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@

#include "DnssdImpl.h"

#include <cstddef>
#include <jni.h>
#include <lib/support/CHIPJNIError.h>
#include <lib/support/JniReferences.h>
#include <lib/support/JniTypeWrappers.h>

#include <lib/dnssd/platform/Dnssd.h>
#include <lib/support/CHIPJNIError.h>
#include <lib/support/CHIPMemString.h>
#include <lib/support/CodeUtils.h>
#include <lib/support/JniReferences.h>
#include <lib/support/JniTypeWrappers.h>
#include <lib/support/SafeInt.h>
#include <lib/support/logging/CHIPLogging.h>
#include <platform/internal/CHIPDeviceLayerInternal.h>

#include <cstddef>
#include <jni.h>
#include <string>

namespace chip {
Expand Down Expand Up @@ -65,7 +65,10 @@ CHIP_ERROR ChipDnssdRemoveServices()
VerifyOrReturnError(sResolverObject != nullptr && sRemoveServicesMethod != nullptr, CHIP_ERROR_INCORRECT_STATE);
JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread();

env->CallVoidMethod(sResolverObject, sRemoveServicesMethod);
{
DeviceLayer::StackUnlock unlock;
env->CallVoidMethod(sResolverObject, sRemoveServicesMethod);
}

if (env->ExceptionCheck())
{
Expand Down Expand Up @@ -114,8 +117,11 @@ CHIP_ERROR ChipDnssdPublishService(const DnssdService * service)
env->SetObjectArrayElement(subTypes, i, jniSubType.jniValue());
}

env->CallVoidMethod(sResolverObject, sPublishMethod, jniName.jniValue(), jniHostName.jniValue(), jniServiceType.jniValue(),
service->mPort, keys, datas, subTypes);
{
DeviceLayer::StackUnlock unlock;
env->CallVoidMethod(sResolverObject, sPublishMethod, jniName.jniValue(), jniHostName.jniValue(), jniServiceType.jniValue(),
service->mPort, keys, datas, subTypes);
}

if (env->ExceptionCheck())
{
Expand Down Expand Up @@ -154,8 +160,11 @@ CHIP_ERROR ChipDnssdResolve(DnssdService * service, Inet::InterfaceId interface,
UtfString jniInstanceName(env, service->mName);
UtfString jniServiceType(env, serviceType.c_str());

env->CallVoidMethod(sResolverObject, sResolveMethod, jniInstanceName.jniValue(), jniServiceType.jniValue(),
reinterpret_cast<jlong>(callback), reinterpret_cast<jlong>(context), sMdnsCallbackObject);
{
DeviceLayer::StackUnlock unlock;
env->CallVoidMethod(sResolverObject, sResolveMethod, jniInstanceName.jniValue(), jniServiceType.jniValue(),
reinterpret_cast<jlong>(callback), reinterpret_cast<jlong>(context), sMdnsCallbackObject);
}

if (env->ExceptionCheck())
{
Expand Down Expand Up @@ -209,6 +218,7 @@ void HandleResolve(jstring instanceName, jstring serviceType, jstring address, j
VerifyOrReturn(callbackHandle != 0, ChipLogError(Discovery, "HandleResolve called with callback equal to nullptr"));

const auto dispatch = [callbackHandle, contextHandle](CHIP_ERROR error, DnssdService * service = nullptr) {
DeviceLayer::StackLock lock;
DnssdResolveCallback callback = reinterpret_cast<DnssdResolveCallback>(callbackHandle);
callback(reinterpret_cast<void *>(contextHandle), service, error);
};
Expand Down

0 comments on commit 69423bc

Please sign in to comment.