Runtime Exception - "WakeLock under-locked" in onHandleIntent of Service Intent

3,789 views
Skip to first unread message

TimHilco

unread,
Mar 26, 2010, 9:00:25 AM3/26/10
to cw-android
While conducting a stress and duration test on my app, I received the
RuntimeException "WakeLock under-locked" The application ran for 60
hours before it got the exception. I have an AlarmManager that wakes
up every 5 minutes. (I know this is alot, but I'm trying to stress the
app - The production version will have a lot higher interval).

I'm following the WakeLock/Service Intent design pattern described in
your books and on the forums. AlarmManager--> BroadbastReceiver --
>MyService (WakefulIntentService) . I'm getting the following stack
trace:

********* Start Error Message @ Fri Mar 26 01:37:48 America/New_York
2010*******************
SERVICE: DeviceDirectorService-
com.hilco.health.device.director.service.DeviceDirectorService@447fc5b8
MESSAGE: Uncaught Exception Handler:WakeLock under-locked
com.commonsware.android.syssvc.AppService.Static
THROWABLE: WakeLock under-locked
com.commonsware.android.syssvc.AppService.Static
java.lang.RuntimeException: WakeLock under-locked
com.commonsware.android.syssvc.AppService.Static
at android.os.PowerManager$WakeLock.release(PowerManager.java:304)
at android.os.PowerManager$WakeLock.release(PowerManager.java:279)
at
com.hilco.health.device.director.service.WakefulIntentService.onHandleIntent(WakefulIntentService.java:
54)
at
com.hilco.health.device.director.service.DeviceDirectorService.onHandleIntent(DeviceDirectorService.java:
212)
at android.app.IntentService
$ServiceHandler.handleMessage(IntentService.java:30)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.os.HandlerThread.run(HandlerThread.java:60)
********** End Error Message *****************************

The code for MyService:onHandleIntent
protected void onHandleIntent(Intent intent) {
Log.v(TAG,"Enter method onHandleIntent(Intent intent)
*******************************************************************
");
updateSystemStatus();
performDeviceSampling();
super.onHandleIntent(intent);
Log
.v(
TAG,
"Exit method onHandleIntent(Intent intent)
*******************************************************************
");

counter++;
}

Mark Murphy

unread,
Mar 26, 2010, 9:14:05 AM3/26/10
to cw-an...@googlegroups.com

"WakeLock under-locked" would mean that the WakeLock was released more
times than it was acquired. In theory, this should not happen except if
there is some coding bug whereby the lock is not acquired before
startService() is called.

There is one scenario I can think of in which you might get this error
somewhat legitimately:

-- You acquire the static lock in a BroadcastReceiver
-- You call startService() in the BroadcastReceiver
-- Android creates and starts the service *in a different process*
(e.g., Android terminated the process that held the BroadcastReceiver)
-- The service tries to unlock and gets this error, because we're using
a different WakeLock object, because we're in a different process

Since I worked with Ms. Hackborn on this solution, many months ago, I
assumed that this scenario would be infrequent, and that Android somehow
knows to put the started service into the same process that the
BroadcastReceiver had used.

FWIW, I have had mine going on a 5-minute interval for 24+ hours without
a hiccup, but that was on an otherwise idle device. A device that is
crazy busy might be more prone to this, particularly if it winds up
recycling the process.

The exception *should* be benign. After all, the goal is for the
WakeLock to be released, and if it was released at the time of the
exception, that's not a huge problem.

What I don't know is what happens to the original WakeLock:

-- If the static WakeLock we acquired in the BroadcastReceiver is being
released when the process is terminated, that is OK, but it means there
is still a chance that our WakefulIntentService will not get its work
done in time.

-- If the static WakeLock we acquired in the BroadcastReceiver is *not*
being released when the process is terminated, then the device will soon
have to be rebooted, because it is never going to sleep.

If somebody comes up with sample code that fairly consistently generates
this "WakeLock under-locked" condition, I can try to figure out what is
going on and determine what better defensive measures need to be taken.

--
Mark Murphy (a Commons Guy)
http://commonsware.com | http://twitter.com/commonsguy

Android Training in NYC: 1-2 May 2010: http://guruloft.com

TimHilco

unread,
Mar 26, 2010, 9:56:08 PM3/26/10
to cw-android
Thanks Mark

This wasn't the first time, I've seen this issue. I've had testing
scenarios where the app runs for days and then abruptbly stops. This
drove me to add the debugging to get more detailed information. I was
somewhat glad to see the stack trace, so could start to address the
issue.

It is having a negative effect on the app. Once I hit this error, I
stop receiving the alarms. One question I was also going to ask : "Is
there a way to see if a particular alarm is still active?

Now that I have some sense of where the issue might be, I'll put in
some additional debugging and checks to try to get more infromation.
I'll add some logging to show the pid and keep a switch/reference
counter to try to trap when the acquire/release are out of sync. If
you have any addtional thoughts for debugging information, I'll add it
in

I'm running this on an unactivated Motorola Droid. Like your testing,
it is not very busy. I was following the thread about Droid losing
AlramManagers closely to see if I was having the same issues with the
Droid.

I'll keep at it and report any new information I can gather.

Tim

On Mar 26, 8:14 am, Mark Murphy <mmur...@commonsware.com> wrote:
> TimHilco wrote:
> > While conducting a stress and duration test on my app, I received the
> > RuntimeException "WakeLock under-locked" The application ran for 60
> > hours before it got the exception. I have an AlarmManager that wakes
> > up every 5 minutes. (I know this is alot, but I'm trying to stress the
> > app - The production version will have a lot higher interval).
>
> > I'm following the WakeLock/Service Intent design pattern described in
> > your books and on the forums. AlarmManager--> BroadbastReceiver --
> >> MyService (WakefulIntentService) . I'm getting the following stack
> > trace:
>
> > ********* Start Error Message @ Fri Mar 26 01:37:48 America/New_York
> > 2010*******************
> > SERVICE: DeviceDirectorService-
> > com.hilco.health.device.director.service.DeviceDirectorService@447fc5b8
> > MESSAGE: Uncaught Exception Handler:WakeLock under-locked
> > com.commonsware.android.syssvc.AppService.Static
> > THROWABLE: WakeLock under-locked
> > com.commonsware.android.syssvc.AppService.Static
> > java.lang.RuntimeException: WakeLock under-locked
> > com.commonsware.android.syssvc.AppService.Static
> >    at android.os.PowerManager$WakeLock.release(PowerManager.java:304)
> >    at android.os.PowerManager$WakeLock.release(PowerManager.java:279)
> >    at

> > com.hilco.health.device.director.service.WakefulIntentService.onHandleInten­t(WakefulIntentService.java:
> > 54)
> >    at
> > com.hilco.health.device.director.service.DeviceDirectorService.onHandleInte­nt(DeviceDirectorService.java:

> Mark Murphy (a Commons Guy)http://commonsware.com|http://twitter.com/commonsguy
>
> Android Training in NYC: 1-2 May 2010:http://guruloft.com- Hide quoted text -
>
> - Show quoted text -

Mark Murphy

unread,
Mar 27, 2010, 6:50:17 AM3/27/10
to cw-an...@googlegroups.com
TimHilco wrote:
> "Is there a way to see if a particular alarm is still active?

Alas, no. Like other Android APIs, AlarmManager uses the "roach motel"
architecture -- data comes in, never comes out. :-(

--

_Android Programming Tutorials_ Version 2.0 Available!

Reply all
Reply to author
Forward
0 new messages