Skip to content

Commit 8a9c13d

Browse files
author
Kevin Cernekee
committed
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>
1 parent 02fb470 commit 8a9c13d

1 file changed

Lines changed: 12 additions & 20 deletions

File tree

  • org.eclipse.paho.android.service/org.eclipse.paho.android.service/src/main/java/org/eclipse/paho/android/service

org.eclipse.paho.android.service/org.eclipse.paho.android.service/src/main/java/org/eclipse/paho/android/service/AlarmPingSender.java

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -132,46 +132,38 @@ public void onReceive(Context context, Intent intent) {
132132
Log.d(TAG, "Ping " + count + " times.");
133133

134134
Log.d(TAG, "Check time :" + System.currentTimeMillis());
135-
IMqttToken token = comms.checkForActivity();
136135

137-
// No ping has been sent.
138-
if (token == null) {
139-
return;
140-
}
136+
PowerManager pm = (PowerManager) service
137+
.getSystemService(Service.POWER_SERVICE);
138+
wakelock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, wakeLockTag);
139+
wakelock.acquire();
141140

142141
// Assign new callback to token to execute code after PingResq
143142
// arrives. Get another wakelock even receiver already has one,
144143
// release it until ping response returns.
145-
if (wakelock == null) {
146-
PowerManager pm = (PowerManager) service
147-
.getSystemService(Service.POWER_SERVICE);
148-
wakelock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
149-
wakeLockTag);
150-
}
151-
wakelock.acquire();
152-
token.setActionCallback(new IMqttActionListener() {
144+
IMqttToken token = comms.checkForActivity(new IMqttActionListener() {
153145

154146
@Override
155147
public void onSuccess(IMqttToken asyncActionToken) {
156148
Log.d(TAG, "Success. Release lock(" + wakeLockTag + "):"
157149
+ System.currentTimeMillis());
158150
//Release wakelock when it is done.
159-
if(wakelock != null && wakelock.isHeld()){
160-
wakelock.release();
161-
}
151+
wakelock.release();
162152
}
163153

164154
@Override
165155
public void onFailure(IMqttToken asyncActionToken,
166-
Throwable exception) {
156+
Throwable exception) {
167157
Log.d(TAG, "Failure. Release lock(" + wakeLockTag + "):"
168158
+ System.currentTimeMillis());
169159
//Release wakelock when it is done.
170-
if(wakelock != null && wakelock.isHeld()){
171-
wakelock.release();
172-
}
160+
wakelock.release();
173161
}
174162
});
163+
164+
if (token == null) {
165+
wakelock.release();
166+
}
175167
}
176168
}
177169
}

0 commit comments

Comments
 (0)