Uploaded image for project: 'Commons Pool'
  1. Commons Pool
  2. POOL-102

Thread waiting forever for borrowObject() cannot be interrupted

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Minor
    • Resolution: Fixed
    • 1.1, 1.2, 1.3
    • 2.0
    • None

    Description

      In the following GenericObjectPool snippet inside borrowObject(), InterruptedException is caught and ignored.

      case WHEN_EXHAUSTED_BLOCK:
      try {
      if(_maxWait <= 0)

      { wait(); } else { wait(_maxWait); }
      } catch(InterruptedException e) { // ignored }

      There are two problems here:
      1) a thread waiting forever to get an object out of the pool will NEVER terminate, even if interrupted
      2) even if you put a "throw e" in, it will still be wrong because the thread's interrupted status is not preserved

      This will cause cancellation problems for threads that are inside borrowObject() that want to terminate early ONLY if they are interrupted.

      For example, if a borrow-and-wait-forever was running on an pooled executor thread in Java 1.5 and the executor service tried to cancel a task and that task had early-termination logic in it that checked interrupted status to terminate early, the task would never be cancelled.

      For us, this is minor because we are on a Tomcat request thread that has to wait for this resource to continue, but for others that have pools of stuff that are being used by time-bound tasks, it's inconvenient to write code that waits for what ends up being arbitrary time periods for a wait. It would be easier to just say wait forever and allow interruption by someone else who is watching me.

      Suggestion: make the code read like this:

      case WHEN_EXHAUSTED_BLOCK:
      try {
      if(_maxWait <= 0) { wait(); }

      else

      { wait(_maxWait); }

      } catch(InterruptedException e)

      { Thread.currentThread().interrupt(); throw e; }

      Attachments

        Activity

          People

            Unassigned Unassigned
            jdsumsion John Sumsion
            Votes:
            1 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: