Fix jsch client hanging
authorDmitry Neverov <dmitry.neverov@gmail.com>
Tue, 23 Jan 2018 12:50:42 +0000 (13:50 +0100)
committerDmitry Neverov <dmitry.neverov@gmail.com>
Tue, 23 Jan 2018 12:50:42 +0000 (13:50 +0100)
We need to ask for input stream before connecting because it initializes
the stream.

git-agent/src/jetbrains/buildServer/buildTriggers/vcs/git/agent/JSchClient.java

index ca32e99bdc3f31656853b1497989e648bfc83553..8304e19f4327c795fe03beb65ed2f17cbe193149 100644 (file)
@@ -29,6 +29,7 @@ import javax.security.auth.callback.Callback;
 import javax.security.auth.callback.CallbackHandler;
 import javax.security.auth.callback.UnsupportedCallbackException;
 import java.io.File;
 import javax.security.auth.callback.CallbackHandler;
 import javax.security.auth.callback.UnsupportedCallbackException;
 import java.io.File;
+import java.io.IOException;
 import java.io.InputStream;
 import java.security.Security;
 import java.text.SimpleDateFormat;
 import java.io.InputStream;
 import java.security.Security;
 import java.text.SimpleDateFormat;
@@ -168,12 +169,16 @@ public class JSchClient {
       channel.setCommand(myCommand);
       channel.setInputStream(System.in);
       channel.setErrStream(System.err);
       channel.setCommand(myCommand);
       channel.setInputStream(System.in);
       channel.setErrStream(System.err);
+      InputStream input = channel.getInputStream();
       channel.connect();
 
       channel.connect();
 
-      InputStream input = channel.getInputStream();
+      if (!channel.isConnected()) {
+        throw new IOException("Connection failed");
+      }
+
       byte[] buffer = new byte[BUF_SIZE];
       int count;
       byte[] buffer = new byte[BUF_SIZE];
       int count;
-      while ((count = input.read(buffer)) != -1) {
+      while (channel.isConnected() && !channel.isClosed() && (count = input.read(buffer)) != -1) {
         System.out.write(buffer, 0, count);
       }
     } finally {
         System.out.write(buffer, 0, count);
       }
     } finally {