Skip to content

Commit

Permalink
Bug: 480134 - Wakelock not released in AlarmPingSender
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Kevin Cernekee committed Nov 13, 2015
1 parent 02fb470 commit 8a9c13d
Showing 1 changed file with 12 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -132,46 +132,38 @@ public void onReceive(Context context, Intent intent) {
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();
}
}
}
}

0 comments on commit 8a9c13d

Please sign in to comment.