26
26
import java .io .File ;
27
27
import java .io .FileOutputStream ;
28
28
import java .io .IOException ;
29
+ import java .io .InterruptedIOException ;
29
30
import java .io .OutputStreamWriter ;
30
31
import java .lang .management .ManagementFactory ;
31
32
import java .nio .file .DirectoryStream ;
@@ -413,6 +414,8 @@ public synchronized void publish(LogRecord record) {
413
414
return ;
414
415
}
415
416
417
+ // This allows us to do the I/O while not forgetting that we were interrupted.
418
+ boolean isInterrupted = Thread .interrupted ();
416
419
try {
417
420
String message = getFormatter ().format (record );
418
421
openOutputIfNeeded ();
@@ -421,6 +424,9 @@ public synchronized void publish(LogRecord record) {
421
424
reportError (null , e , ErrorManager .WRITE_FAILURE );
422
425
// Failing to log is non-fatal. Continue to try to rotate the log if necessary, which may fix
423
426
// the underlying IO problem with the file.
427
+ if (e instanceof InterruptedIOException ) {
428
+ isInterrupted = true ;
429
+ }
424
430
}
425
431
426
432
try {
@@ -430,35 +436,58 @@ public synchronized void publish(LogRecord record) {
430
436
}
431
437
} catch (IOException e ) {
432
438
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 ();
433
445
}
434
446
}
435
447
436
448
@ Override
437
449
public synchronized void flush () {
450
+ boolean isInterrupted = Thread .interrupted ();
438
451
if (output .isOpen ()) {
439
452
try {
440
453
output .flush ();
441
454
} catch (IOException e ) {
442
455
reportError (null , e , ErrorManager .FLUSH_FAILURE );
456
+ if (e instanceof InterruptedIOException ) {
457
+ isInterrupted = true ;
458
+ }
443
459
}
444
460
}
461
+ if (isInterrupted ) {
462
+ Thread .currentThread ().interrupt ();
463
+ }
445
464
}
446
465
447
466
@ Override
448
467
public synchronized void close () {
468
+ boolean isInterrupted = Thread .interrupted ();
449
469
if (output .isOpen ()) {
450
470
try {
451
471
output .write (getFormatter ().getTail (this ));
452
472
} catch (IOException e ) {
453
473
reportError ("Failed to write log tail" , e , ErrorManager .WRITE_FAILURE );
474
+ if (e instanceof InterruptedIOException ) {
475
+ isInterrupted = true ;
476
+ }
454
477
}
455
478
456
479
try {
457
480
output .close ();
458
481
} catch (IOException e ) {
459
482
reportError (null , e , ErrorManager .CLOSE_FAILURE );
483
+ if (e instanceof InterruptedIOException ) {
484
+ isInterrupted = true ;
485
+ }
460
486
}
461
487
}
488
+ if (isInterrupted ) {
489
+ Thread .currentThread ().interrupt ();
490
+ }
462
491
}
463
492
464
493
/**
0 commit comments