Skip to content

Commit dba8960

Browse files
committed
Fix for WFCORE-7097, Intermittent bootable jar test failures of PermissionsDeploymentTestCase.testWithConfiguredMaxBootThreads
1 parent b94be1e commit dba8960

File tree

3 files changed

+40
-24
lines changed

3 files changed

+40
-24
lines changed

bootable-jar/boot/src/main/java/org/wildfly/core/jar/boot/CleanupProcessor.java

+6
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ public static void main(final String[] args) throws Exception {
3434
final Path installDir = Paths.get(args[0]);
3535
final int retries = Integer.parseInt(args[1]);
3636
final Path cleanupMarker = installDir.resolve("wildfly-cleanup-marker");
37+
if (Files.notExists(cleanupMarker)) {
38+
return;
39+
}
3740
int attempts = 1;
3841
while (attempts <= retries) {
3942
final boolean lastAttempt = attempts == retries;
@@ -51,6 +54,9 @@ public static void main(final String[] args) throws Exception {
5154
}
5255

5356
private static void cleanup(final Path installDir, final Path cleanupMarker, final boolean log) throws IOException {
57+
if (Files.notExists(cleanupMarker)) {
58+
return;
59+
}
5460
Files.walkFileTree(installDir, new SimpleFileVisitor<Path>() {
5561
@Override
5662
public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) throws IOException {

bootable-jar/runtime/src/main/java/org/wildfly/core/jar/runtime/BootableJar.java

+6-4
Original file line numberDiff line numberDiff line change
@@ -442,20 +442,22 @@ public void run() {
442442
thread.setName("installation-cleaner");
443443
return thread;
444444
});
445-
final InstallationCleaner cleaner = new InstallationCleaner(environment, log);
446-
executor.submit(cleaner);
447445
if (Files.exists(pidFile)) {
448446
waitForShutdown();
449447
}
448+
final InstallationCleaner cleaner = new InstallationCleaner(environment, log);
449+
executor.submit(cleaner);
450450
executor.shutdown();
451451
try {
452452
if (!executor.awaitTermination(environment.getTimeout(), TimeUnit.SECONDS)) {
453453
// For some reason we've timed out. The deletion should likely be executing.
454-
// We can't start a new cleanup to force it. On Windows we would have the side effect to have 2 cleaner processes to
454+
// We can't start a new full cleanup to force it. On Windows we would have the side effect to have 2 cleaner processes to
455455
// be executed, with the risk that a new installation has been installed and the new cleaner cleaning the new installation
456456
log.cleanupTimeout(environment.getTimeout(), environment.getJBossHome());
457+
// This cleanup only runs if marker still exists.
458+
cleaner.cleanupTimeout();
457459
}
458-
} catch (InterruptedException e) {
460+
} catch (IOException | InterruptedException e) {
459461
// The task has been interrupted, leaving
460462
log.cleanupTimeout(environment.getTimeout(), environment.getJBossHome());
461463
}

bootable-jar/runtime/src/main/java/org/wildfly/core/jar/runtime/InstallationCleaner.java

+28-20
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class InstallationCleaner implements Runnable {
3535
private final BootableJarLogger logger;
3636
private final boolean newProcess;
3737
private final int retries;
38+
private Process process;
3839

3940
InstallationCleaner(final BootableEnvironment environment, final BootableJarLogger logger) {
4041
this.environment = environment;
@@ -45,28 +46,13 @@ class InstallationCleaner implements Runnable {
4546
}
4647

4748
@Override
48-
public void run() {
49+
public synchronized void run() {
4950
// Clean up is not already in progress
5051
if (Files.notExists(cleanupMarker)) {
5152
try {
5253
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-
}
6854
cleanup();
69-
} catch (IOException e) {
55+
} catch (InterruptedException | IOException e) {
7056
logger.failedToStartCleanupProcess(e, environment.getJBossHome());
7157
}
7258
}
@@ -83,7 +69,10 @@ public void run() {
8369
*
8470
* @throws IOException if an error occurs deleting the directory
8571
*/
86-
void cleanup() throws IOException {
72+
private void cleanup() throws InterruptedException, IOException {
73+
if (Files.notExists(cleanupMarker)) {
74+
return;
75+
}
8776
if (newProcess) {
8877
try {
8978
newProcess();
@@ -104,6 +93,23 @@ void cleanup() throws IOException {
10493
}
10594
}
10695

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+
107113
private void deleteDirectory() throws IOException {
108114
final Path installDir = environment.getJBossHome();
109115
Files.walkFileTree(installDir, new SimpleFileVisitor<Path>() {
@@ -136,7 +142,7 @@ public FileVisitResult postVisitDirectory(final Path dir, final IOException exc)
136142
});
137143
}
138144

139-
private void newProcess() throws IOException {
145+
private void newProcess() throws IOException, InterruptedException {
140146
// Start a new process which will clean up the install directory. This is done in a new process in cases where
141147
// this process may hold locks on to resources that need to be cleaned up.
142148
final String[] cmd = {
@@ -153,7 +159,9 @@ private void newProcess() throws IOException {
153159
.redirectError(ProcessBuilder.Redirect.INHERIT)
154160
.redirectOutput(ProcessBuilder.Redirect.INHERIT)
155161
.directory(new File(System.getProperty("user.dir")));
156-
builder.start();
162+
process = builder.start();
163+
process.waitFor(environment.getTimeout(), TimeUnit.SECONDS);
164+
process.destroyForcibly();
157165
}
158166

159167
private String getJavaCommand() {

0 commit comments

Comments
 (0)