MINOR: Remove possibility of overriding test files clean up
Reviewers: Chris Egerton
@@ -29,6 +29,7 @@ import org.apache.kafka.common.record.UnalignedRecords;
import org.apache.kafka.common.requests.ByteBufferChannel;
import org.apache.kafka.common.requests.RequestHeader;
import org.apache.kafka.common.utils.Exit;
+import org.apache.kafka.common.utils.KafkaThread;
import org.apache.kafka.common.utils.Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -150,13 +151,15 @@ public class TestUtils {
final File file = Files.createTempFile(prefix, suffix).toFile();
file.deleteOnExit();
- Exit.addShutdownHook("delete-temp-file-shutdown-hook", () -> {
+ // Note that we don't use Exit.addShutdownHook here because it allows for the possibility of accidently
+ // overriding the behaviour of this hook leading to leaked files.
+ Runtime.getRuntime().addShutdownHook(KafkaThread.nonDaemon("delete-temp-file-shutdown-hook", () -> {
try {
Utils.delete(file);
} catch (IOException e) {
log.error("Error deleting {}", file.getAbsolutePath(), e);
}
- });
+ }));
return file;
}
