commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Gary Gregory (JIRA)" <j...@apache.org>
Subject [jira] [Commented] (IO-357) Tailer: Interruption while sleeping is silently ignored
Date Wed, 21 Nov 2012 15:12:00 GMT

    [ https://issues.apache.org/jira/browse/IO-357?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13502032#comment-13502032
] 

Gary Gregory commented on IO-357:
---------------------------------

"AFAICT there's no point calling stop outside the run loop, because the flag won't be checked."

I added a call to stop() to play nice with [IO-345]: Supply a hook method allowing Tailer
actively determining stop condition.

This lets a Tailer subclass (as requested in IO-345) access the run value.


"Also InterruptedException is an instance of Exception; no point in having a separate catch
unless the code is different."

The code in the two catche clauses _is_ different. For InterruptedException, interrupt() is
called.

It is more obvious like this: 

{noformat}
Index: src/main/java/org/apache/commons/io/input/Tailer.java
===================================================================
--- src/main/java/org/apache/commons/io/input/Tailer.java	(revision 1411980)
+++ src/main/java/org/apache/commons/io/input/Tailer.java	(working copy)
@@ -356,10 +356,7 @@
                     listener.fileNotFound();
                 }
                 if (reader == null) {
-                    try {
-                        Thread.sleep(delayMillis);
-                    } catch (InterruptedException e) {
-                    }
+                    Thread.sleep(delayMillis);
                 } else {
                     // The current position in the file
                     position = end ? file.length() : 0;
@@ -410,22 +407,27 @@
                 if (reOpen) {
                     IOUtils.closeQuietly(reader);
                 }
-                try {
-                    Thread.sleep(delayMillis);
-                } catch (InterruptedException e) {
-                }
+                Thread.sleep(delayMillis);
                 if (getRun() && reOpen) {
                     reader = new RandomAccessFile(file, RAF_MODE);
                     reader.seek(position);
                 }
             }
-        } catch (Exception e) {
-            listener.handle(e);
+        } catch (InterruptedException e) {            
+            stop(e);
+            Thread.currentThread().interrupt();
+        } catch (Exception e) {            
+            stop(e);
         } finally {
             IOUtils.closeQuietly(reader);
         }
     }
 
+    private void stop(Exception e) {
+        listener.handle(e);
+        stop();
+    }
+
     /**
      * Allows the tailer to complete its current loop and return.
      */
{noformat}
                
> Tailer: Interruption while sleeping is silently ignored
> -------------------------------------------------------
>
>                 Key: IO-357
>                 URL: https://issues.apache.org/jira/browse/IO-357
>             Project: Commons IO
>          Issue Type: Bug
>          Components: Streams/Writers
>    Affects Versions: 2.4
>            Reporter: Morten Hattesen
>
> The implementation of org.apache.commons.io.input.Tailer silently ignores interruptions
while sleeping (in two places).
> Source snippet:
> {code}
> 360	                    try {
> 361	                        Thread.sleep(delayMillis);
> 362	                    } catch (InterruptedException e) {
> 363	                    }
> ...
> 425	                try {
> 426	                    Thread.sleep(delayMillis);
> 427	                } catch (InterruptedException e) {
> 428	                }
> {code}
> This is an inappropriate behavior, since it prevents controlled shutdown by a container.
> This may be rectified in one of these ways:
> # Declare the method as "throws InterruptedException" and re-throw the InterruptedException,
after possibly performing come cleanup, or removing the catch clause entirely. This will ensure
that a thread interruption (possibly caused by the forced shutdown by a container) will cause
processing to stop, and shutdown to proceed. Problem: Requires backwards incompatible change
to method signature.
> # Treat an interrupt as an alternate way of signalling the Tailer to stop, by calling
{{stop()}} in the catch clause.
> # Reassert the interrupted state of the thread by calling {{Thread.currentThread.interrupt()}}
to be able to detect the interruption at a later stage.
> For reference, please refer to these resources about handling thread interruption:
> * http://www.ibm.com/developerworks/java/library/j-jtp05236/index.html
> * Java Concurrency in Practice http://www.javaconcurrencyinpractice.com/

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira

Mime
View raw message