This is an automated email from the ASF dual-hosted git repository.

heneveld pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/brooklyn-server.git

commit 552b5ecc1e22dc57c2a2c7439e8d414a400afa7a
Author: Alex Heneveld <[email protected]>
AuthorDate: Tue Aug 2 13:25:06 2022 +0100

    more checks to prevent sshj swallowing thread interruptions
---
 .../brooklyn/util/core/internal/ssh/sshj/SshjTool.java    | 10 +++++++---
 .../org/apache/brooklyn/util/exceptions/Exceptions.java   | 15 +++++----------
 2 files changed, 12 insertions(+), 13 deletions(-)

diff --git 
a/core/src/main/java/org/apache/brooklyn/util/core/internal/ssh/sshj/SshjTool.java
 
b/core/src/main/java/org/apache/brooklyn/util/core/internal/ssh/sshj/SshjTool.java
index ddc96ad611..f953286ea5 100644
--- 
a/core/src/main/java/org/apache/brooklyn/util/core/internal/ssh/sshj/SshjTool.java
+++ 
b/core/src/main/java/org/apache/brooklyn/util/core/internal/ssh/sshj/SshjTool.java
@@ -899,9 +899,10 @@ public class SshjTool extends SshAbstractTool implements 
SshTool {
                         long joinTimeout = 10*1000;
                         if (outgobbler != null) outgobbler.join(joinTimeout);
                         if (errgobbler != null) errgobbler.join(joinTimeout);
-                    } catch (InterruptedException e) {
-                        LOG.warn("Interrupted gobbling streams from ssh: 
"+command, e);
-                        Thread.currentThread().interrupt();
+                    } catch (Exception e) {
+                        if (checkInterrupted(e)) {
+                            LOG.warn("Interrupted gobbling streams from ssh: " 
+ command, e);
+                        }
                     }
                 }
 
@@ -975,6 +976,9 @@ public class SshjTool extends SshAbstractTool implements 
SshTool {
                         output.write(toUTF8ByteArray(cmd+"\n"));
                         output.flush();
                     } catch (ConnectionException e) {
+                        if (checkInterrupted(e)) {
+                            throw e;
+                        }
                         if (!shell.isOpen()) {
                             // shell is closed; presumably the user command 
did `exit`
                             if (LOG.isDebugEnabled()) LOG.debug("Shell closed 
to {} when executing {}", SshjTool.this.toString(), commands);
diff --git 
a/utils/common/src/main/java/org/apache/brooklyn/util/exceptions/Exceptions.java
 
b/utils/common/src/main/java/org/apache/brooklyn/util/exceptions/Exceptions.java
index 29a9300b63..7b7590dc5c 100644
--- 
a/utils/common/src/main/java/org/apache/brooklyn/util/exceptions/Exceptions.java
+++ 
b/utils/common/src/main/java/org/apache/brooklyn/util/exceptions/Exceptions.java
@@ -120,12 +120,8 @@ public class Exceptions {
      * <li> wraps as PropagatedRuntimeException for easier filtering
      */
     public static RuntimeException propagate(Throwable throwable) {
-        if (throwable instanceof InterruptedException || throwable instanceof 
RuntimeInterruptedException || Exceptions.isRootCauseIsInterruption(throwable)) 
{
-            // previously only interrupted if we caught RuntimeInterrupted; 
but best seems to be to always set the interrupted bit
-            Thread.currentThread().interrupt();
-            throw new RuntimeInterruptedException(throwable);
-        }
-        Throwables.propagateIfPossible(checkNotNull(throwable));
+        propagateIfInterrupt(throwable);
+        Throwables.throwIfUnchecked(throwable);
         throw new PropagatedRuntimeException(throwable);
     }
 
@@ -185,11 +181,10 @@ public class Exceptions {
      * or {@link RuntimeInterruptedException}.
      */
     public static void propagateIfInterrupt(Throwable throwable) {
-        if (throwable instanceof InterruptedException) {
-            throw new RuntimeInterruptedException(throwable);
-        } else if (throwable instanceof RuntimeInterruptedException) {
+        if (throwable!=null && (throwable instanceof InterruptedException || 
throwable instanceof RuntimeInterruptedException || 
Exceptions.isRootCauseIsInterruption(throwable))) {
+            // previously only interrupted if we caught RuntimeInterrupted; 
but best seems to be to always set the interrupted bit
             Thread.currentThread().interrupt();
-            throw (RuntimeInterruptedException) throwable;
+            throw new RuntimeInterruptedException(throwable);
         }
     }
 

Reply via email to