Uploaded image for project: 'Karaf'
  1. Karaf
  2. KARAF-2003

Interrupt a running command in shell

    Details

    • Type: Improvement
    • Status: Resolved
    • Priority: Minor
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 3.0.0
    • Component/s: karaf-shell
    • Labels:
      None

      Description

      Right now it's not possible to interrupt a "long" running command. For example by using ctrl-c to stop it like in a unix shell.

        Issue Links

          Activity

          Hide
          gnt Guillaume Nodet added a comment -

          That's not really true. Typing Ctrl+C in the console will interrupt the current thread. We just need to have commands being able to check the interrupted flag and exit I think.
          When using a pipe however, I don't recall which thread is interrupted.

          For example if you run
          > sleep 60000

          You can interrupt at any time by typing Ctrl+C and the sleep command will exit immediately.

          Show
          gnt Guillaume Nodet added a comment - That's not really true. Typing Ctrl+C in the console will interrupt the current thread. We just need to have commands being able to check the interrupted flag and exit I think. When using a pipe however, I don't recall which thread is interrupted. For example if you run > sleep 60000 You can interrupt at any time by typing Ctrl+C and the sleep command will exit immediately.
          Hide
          achim_nierbeck Achim Nierbeck added a comment -

          Ok, so this doesn't seem to work for the features:install command then, or if it does work it doesn't give "instant" feedback

          Show
          achim_nierbeck Achim Nierbeck added a comment - Ok, so this doesn't seem to work for the features:install command then, or if it does work it doesn't give "instant" feedback
          Hide
          gnt Guillaume Nodet added a comment -

          What we could do is provide a checkInterrupted() method on the AbstractAction and have long running commands regularly call it.

          public void checkInterrupted() throws InterruptedException {
              Thread.yield(); 
              if (Thread.currentThread().isInterrupted()) {
                  throw new InterruptedException();
              }
          }
          
          Show
          gnt Guillaume Nodet added a comment - What we could do is provide a checkInterrupted() method on the AbstractAction and have long running commands regularly call it. public void checkInterrupted() throws InterruptedException { Thread .yield(); if ( Thread .currentThread().isInterrupted()) { throw new InterruptedException(); } }
          Hide
          gnt Guillaume Nodet added a comment -

          Yeah, I'm not saying it always work, but interrupting a thread will only work if the thread can be interrupted, i.e. on a sleep, blocking io, or something like that.
          It may also be the case that some code catch and discard InterruptedException, I've already seen that.

          Show
          gnt Guillaume Nodet added a comment - Yeah, I'm not saying it always work, but interrupting a thread will only work if the thread can be interrupted, i.e. on a sleep, blocking io, or something like that. It may also be the case that some code catch and discard InterruptedException, I've already seen that.
          Hide
          gnt Guillaume Nodet added a comment -

          For feature:install, I came up with the following patch

          diff --git a/features/core/src/main/java/org/apache/karaf/features/internal/FeaturesServiceImpl.java b/features/core/src/main/java/org/apache/karaf/features/internal/FeaturesServiceImpl.java
          index 3ba99e6..135866e 100644
          --- a/features/core/src/main/java/org/apache/karaf/features/internal/FeaturesServiceImpl.java
          +++ b/features/core/src/main/java/org/apache/karaf/features/internal/FeaturesServiceImpl.java
          @@ -20,8 +20,10 @@ import java.io.BufferedInputStream;
           import java.io.File;
           import java.io.FileInputStream;
           import java.io.FileOutputStream;
          +import java.io.FilterInputStream;
           import java.io.IOException;
           import java.io.InputStream;
          +import java.io.InterruptedIOException;
           import java.io.OutputStream;
           import java.net.MalformedURLException;
           import java.net.URI;
          @@ -845,8 +847,17 @@ public class FeaturesServiceImpl implements FeaturesService, FrameworkListener {
                           is.reset();
                       } catch (IOException e) {
                           is.close();
          -                is = new BufferedInputStream(new URL(bundleLocation).openStream());
          +                is = new URL(bundleLocation).openStream();
                       }
          +            is = new BufferedInputStream(new FilterInputStream(is) {
          +                @Override
          +                public int read(byte b[], int off, int len) throws IOException {
          +                    if (Thread.currentThread().isInterrupted()) {
          +                        throw new InterruptedIOException();
          +                    }
          +                    return super.read(b, off, len);
          +                }
          +            });
                       LOGGER.debug("Installing bundle " + bundleLocation);
                       if (verbose) {
                           System.out.println("Installing bundle " + bundleLocation);
          

          which seems to do the work.

          Show
          gnt Guillaume Nodet added a comment - For feature:install, I came up with the following patch diff --git a/features/core/src/main/java/org/apache/karaf/features/internal/FeaturesServiceImpl.java b/features/core/src/main/java/org/apache/karaf/features/internal/FeaturesServiceImpl.java index 3ba99e6..135866e 100644 --- a/features/core/src/main/java/org/apache/karaf/features/internal/FeaturesServiceImpl.java +++ b/features/core/src/main/java/org/apache/karaf/features/internal/FeaturesServiceImpl.java @@ -20,8 +20,10 @@ import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; + import java.io.FilterInputStream; import java.io.IOException; import java.io.InputStream; + import java.io.InterruptedIOException; import java.io.OutputStream; import java.net.MalformedURLException; import java.net.URI; @@ -845,8 +847,17 @@ public class FeaturesServiceImpl implements FeaturesService, FrameworkListener { is.reset(); } catch (IOException e) { is.close(); - is = new BufferedInputStream( new URL(bundleLocation).openStream()); + is = new URL(bundleLocation).openStream(); } + is = new BufferedInputStream( new FilterInputStream(is) { + @Override + public int read( byte b[], int off, int len) throws IOException { + if ( Thread .currentThread().isInterrupted()) { + throw new InterruptedIOException(); + } + return super .read(b, off, len); + } + }); LOGGER.debug( "Installing bundle " + bundleLocation); if (verbose) { System .out.println( "Installing bundle " + bundleLocation); which seems to do the work.
          Hide
          achim_nierbeck Achim Nierbeck added a comment -

          I think I'm gonna give it a try for 3.0

          Show
          achim_nierbeck Achim Nierbeck added a comment - I think I'm gonna give it a try for 3.0
          Hide
          alanfoster Alan Foster added a comment -

          It would be cool if this also fixed the exception given when pressing ctrl+c in conjunction with the 'more' command

          Currently it gives the following exception

          --More--^C
          Error executing command: java.lang.InterruptedException
          karaf@root>
          

          Log file stacktrace

          013-05-05 20:51:54,740 | ERROR | l Console Thread | Console
               | araf.shell.console.jline.Console  199 | 14 - org.apache.karaf.shell.cons
          le - 2.3.2.SNAPSHOT | Exception caught while executing command
          ava.lang.InterruptedException
                 at java.lang.Object.wait(Native Method)[:1.6.0_27]
                 at java.lang.Thread.join(Thread.java:1186)[:1.6.0_27]
                 at java.lang.Thread.join(Thread.java:1239)[:1.6.0_27]
                 at org.apache.felix.gogo.runtime.Closure.execute(Closure.java:200)[14:or
          .apache.karaf.shell.console:2.3.2.SNAPSHOT]
                 at org.apache.felix.gogo.runtime.Closure.execute(Closure.java:120)[14:or
          .apache.karaf.shell.console:2.3.2.SNAPSHOT]
                 at org.apache.felix.gogo.runtime.CommandSessionImpl.execute(CommandSessi
          nImpl.java:89)[14:org.apache.karaf.shell.console:2.3.2.SNAPSHOT]
                 at org.apache.karaf.shell.console.jline.Console.run(Console.java:173)[14
          org.apache.karaf.shell.console:2.3.2.SNAPSHOT]
                 at java.lang.Thread.run(Thread.java:662)[:1.6.0_27]
          
          Show
          alanfoster Alan Foster added a comment - It would be cool if this also fixed the exception given when pressing ctrl+c in conjunction with the 'more' command Currently it gives the following exception --More--^C Error executing command: java.lang.InterruptedException karaf@root> Log file stacktrace 013-05-05 20:51:54,740 | ERROR | l Console Thread | Console | araf.shell.console.jline.Console 199 | 14 - org.apache.karaf.shell.cons le - 2.3.2.SNAPSHOT | Exception caught while executing command ava.lang.InterruptedException at java.lang. Object .wait(Native Method)[:1.6.0_27] at java.lang. Thread .join( Thread .java:1186)[:1.6.0_27] at java.lang. Thread .join( Thread .java:1239)[:1.6.0_27] at org.apache.felix.gogo.runtime.Closure.execute(Closure.java:200)[14:or .apache.karaf.shell.console:2.3.2.SNAPSHOT] at org.apache.felix.gogo.runtime.Closure.execute(Closure.java:120)[14:or .apache.karaf.shell.console:2.3.2.SNAPSHOT] at org.apache.felix.gogo.runtime.CommandSessionImpl.execute(CommandSessi nImpl.java:89)[14:org.apache.karaf.shell.console:2.3.2.SNAPSHOT] at org.apache.karaf.shell.console.jline.Console.run(Console.java:173)[14 org.apache.karaf.shell.console:2.3.2.SNAPSHOT] at java.lang. Thread .run( Thread .java:662)[:1.6.0_27]
          Hide
          achim_nierbeck Achim Nierbeck added a comment -

          Author: anierbeck
          Date: Fri May 17 15:55:05 2013
          New Revision: 1483884

          URL: http://svn.apache.org/r1483884

          Show
          achim_nierbeck Achim Nierbeck added a comment - Author: anierbeck Date: Fri May 17 15:55:05 2013 New Revision: 1483884 URL: http://svn.apache.org/r1483884
          Hide
          danttran Dan Tran added a comment -

          can we have this fix at 2.3.x branch?

          Show
          danttran Dan Tran added a comment - can we have this fix at 2.3.x branch?

            People

            • Assignee:
              achim_nierbeck Achim Nierbeck
              Reporter:
              achim_nierbeck Achim Nierbeck
            • Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Development