Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8066966

java.nio.file.Path#register clears the interrupt status on windows and ubuntu

    XMLWordPrintable

Details

    • Bug
    • Resolution: Duplicate
    • P4
    • None
    • 7u45
    • core-libs
    • x86
    • linux_ubuntu

    Description

      FULL PRODUCT VERSION :
      java version "1.7.0_45"
      Java(TM) SE Runtime Environment (build 1.7.0_45-b18)
      Java HotSpot(TM) 64-Bit Server VM (build 24.45-b08, mixed mode)


      ADDITIONAL OS VERSION INFORMATION :
      Linux jatin 3.13.0-29-generic #53-Ubuntu SMP Wed Jun 4 21:00:20 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux


      A DESCRIPTION OF THE PROBLEM :
      Calling register method on java.nio.file.Path object clears the interrupt status of the thread.

      i.e. if `Thread.currentThread().isInterrupted()` returns true. After calling `path.register(watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY)`, doing `Thread.currentThread().isInterrupted()` returns false

      ADDITIONAL REGRESSION INFORMATION:
      java version "1.7.0_45"
      Java(TM) SE Runtime Environment (build 1.7.0_45-b18)
      Java HotSpot(TM) 64-Bit Server VM (build 24.45-b08, mixed mode)


      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1) Interrupt a thread
      2) Check the interrupt status of thread to be true
      3) Let the thread register a file with the watcher service by doing say: dir.register(watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);
      4) Check the interrupt status of thread again.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Interrupt status after calling `Path#register` should be the same as it was before calling it
      ACTUAL -
      Interrupt status was cleared

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------

      import java.io.*;
      import java.nio.file.*;
      import static java.nio.file.LinkOption.*;
      import static java.nio.file.StandardWatchEventKinds.*;
      import java.nio.file.attribute.*;


      public class WatchDir {

      private final WatchService watcher;


      private void register(Path dir) throws IOException {
          //interrupt itself
          Thread.currentThread().interrupt();
          boolean before = Thread.currentThread().isInterrupted();
          WatchKey key = dir.register(watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);
          boolean after = Thread.currentThread().isInterrupted();

          if(before){
              if(!after){
                  System.out.println("--------------------BUG-------------------");
                  System.out.format("Interrupt Status: true before making call to Path#register for folder folder: %s\n", dir);
                  System.out.format("Interrupt Status: false after making call to Path#register for folder folder: %s\n", dir);
                  System.out.println("The interrupt status was cleared by `register` (Unexpected Behavior).");
                  System.exit(0);
              }else{
                  System.out.println("Not a bug on your machine. The interrupt was not cleared after call to Path#register. Works as expected");
                  System.out.println("Works well on your machine. Didn't work for me on Windows and Ubuntu with Java 7");
              }
          }
      }

      /**
       * Creates a WatchService and registers the given directory
       */
      WatchDir(Path dir) throws IOException {
          this.watcher = FileSystems.getDefault().newWatchService();
          register(dir);
      }


      public static void main(String[] args) throws Exception {
          // register directory and process its events
          Path dir = Paths.get(".");
          new WatchDir(dir);
      }}

      ---------- END SOURCE ----------

      SUPPORT :
      YES

      Attachments

        Issue Links

          Activity

            People

              Unassigned Unassigned
              webbuggrp Webbug Group
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: