Permalink
Browse files

fix: exception when watcher services are closed

When watcher are closed and there is still a running thread waiting for some event,
an java.nio.file.ClosedWatchServiceException is thrown. According to the
javadoc (https://docs.oracle.com/javase/7/docs/api/java/nio/file/WatchService.html#take%28%29)
this is the expected behavior, so we don't need to propagate this exception outside the thread.
  • Loading branch information...
a-peyrard committed Jun 8, 2015
1 parent d05094a commit be01892571b8d6b3bff04cc7b58152fd23192763
Showing with 59 additions and 54 deletions.
  1. +59 −54 restx-common/src/main/java/restx/common/watch/StdWatcherService.java
@@ -111,60 +111,65 @@ public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs)
* Process all events for keys queued to the watcher
*/
void processEvents() {
for (;;) {
// wait for key to be signalled
WatchKey key;
try {
key = watcher.take();
} catch (InterruptedException x) {
return;
}
Path dir = keys.get(key);
if (dir == null) {
System.err.println("WatchKey not recognized!!");
continue;
}
for (WatchEvent<?> event: key.pollEvents()) {
WatchEvent.Kind kind = event.kind();
// Context for directory entry event is the file name of entry
WatchEvent<Path> ev = cast(event);
coalescor.post(FileWatchEvent.newInstance(root, dir, ev.context(), ev.kind(), ev.count()));
if (kind == OVERFLOW) {
continue;
}
Path name = ev.context();
Path child = dir.resolve(name);
// if directory is created, and watching recursively, then
// register it and its sub-directories
if (recursive && (kind == ENTRY_CREATE)) {
try {
if (Files.isDirectory(child, NOFOLLOW_LINKS)) {
registerAll(child);
}
} catch (IOException x) {
// ignore to keep sample readbale
}
}
}
// reset key and remove from set if directory no longer accessible
boolean valid = key.reset();
if (!valid) {
keys.remove(key);
// all directories are inaccessible
if (keys.isEmpty()) {
break;
}
}
try {
for (;;) {
// wait for key to be signalled
WatchKey key;
try {
key = watcher.take();
} catch (InterruptedException x) {
return;
}
Path dir = keys.get(key);
if (dir == null) {
System.err.println("WatchKey not recognized!!");
continue;
}
for (WatchEvent<?> event: key.pollEvents()) {
WatchEvent.Kind kind = event.kind();
// Context for directory entry event is the file name of entry
WatchEvent<Path> ev = cast(event);
coalescor.post(FileWatchEvent.newInstance(root, dir, ev.context(), ev.kind(), ev.count()));
if (kind == OVERFLOW) {
continue;
}
Path name = ev.context();
Path child = dir.resolve(name);
// if directory is created, and watching recursively, then
// register it and its sub-directories
if (recursive && (kind == ENTRY_CREATE)) {
try {
if (Files.isDirectory(child, NOFOLLOW_LINKS)) {
registerAll(child);
}
} catch (IOException x) {
// ignore to keep sample readbale
}
}
}
// reset key and remove from set if directory no longer accessible
boolean valid = key.reset();
if (!valid) {
keys.remove(key);
// all directories are inaccessible
if (keys.isEmpty()) {
break;
}
}
}
} catch (ClosedWatchServiceException e) {
// just ignore this exception, it just meant that the service has been closed,
// while the current thread was waiting some event with the "take" method.
}
}

0 comments on commit be01892

Please sign in to comment.