Skip to content

Commit d4e83af

Browse files
authored
Clean-up file watcher keys. (#89429) (#89432)
Clean-up all open watcher keys in FileSettingsService.
1 parent 28a3b72 commit d4e83af

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

server/src/main/java/org/elasticsearch/reservedstate/service/FileSettingsService.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ public class FileSettingsService extends AbstractLifecycleComponent implements C
6363

6464
private volatile FileUpdateState fileUpdateState = null;
6565
private volatile WatchKey settingsDirWatchKey = null;
66+
private volatile WatchKey configDirWatchKey = null;
6667

6768
private volatile boolean active = false;
6869
private volatile boolean initialState = true;
@@ -207,6 +208,17 @@ boolean watching() {
207208
return this.watchService != null;
208209
}
209210

211+
private void cleanupWatchKeys() {
212+
if (settingsDirWatchKey != null) {
213+
settingsDirWatchKey.cancel();
214+
settingsDirWatchKey = null;
215+
}
216+
if (configDirWatchKey != null) {
217+
configDirWatchKey.cancel();
218+
configDirWatchKey = null;
219+
}
220+
}
221+
210222
synchronized void startWatcher(ClusterState clusterState, boolean onStartup) {
211223
if (watching() || active == false) {
212224
refreshExistingFileStateIfNeeded(clusterState);
@@ -246,10 +258,11 @@ synchronized void startWatcher(ClusterState clusterState, boolean onStartup) {
246258
// We watch the config directory always, even if initially we had an operator directory
247259
// it can be deleted and created later. The config directory never goes away, we only
248260
// register it once for watching.
249-
enableSettingsWatcher(null, operatorSettingsDir().getParent());
261+
configDirWatchKey = enableSettingsWatcher(configDirWatchKey, operatorSettingsDir().getParent());
250262
} catch (Exception e) {
251263
if (watchService != null) {
252264
try {
265+
cleanupWatchKeys();
253266
this.watchService.close();
254267
} catch (Exception ignore) {} finally {
255268
this.watchService = null;
@@ -323,10 +336,7 @@ synchronized void stopWatcher() {
323336
logger.debug("stopping watcher ...");
324337
if (watching()) {
325338
try {
326-
if (settingsDirWatchKey != null) {
327-
settingsDirWatchKey.cancel();
328-
settingsDirWatchKey = null;
329-
}
339+
cleanupWatchKeys();
330340
fileUpdateState = null;
331341
watchService.close();
332342
if (watcherThreadLatch != null) {

0 commit comments

Comments
 (0)