Skip to content

Commit

Permalink
Remember to close a test jar file
Browse files Browse the repository at this point in the history
The `instrumenter.nextClass()` call in this test helper method opens the
jar file that is identified by `instrumentedJarLocation`.  Previously,
nothing subsequently closed that jar file.  This open-file leak causes
trouble on Windows:  Windows refuses to delete an open file, which then
causes post-test `@TempDir` cleanup to fail.

Now we explicitly close `instrumenter`, which in turn closes the jar
file, which in turn allows post-test `@TempDir` cleanup to succeed on
Windows.
  • Loading branch information
liblit committed Aug 6, 2023
1 parent dce789a commit caffc53
Showing 1 changed file with 19 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -150,16 +150,18 @@ public void emitTo(Output w) {
}

private void write() throws IllegalStateException, IOException, InvalidClassFileException {
// Write all modified classes
for (ClassInstrumenter ci2 : classInstrumenters) {
if (ci2.isChanged()) {
ClassWriter cw = ci2.emitClass();
instrumenter.outputModifiedClass(ci2, cw);
try {
// Write all modified classes
for (ClassInstrumenter ci2 : classInstrumenters) {
if (ci2.isChanged()) {
ClassWriter cw = ci2.emitClass();
instrumenter.outputModifiedClass(ci2, cw);
}
}
} finally {
// Finally write the instrumented jar
instrumenter.close();
}

// Finally write the instrumented jar
instrumenter.close();
}

private void setValidationInstrumenter() throws IOException {
Expand All @@ -169,11 +171,15 @@ private void setValidationInstrumenter() throws IOException {
instrumenter.addInputJar(instrumentedJarLocation.toFile());
instrumenter.beginTraversal();

// To be able to reuse all classes from shrike save them in a new list
classInstrumenters = new ArrayList<>();
ClassInstrumenter ci = null;
while ((ci = instrumenter.nextClass()) != null) {
classInstrumenters.add(ci);
try {
// To be able to reuse all classes from shrike save them in a new list
classInstrumenters = new ArrayList<>();
ClassInstrumenter ci = null;
while ((ci = instrumenter.nextClass()) != null) {
classInstrumenters.add(ci);
}
} finally {
instrumenter.close();
}
}

Expand Down

0 comments on commit caffc53

Please sign in to comment.