From 15622af005d77be07a89c29bb7e7565af72f33c9 Mon Sep 17 00:00:00 2001 From: Matthew Date: Tue, 27 Aug 2024 19:16:28 +0200 Subject: [PATCH] Adding timeouts to wait methods, to never allow for infinite wait() state. (#198) * Adding timeouts to wait methods, to never allow for infinite thread wait. * Update StreamGobbler.java Add timeouts to StreamGobbler methods * Update ChannelManager.java Use existing DEFAULT_WAIT_TIMEOUT for timeout * Update TransportManager.java Add DEFAULT_WAIT_TIMEOUT, and use it for wait() calls * Update KexManager.java * Update FifoBuffer.java * Increase default timeout to 20min, to allow for temporary network issues. * Fix DM_BOXED_PRIMITIVE_FOR_PARSING --- src/com/trilead/ssh2/StreamGobbler.java | 8 ++++++-- src/com/trilead/ssh2/auth/AuthenticationManager.java | 2 +- src/com/trilead/ssh2/channel/ChannelManager.java | 6 +++--- src/com/trilead/ssh2/channel/FifoBuffer.java | 8 ++++++-- src/com/trilead/ssh2/transport/KexManager.java | 4 +++- src/com/trilead/ssh2/transport/TransportManager.java | 6 ++++-- 6 files changed, 23 insertions(+), 11 deletions(-) diff --git a/src/com/trilead/ssh2/StreamGobbler.java b/src/com/trilead/ssh2/StreamGobbler.java index 93c25de4..5d4b1118 100644 --- a/src/com/trilead/ssh2/StreamGobbler.java +++ b/src/com/trilead/ssh2/StreamGobbler.java @@ -35,6 +35,10 @@ public class StreamGobbler extends InputStream { + + private static final String PROPERTY_TIMEOUT = StreamGobbler.class.getName() + ".timeout"; + private static long DEFAULT_WAIT_TIMEOUT = Long.parseLong(System.getProperty(PROPERTY_TIMEOUT,"1200000")); + class GobblerThread extends Thread { public void run() @@ -141,7 +145,7 @@ public int read() throws IOException try { - synchronizer.wait(); + synchronizer.wait(DEFAULT_WAIT_TIMEOUT); } catch (InterruptedException e) { @@ -210,7 +214,7 @@ public int read(byte[] b, int off, int len) throws IOException try { - synchronizer.wait(); + synchronizer.wait(DEFAULT_WAIT_TIMEOUT); } catch (InterruptedException e) { diff --git a/src/com/trilead/ssh2/auth/AuthenticationManager.java b/src/com/trilead/ssh2/auth/AuthenticationManager.java index f0bdf789..c294567b 100644 --- a/src/com/trilead/ssh2/auth/AuthenticationManager.java +++ b/src/com/trilead/ssh2/auth/AuthenticationManager.java @@ -28,7 +28,7 @@ public class AuthenticationManager implements MessageHandler { public static final String PROPERTY_TIMEOUT = AuthenticationManager.class.getName() + ".timeout"; - public static final long TIMEOUT = Long.valueOf(System.getProperty(PROPERTY_TIMEOUT,"120000")); + public static final long TIMEOUT = Long.parseLong(System.getProperty(PROPERTY_TIMEOUT,"1200000")); TransportManager tm; Vector packets = new Vector(); diff --git a/src/com/trilead/ssh2/channel/ChannelManager.java b/src/com/trilead/ssh2/channel/ChannelManager.java index ef516533..7d79a1b2 100644 --- a/src/com/trilead/ssh2/channel/ChannelManager.java +++ b/src/com/trilead/ssh2/channel/ChannelManager.java @@ -38,7 +38,7 @@ public class ChannelManager implements MessageHandler { private static final Logger log = Logger.getLogger(ChannelManager.class); private static final String PROPERTY_TIMEOUT = ChannelManager.class.getName() + ".timeout"; - private static long DEFAULT_WAIT_TIMEOUT = Long.valueOf(System.getProperty(PROPERTY_TIMEOUT,"120000")); + private static long DEFAULT_WAIT_TIMEOUT = Long.parseLong(System.getProperty(PROPERTY_TIMEOUT,"1200000")); private HashMap x11_magic_cookies = new HashMap(); @@ -382,7 +382,7 @@ public void sendData(Channel c, byte[] buffer, int pos, int len) throws IOExcept try { - c.wait(); + c.wait(DEFAULT_WAIT_TIMEOUT); } catch (InterruptedException ignore) { @@ -913,7 +913,7 @@ public int waitForCondition(Channel c, long timeout, int condition_mask) throws if (timeout > 0) c.wait(timeout); else - c.wait(); + c.wait(DEFAULT_WAIT_TIMEOUT); } } } diff --git a/src/com/trilead/ssh2/channel/FifoBuffer.java b/src/com/trilead/ssh2/channel/FifoBuffer.java index 332fe158..26b3cb78 100644 --- a/src/com/trilead/ssh2/channel/FifoBuffer.java +++ b/src/com/trilead/ssh2/channel/FifoBuffer.java @@ -18,6 +18,10 @@ * @author Kohsuke Kawaguchi */ class FifoBuffer { + + private static final String PROPERTY_TIMEOUT = FifoBuffer.class.getName() + ".timeout"; + private static long DEFAULT_WAIT_TIMEOUT = Long.parseLong(System.getProperty(PROPERTY_TIMEOUT,"1200000")); + /** * Unit of buffer, singly linked and lazy created as needed. */ @@ -153,7 +157,7 @@ public void write(byte[] buf, int start, int len) throws InterruptedException { synchronized (lock) { while ((chunk = Math.min(len,writable()))==0) - lock.wait(); + lock.wait(DEFAULT_WAIT_TIMEOUT); w.write(buf, start, chunk); @@ -209,7 +213,7 @@ public int read(byte[] buf, int start, int len) throws InterruptedException { releaseRing(); return -1; // no more data } - lock.wait(); // wait until the writer gives us something + lock.wait(DEFAULT_WAIT_TIMEOUT); // wait until the writer gives us something } r.read(buf,start,chunk); diff --git a/src/com/trilead/ssh2/transport/KexManager.java b/src/com/trilead/ssh2/transport/KexManager.java index 2057e347..94b4fc95 100644 --- a/src/com/trilead/ssh2/transport/KexManager.java +++ b/src/com/trilead/ssh2/transport/KexManager.java @@ -45,6 +45,8 @@ public class KexManager implements MessageHandler { private static final Logger log = Logger.getLogger(KexManager.class); + private static final String PROPERTY_TIMEOUT = KexManager.class.getName() + ".timeout"; + private static long DEFAULT_WAIT_TIMEOUT = Long.parseLong(System.getProperty(PROPERTY_TIMEOUT,"1200000")); private static final List DEFAULT_KEY_ALGORITHMS = buildDefaultKeyAlgorithms(); @@ -98,7 +100,7 @@ public ConnectionInfo getOrWaitForConnectionInfo(int minKexCount) throws IOExcep try { - accessLock.wait(); + accessLock.wait(DEFAULT_WAIT_TIMEOUT); } catch (InterruptedException e) { diff --git a/src/com/trilead/ssh2/transport/TransportManager.java b/src/com/trilead/ssh2/transport/TransportManager.java index eda7945c..ad31f8c9 100644 --- a/src/com/trilead/ssh2/transport/TransportManager.java +++ b/src/com/trilead/ssh2/transport/TransportManager.java @@ -54,6 +54,8 @@ public class TransportManager { private static final Logger log = Logger.getLogger(TransportManager.class); + private static final String PROPERTY_TIMEOUT = TransportManager.class.getName() + ".timeout"; + private static long DEFAULT_WAIT_TIMEOUT = Long.parseLong(System.getProperty(PROPERTY_TIMEOUT,"1200000")); class HandlerEntry { @@ -87,7 +89,7 @@ public void run() try { - asynchronousQueue.wait(2000); + asynchronousQueue.wait(DEFAULT_WAIT_TIMEOUT); } catch (InterruptedException e) { @@ -685,7 +687,7 @@ public void sendMessage(byte[] msg) throws IOException try { - connectionSemaphore.wait(); + connectionSemaphore.wait(DEFAULT_WAIT_TIMEOUT); } catch (InterruptedException e) {