Skip to content

Commit f8f66f3

Browse files
larsrc-googlecopybara-github
authored andcommitted
Make SimpleLogHandler not swallow interrupts.
RELNOTES: n/a PiperOrigin-RevId: 346163236
1 parent d91e5b4 commit f8f66f3

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

src/main/java/com/google/devtools/build/lib/util/SimpleLogHandler.java

+29
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.io.File;
2727
import java.io.FileOutputStream;
2828
import java.io.IOException;
29+
import java.io.InterruptedIOException;
2930
import java.io.OutputStreamWriter;
3031
import java.lang.management.ManagementFactory;
3132
import java.nio.file.DirectoryStream;
@@ -413,6 +414,8 @@ public synchronized void publish(LogRecord record) {
413414
return;
414415
}
415416

417+
// This allows us to do the I/O while not forgetting that we were interrupted.
418+
boolean isInterrupted = Thread.interrupted();
416419
try {
417420
String message = getFormatter().format(record);
418421
openOutputIfNeeded();
@@ -421,6 +424,9 @@ public synchronized void publish(LogRecord record) {
421424
reportError(null, e, ErrorManager.WRITE_FAILURE);
422425
// Failing to log is non-fatal. Continue to try to rotate the log if necessary, which may fix
423426
// the underlying IO problem with the file.
427+
if (e instanceof InterruptedIOException) {
428+
isInterrupted = true;
429+
}
424430
}
425431

426432
try {
@@ -430,35 +436,58 @@ public synchronized void publish(LogRecord record) {
430436
}
431437
} catch (IOException e) {
432438
reportError("Failed to rotate log file", e, ErrorManager.GENERIC_FAILURE);
439+
if (e instanceof InterruptedIOException) {
440+
isInterrupted = true;
441+
}
442+
}
443+
if (isInterrupted) {
444+
Thread.currentThread().interrupt();
433445
}
434446
}
435447

436448
@Override
437449
public synchronized void flush() {
450+
boolean isInterrupted = Thread.interrupted();
438451
if (output.isOpen()) {
439452
try {
440453
output.flush();
441454
} catch (IOException e) {
442455
reportError(null, e, ErrorManager.FLUSH_FAILURE);
456+
if (e instanceof InterruptedIOException) {
457+
isInterrupted = true;
458+
}
443459
}
444460
}
461+
if (isInterrupted) {
462+
Thread.currentThread().interrupt();
463+
}
445464
}
446465

447466
@Override
448467
public synchronized void close() {
468+
boolean isInterrupted = Thread.interrupted();
449469
if (output.isOpen()) {
450470
try {
451471
output.write(getFormatter().getTail(this));
452472
} catch (IOException e) {
453473
reportError("Failed to write log tail", e, ErrorManager.WRITE_FAILURE);
474+
if (e instanceof InterruptedIOException) {
475+
isInterrupted = true;
476+
}
454477
}
455478

456479
try {
457480
output.close();
458481
} catch (IOException e) {
459482
reportError(null, e, ErrorManager.CLOSE_FAILURE);
483+
if (e instanceof InterruptedIOException) {
484+
isInterrupted = true;
485+
}
460486
}
461487
}
488+
if (isInterrupted) {
489+
Thread.currentThread().interrupt();
490+
}
462491
}
463492

464493
/**

src/test/java/com/google/devtools/build/lib/util/SimpleLogHandlerTest.java

+22
Original file line numberDiff line numberDiff line change
@@ -498,4 +498,26 @@ public void getLoggerFilePath_onMissingLogHandler_fails() throws Exception {
498498

499499
assertThrows(IOException.class, () -> handlerQuerier.getLoggerFilePath(logger));
500500
}
501+
502+
@Test
503+
public void publish_handlesInterrupt() throws Exception {
504+
SimpleLogHandler handler =
505+
SimpleLogHandler.builder()
506+
.setPrefix(tmp.getRoot() + File.separator + "hello")
507+
.setFormatter(new TrivialFormatter())
508+
.build();
509+
Thread t =
510+
new Thread(
511+
() -> {
512+
Thread.currentThread().interrupt();
513+
handler.publish(new LogRecord(Level.SEVERE, "Hello world")); // To open the log file.
514+
assertThat(Thread.currentThread().isInterrupted()).isTrue();
515+
handler.flush();
516+
assertThat(Thread.currentThread().isInterrupted()).isTrue();
517+
handler.close();
518+
assertThat(Thread.currentThread().isInterrupted()).isTrue();
519+
});
520+
t.run();
521+
t.join();
522+
}
501523
}

0 commit comments

Comments
 (0)