8153925: (fs) WatchService hangs on GetOverlappedResult and locks directory (win)
Co-authored-by: Thomas Mader <tmader@redhat.com> Reviewed-by: alanb
This commit is contained in:
parent
b3854d5830
commit
39846dcf8d
@ -113,6 +113,10 @@ class WindowsWatchService
|
|||||||
// completion key (used to map I/O completion to WatchKey)
|
// completion key (used to map I/O completion to WatchKey)
|
||||||
private int completionKey;
|
private int completionKey;
|
||||||
|
|
||||||
|
// flag indicates that ReadDirectoryChangesW failed
|
||||||
|
// and overlapped I/O operation wasn't started
|
||||||
|
private boolean errorStartingOverlapped;
|
||||||
|
|
||||||
WindowsWatchKey(Path dir,
|
WindowsWatchKey(Path dir,
|
||||||
AbstractWatchService watcher,
|
AbstractWatchService watcher,
|
||||||
FileKey fileKey)
|
FileKey fileKey)
|
||||||
@ -175,6 +179,14 @@ class WindowsWatchService
|
|||||||
return completionKey;
|
return completionKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void setErrorStartingOverlapped(boolean value) {
|
||||||
|
errorStartingOverlapped = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
boolean isErrorStartingOverlapped() {
|
||||||
|
return errorStartingOverlapped;
|
||||||
|
}
|
||||||
|
|
||||||
// Invalidate the key, assumes that resources have been released
|
// Invalidate the key, assumes that resources have been released
|
||||||
void invalidate() {
|
void invalidate() {
|
||||||
((WindowsWatchService)watcher()).poller.releaseResources(this);
|
((WindowsWatchService)watcher()).poller.releaseResources(this);
|
||||||
@ -182,6 +194,7 @@ class WindowsWatchService
|
|||||||
buffer = null;
|
buffer = null;
|
||||||
countAddress = 0;
|
countAddress = 0;
|
||||||
overlappedAddress = 0;
|
overlappedAddress = 0;
|
||||||
|
errorStartingOverlapped = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -455,12 +468,14 @@ class WindowsWatchService
|
|||||||
* resources.
|
* resources.
|
||||||
*/
|
*/
|
||||||
private void releaseResources(WindowsWatchKey key) {
|
private void releaseResources(WindowsWatchKey key) {
|
||||||
|
if (!key.isErrorStartingOverlapped()) {
|
||||||
try {
|
try {
|
||||||
CancelIo(key.handle());
|
CancelIo(key.handle());
|
||||||
GetOverlappedResult(key.handle(), key.overlappedAddress());
|
GetOverlappedResult(key.handle(), key.overlappedAddress());
|
||||||
} catch (WindowsException expected) {
|
} catch (WindowsException expected) {
|
||||||
// expected as I/O operation has been cancelled
|
// expected as I/O operation has been cancelled
|
||||||
}
|
}
|
||||||
|
}
|
||||||
CloseHandle(key.handle());
|
CloseHandle(key.handle());
|
||||||
closeAttachedEvent(key.overlappedAddress());
|
closeAttachedEvent(key.overlappedAddress());
|
||||||
key.buffer().free();
|
key.buffer().free();
|
||||||
@ -628,6 +643,7 @@ class WindowsWatchService
|
|||||||
} catch (WindowsException x) {
|
} catch (WindowsException x) {
|
||||||
// no choice but to cancel key
|
// no choice but to cancel key
|
||||||
criticalError = true;
|
criticalError = true;
|
||||||
|
key.setErrorStartingOverlapped(true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (criticalError) {
|
if (criticalError) {
|
||||||
|
102
jdk/test/java/nio/file/WatchService/DeleteInterference.java
Normal file
102
jdk/test/java/nio/file/WatchService/DeleteInterference.java
Normal file
@ -0,0 +1,102 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2016, Red Hat, Inc. and/or its affiliates.
|
||||||
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
|
*
|
||||||
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
|
* under the terms of the GNU General Public License version 2 only, as
|
||||||
|
* published by the Free Software Foundation.
|
||||||
|
*
|
||||||
|
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||||
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||||
|
* version 2 for more details (a copy is included in the LICENSE file that
|
||||||
|
* accompanied this code).
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License version
|
||||||
|
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||||
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
* @bug 8153925
|
||||||
|
* @summary Tests potential interference between a thread creating and closing
|
||||||
|
* a WatchService with another thread that is deleting and re-creating the
|
||||||
|
* directory at around the same time. This scenario tickled a timing bug
|
||||||
|
* in the Windows implementation.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.file.DirectoryStream;
|
||||||
|
import java.nio.file.FileSystem;
|
||||||
|
import java.nio.file.FileSystems;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.nio.file.WatchService;
|
||||||
|
import java.util.concurrent.ExecutorService;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
import java.util.concurrent.Future;
|
||||||
|
|
||||||
|
import static java.nio.file.StandardWatchEventKinds.*;
|
||||||
|
|
||||||
|
public class DeleteInterference {
|
||||||
|
|
||||||
|
private static final int ITERATIONS_COUNT = 1024;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Execute two tasks in a thread pool. One task loops on creating and
|
||||||
|
* closing a WatchService, the other task deletes and re-creates the
|
||||||
|
* directory.
|
||||||
|
*/
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
Path dir = Files.createTempDirectory("work");
|
||||||
|
ExecutorService pool = Executors.newCachedThreadPool();
|
||||||
|
try {
|
||||||
|
Future<?> task1 = pool.submit(() -> openAndCloseWatcher(dir));
|
||||||
|
Future<?> task2 = pool.submit(() -> deleteAndRecreateDirectory(dir));
|
||||||
|
task1.get();
|
||||||
|
task2.get();
|
||||||
|
} finally {
|
||||||
|
pool.shutdown();
|
||||||
|
deleteFileTree(dir);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void openAndCloseWatcher(Path dir) {
|
||||||
|
FileSystem fs = FileSystems.getDefault();
|
||||||
|
for (int i = 0; i < ITERATIONS_COUNT; i++) {
|
||||||
|
try (WatchService watcher = fs.newWatchService()) {
|
||||||
|
dir.register(watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);
|
||||||
|
} catch (IOException ioe) {
|
||||||
|
// ignore
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void deleteAndRecreateDirectory(Path dir) {
|
||||||
|
for (int i = 0; i < ITERATIONS_COUNT; i++) {
|
||||||
|
try {
|
||||||
|
deleteFileTree(dir);
|
||||||
|
Path subdir = Files.createDirectories(dir.resolve("subdir"));
|
||||||
|
Files.createFile(subdir.resolve("test"));
|
||||||
|
} catch (IOException ioe) {
|
||||||
|
// ignore
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void deleteFileTree(Path file) {
|
||||||
|
try {
|
||||||
|
if (Files.isDirectory(file)) {
|
||||||
|
try (DirectoryStream<Path> stream = Files.newDirectoryStream(file)) {
|
||||||
|
for (Path pa : stream) {
|
||||||
|
deleteFileTree(pa);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Files.delete(file);
|
||||||
|
} catch (IOException ioe) {
|
||||||
|
// ignore
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user