mirror of
https://github.com/eclipse/paho.mqtt.android.git
synced 2025-05-09 02:41:15 +08:00
Bug: 480134 - Wakelock not released in AlarmPingSender
Setting up callbacks after initiating the background ping can cause a race condition, in which the operation completes before the callbacks are set. In this case, onSuccess() or onFailure() will not run, and the wakelock will never be released. To prevent this, specify a IMqttActionListener when creating the token. Change-Id: I25f898e9558d16b61e9102adcf76fe7f2335ea2f Signed-off-by: Kevin Cernekee <cernekee@google.com>
This commit is contained in:
parent
2e8a74c7e0
commit
c5a385f87c
@ -132,46 +132,38 @@ class AlarmPingSender implements MqttPingSender {
|
||||
Log.d(TAG, "Ping " + count + " times.");
|
||||
|
||||
Log.d(TAG, "Check time :" + System.currentTimeMillis());
|
||||
IMqttToken token = comms.checkForActivity();
|
||||
|
||||
// No ping has been sent.
|
||||
if (token == null) {
|
||||
return;
|
||||
}
|
||||
PowerManager pm = (PowerManager) service
|
||||
.getSystemService(Service.POWER_SERVICE);
|
||||
wakelock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, wakeLockTag);
|
||||
wakelock.acquire();
|
||||
|
||||
// Assign new callback to token to execute code after PingResq
|
||||
// arrives. Get another wakelock even receiver already has one,
|
||||
// release it until ping response returns.
|
||||
if (wakelock == null) {
|
||||
PowerManager pm = (PowerManager) service
|
||||
.getSystemService(Service.POWER_SERVICE);
|
||||
wakelock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
|
||||
wakeLockTag);
|
||||
}
|
||||
wakelock.acquire();
|
||||
token.setActionCallback(new IMqttActionListener() {
|
||||
IMqttToken token = comms.checkForActivity(new IMqttActionListener() {
|
||||
|
||||
@Override
|
||||
public void onSuccess(IMqttToken asyncActionToken) {
|
||||
Log.d(TAG, "Success. Release lock(" + wakeLockTag + "):"
|
||||
+ System.currentTimeMillis());
|
||||
//Release wakelock when it is done.
|
||||
if(wakelock != null && wakelock.isHeld()){
|
||||
wakelock.release();
|
||||
}
|
||||
wakelock.release();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(IMqttToken asyncActionToken,
|
||||
Throwable exception) {
|
||||
Throwable exception) {
|
||||
Log.d(TAG, "Failure. Release lock(" + wakeLockTag + "):"
|
||||
+ System.currentTimeMillis());
|
||||
//Release wakelock when it is done.
|
||||
if(wakelock != null && wakelock.isHeld()){
|
||||
wakelock.release();
|
||||
}
|
||||
wakelock.release();
|
||||
}
|
||||
});
|
||||
|
||||
if (token == null) {
|
||||
wakelock.release();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user