Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7077313
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:24:28+00:00 2026-05-28T06:24:28+00:00

The Problem I’ve been developing for Android for quite some time now. One of

  • 0

The Problem

I’ve been developing for Android for quite some time now. One of the programs I’ve developed makes heavy use of WakeLocks. It usually perfectly (often for days or weeks at a time, a requirement of the program), but very rarely I notice peculiar behaviour in this code:

acquireWakeLock(wakeLockManager)

    // Preconditions
    assertFalse("Wake lock already acquired.", hasWakeLock());
    assertNotNull("Wake lock manager not provided.", wakeLockManager);

    // Acquire a wake lock.
    wakeLock = wakeLockManager.newPartialWakeLock(DEBUG_TAG);
    wakeLock.acquire();

    // Postconditions
    assertTrue("Wake Lock should be held!", hasWakeLock());

Where hasWakeLock() simply returns the result of (wakeLock != null && wakeLock.isHeld()) and wakeLockManager.newPartialWakeLock(DEBUG_TAG) encapsulates the standard “get the PowerManager and then return a wake lock” code. For the purposes of testing, the assert statements are JUnit assert methods so I think we can assume that they are correct.

The problem with the code is this: the final assertion – assertTrue(hasWakeLock()) – seems to fail every few weeks with very little explanation. It means that I have three possible issues here: (1) the wake lock is never retrieved at all from the PowerManager (2) that I have a concurrency issue that on rare occasions takes effect just before the postcondition but after the call to acquire(), or that (3) acquire() is sometimes faulty.

Investigation of the Problem

As stated above, I have three potential issues that may be occurring that I am investigating / have investigated:

Hypothesis 1: Wake Lock not returned:

Were this the case then I would see a null pointer exception. It can’t be that.

Hypothesis 2: I have a concurrency issue:

A just-conducted formal verification proof of all places involved in acquiring and releasing wakeLock strongly leads me to believe that this is not the case. In the case that my proof is faulty then I may have a concurrency issue, but it is then truly insidious and difficult to find.

Hypothesis 3: WakeLock.acquire() is faulty, and despite what the documentation says it sometimes can fail to acquire the lock:

I dislike this hypothesis because with all of the Android users our there someone aside from myself must have noticed this by now and it is almost always the developer’s code, not the library or OS code, that is faulty. Then again, stranger things have happened and this might be a genuine Android bug, albeit a rarely exhibited one. If this hypothesis is true then acquire() is simply not acquiring the wake lock and this would explain the behaviour I’m seeing.

So StackOverflow, what could be causing this problem? What do you think is wrong? Am I missing something obvious, or could this be a genuine problem with Android’s wake locks?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T06:24:29+00:00Added an answer on May 28, 2026 at 6:24 am

    It would appear that this was indeed a concurrency issue. I overlooked the fact that a relevant method could be called from a non-synchronized location in a separate thread – an obvious problem in a highly multithreaded environment!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Problem: I have an address field from an Access database which has been converted
Problem: I have two spreadsheets that each serve different purposes but contain one particular
Problem (simplified to make things clearer): 1. there is one statically-linked static.lib that has
Problem: We have a web app that calls some web services asynchronously (from the
Problem One: </a> 19-10-2011, 04:49 PM </td> <td class=thread How to fetch the DATE
Problem I have a Silverlight 5 application using the treeview from the SDK. Now
PROBLEM : I am having problems getting socket.IO to connect in some circumstances, so
Problem: Every time I start a new session of Internet Explorer, the web application
Problem: Following error is arising if I use NavigatorContent in Repeater. Is there a
Problem: user made some selection in the multi-selection listbox, then pressed a checkbox, and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.