Uploaded image for project: 'Commons IO'
  1. Commons IO
  2. IO-448

FileUtils.waitFor(...) swallows thread interrupted status

    XMLWordPrintableJSON

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Minor
    • Resolution: Fixed
    • Affects Version/s: 2.4
    • Fix Version/s: 2.5
    • Component/s: Utilities
    • Labels:
      None

      Description

      The method waits for a file to appear for a given amount of time. To do so it calls Thread.sleep several times. If the thread is interrupted, the interrupt will be ignored by catching the ThreadInterrupted exception and waiting further.

      Catching the ThreadInterrupted exception automatically clears the thread's interrupted flag. Consequently the calling method has no chance to detect whether the thread was interrupted. A possible solution is to restore the interrupted status before returning - something like this:

          public static boolean waitFor(File file, int seconds) {
              int timeout = 0;
              int tick = 0;
              boolean wasInterrupted = false;
              try {
                  while (!file.exists()) {
                      // ...
                      try {
                          Thread.sleep(100);
                      } catch (InterruptedException ignore) {
                          wasInterrupted = true;
                      } catch (Exception ex) {
                          break;
                      }
                  }
                  return true;
              } finally {
                  if (wasInterrupted) {
                         Thread.currentThread.interrupt();
                  }
              }
          }
      

        Attachments

          Activity

            People

            • Assignee:
              krosenvold Kristian Rosenvold
              Reporter:
              OydvO Björn Buchner
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: