From 71c15cb65e93eec439d3f86ab4b22986b64f4a6b Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Mon, 2 Oct 2017 20:03:17 +0100 Subject: [PATCH] Avoid possible livelock when stopping FileSystemWatcher in parallel Previously, if the file watcher thread tried to stop the FileSystemWatcher when another thread was already stopping it a livelock could occur. The livelock occurred because the file watcher thread would attempt to lock a monitor that was being held by a thread that had joined the file watcher thread and was waiting for it to die. This commit avoid the livelock by narrowing the synchronization that's used when stopping the FileSystemWatcher. The monitor is used to obtain a reference to the file watcher thread in a thread-safe manner, but it is released prior to joining the file watcher thread and waiting for it to die. This will allow a parallel attempt by the file watcher thread to stop itself to succeed. Closes gh-10496 --- .../devtools/filewatch/FileSystemWatcher.java | 23 ++++++++++--------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/filewatch/FileSystemWatcher.java b/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/filewatch/FileSystemWatcher.java index 18cf85e1309..c3530b3b7fd 100644 --- a/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/filewatch/FileSystemWatcher.java +++ b/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/filewatch/FileSystemWatcher.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2016 the original author or authors. + * Copyright 2012-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -183,22 +183,23 @@ public class FileSystemWatcher { * @param remainingScans the number of remaining scans */ void stopAfter(int remainingScans) { + Thread thread = null; synchronized (this.monitor) { - Thread thread = this.watchThread; + thread = this.watchThread; if (thread != null) { this.remainingScans.set(remainingScans); if (remainingScans <= 0) { thread.interrupt(); } - if (Thread.currentThread() != thread) { - try { - thread.join(); - } - catch (InterruptedException ex) { - Thread.currentThread().interrupt(); - } - } - this.watchThread = null; + } + this.watchThread = null; + } + if (Thread.currentThread() != thread) { + try { + thread.join(); + } + catch (InterruptedException ex) { + Thread.currentThread().interrupt(); } } }