[JSch-users] Resolved: Jsch hangs on reading from "exec" channel.
Status: Alpha
Brought to you by:
ymnk
From: Alexander K. <al...@tm...> - 2004-11-30 14:09:54
|
Hello All, Recently I wrote that sometimes my program hangs on reading from jsch InputStream that is provided by ChannelExec object. I've managed to reproduce the same problem when using jsch in the other application (http://tmate.org/svn/) and found out that the reason of the problem was in the calls order: The code that may hang: mySession = jsch.getSession(credentials.getName(), host, port); mySession.connect(); myChannel = (ChannelExec) mySession.openChannel("exec"); myChannel.setCommand("svnserve -t"); myChannel.connect(); myInputStream = new RollbackInputStream(new BufferedInputStream(myChannel.getInputStream())); myOutputStream = myChannel.getOutputStream(); As you can see output and input streams are fetched _after_ myChannel.connect() call. In case when channel's read thread starts before myChannel.getInputStream() is called it will terminate and no data will be read and redirected to the channels input stream. See below ChannelExec "run" method code, that checks whether InputStream is already created: // this loop will not be started if io.in is NULL and it is NULL till getInputStream() is called. while(thread!=null && io!=null && io.in!=null){ // code that reads data and redirects it to the user's input stream. } // thread is terminated here Obviously, the workaround is to fetch input and output stream before calling Channel.connect() method, but what do you think about a) documenting this problem and better b) not relying on the order of calls, i.e. creating input stream in "connect" method or channel constructor, not in getInputStream() method. Also, _creating_ InputStream in "get" method breaks common sense contract of the "get" method. Thanks, Alexander Kitaev. |