Skip to content

Commit 3bdafe6

Browse files
committed
Change order of closing internal/child watches in JDKFileTreeWatch
1 parent fdd24f8 commit 3bdafe6

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

src/main/java/engineering/swat/watch/impl/jdk/JDKFileTreeWatch.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -178,17 +178,20 @@ public void handleEvent(WatchEvent event) {
178178
@Override
179179
public synchronized void close() throws IOException {
180180
IOException firstFail = null;
181+
182+
var internalOpen = true;
181183
var children = childWatches.keySet().iterator();
182-
while (true) {
184+
do {
183185
try {
184-
// First, close all child watches
185-
if (children.hasNext()) {
186-
closeChildWatch(children.next());
186+
// First, close the internal watch to prevent new child watches
187+
// from being opened concurrently while this method is running.
188+
if (internalOpen) {
189+
internal.close();
190+
internalOpen = false;
187191
}
188-
// Last, close the internal watch
192+
// Next, close all child watches
189193
else {
190-
internal.close();
191-
break;
194+
closeChildWatch(children.next());
192195
}
193196
} catch (IOException ex) {
194197
logger.error("Could not close watch", ex);
@@ -197,7 +200,8 @@ public synchronized void close() throws IOException {
197200
logger.error("Could not close watch", ex);
198201
firstFail = firstFail == null ? new IOException("Unexpected exception when closing", ex) : firstFail;
199202
}
200-
}
203+
} while (children.hasNext());
204+
201205
if (firstFail != null) {
202206
throw firstFail;
203207
}

0 commit comments

Comments
 (0)