@@ -35,6 +35,7 @@ class InstallationCleaner implements Runnable {
35
35
private final BootableJarLogger logger ;
36
36
private final boolean newProcess ;
37
37
private final int retries ;
38
+ private Process process ;
38
39
39
40
InstallationCleaner (final BootableEnvironment environment , final BootableJarLogger logger ) {
40
41
this .environment = environment ;
@@ -45,28 +46,13 @@ class InstallationCleaner implements Runnable {
45
46
}
46
47
47
48
@ Override
48
- public void run () {
49
+ public synchronized void run () {
49
50
// Clean up is not already in progress
50
51
if (Files .notExists (cleanupMarker )) {
51
52
try {
52
53
Files .createFile (cleanupMarker );
53
- long timeout = environment .getTimeout () * 1000 ;
54
- final Path pidFile = environment .getPidFile ();
55
- final long wait = 500L ;
56
- while (Files .exists (pidFile )) {
57
- try {
58
- TimeUnit .MILLISECONDS .sleep (wait );
59
- } catch (InterruptedException ignore ) {
60
- break ;
61
- }
62
- timeout -= wait ;
63
- if (timeout <= 0 ) {
64
- logger .cleanupTimeout (environment .getTimeout (), pidFile );
65
- break ;
66
- }
67
- }
68
54
cleanup ();
69
- } catch (IOException e ) {
55
+ } catch (InterruptedException | IOException e ) {
70
56
logger .failedToStartCleanupProcess (e , environment .getJBossHome ());
71
57
}
72
58
}
@@ -83,7 +69,10 @@ public void run() {
83
69
*
84
70
* @throws IOException if an error occurs deleting the directory
85
71
*/
86
- void cleanup () throws IOException {
72
+ private void cleanup () throws InterruptedException , IOException {
73
+ if (Files .notExists (cleanupMarker )) {
74
+ return ;
75
+ }
87
76
if (newProcess ) {
88
77
try {
89
78
newProcess ();
@@ -104,6 +93,23 @@ void cleanup() throws IOException {
104
93
}
105
94
}
106
95
96
+ // In case of timeout, we attempt to do a cleanup only if the cleanupMarker exists
97
+ synchronized void cleanupTimeout () throws IOException , InterruptedException {
98
+ if (Files .notExists (cleanupMarker )) {
99
+ return ;
100
+ }
101
+ // In case we have a running process, kill it to avoid it to continue any cleanup.
102
+ if (newProcess && process != null ) {
103
+ process .destroyForcibly ();
104
+ process .waitFor (environment .getTimeout (), TimeUnit .SECONDS );
105
+ process = null ;
106
+ }
107
+ // Do a last cleanup, in case the cleanupMarker still exists (could have been deleted by running process).
108
+ if (Files .exists (cleanupMarker )) {
109
+ cleanup ();
110
+ }
111
+ }
112
+
107
113
private void deleteDirectory () throws IOException {
108
114
final Path installDir = environment .getJBossHome ();
109
115
Files .walkFileTree (installDir , new SimpleFileVisitor <Path >() {
@@ -136,7 +142,7 @@ public FileVisitResult postVisitDirectory(final Path dir, final IOException exc)
136
142
});
137
143
}
138
144
139
- private void newProcess () throws IOException {
145
+ private void newProcess () throws IOException , InterruptedException {
140
146
// Start a new process which will clean up the install directory. This is done in a new process in cases where
141
147
// this process may hold locks on to resources that need to be cleaned up.
142
148
final String [] cmd = {
@@ -153,7 +159,9 @@ private void newProcess() throws IOException {
153
159
.redirectError (ProcessBuilder .Redirect .INHERIT )
154
160
.redirectOutput (ProcessBuilder .Redirect .INHERIT )
155
161
.directory (new File (System .getProperty ("user.dir" )));
156
- builder .start ();
162
+ process = builder .start ();
163
+ process .waitFor (environment .getTimeout (), TimeUnit .SECONDS );
164
+ process .destroyForcibly ();
157
165
}
158
166
159
167
private String getJavaCommand () {
0 commit comments