Discussion:
[JSch-users] JSCH .53 return empty strings executing commands on ubuntu
Offer Baruch
2016-05-17 07:10:53 UTC
Permalink
Hello

I am new to the list but using JSCH for a long time accessing redhat and
sles...
Lately i started using it to access ubuntu but i am having a very strange
issue.
The code i am using is shared for all distors and versions but specifically
on ubuntu every once in about 100 commands i issue (in average) i get back
an empty string.
No exceptions, input stream simply reaches EOF.
I was able to recreate the issue running the same command 400 times.
Specifically "echo offer".
In average 4 times will return an empty string. In the real world this
happen randomly on a different command each time.
It only happens with ubuntu (i am using 16.04).
I am using an exec session to run my commands with jsch .53.
Does this ring a bell to anyone?
How can i proceed to solve or get around this problem? is there any debug
options i can use?
I am not able to reproduce without jsch... i tried simple ssh with a single
command.

Can someone point me in the right direction?

Thanks on advance
Offer Baruch
Offer Baruch
2016-05-18 08:04:56 UTC
Permalink
can someone confirm you can actually see this?
i would like to add some more data to this issue.
this is the way i configure my channel:
Channel channel = session.openChannel("exec");
((ChannelExec) channel).setPty(true);
((ChannelExec) channel).setPtyType("dumb");
((ChannelExec) channel).setCommand(command);
connOut = channel.getOutputStream();
connIn = channel.getInputStream();
connErr = ((ChannelExec) channel).getErrStream();
channel.connect();
when i mark the setPty to false everything works fine...
when it is true i get the random error...
i tries different terminal types but that did not solve anything...

please i really need some help here...

thanks
Offer Baruch
Post by Offer Baruch
Hello
I am new to the list but using JSCH for a long time accessing redhat and
sles...
Lately i started using it to access ubuntu but i am having a very strange
issue.
The code i am using is shared for all distors and versions but
specifically on ubuntu every once in about 100 commands i issue (in
average) i get back an empty string.
No exceptions, input stream simply reaches EOF.
I was able to recreate the issue running the same command 400 times.
Specifically "echo offer".
In average 4 times will return an empty string. In the real world this
happen randomly on a different command each time.
It only happens with ubuntu (i am using 16.04).
I am using an exec session to run my commands with jsch .53.
Does this ring a bell to anyone?
How can i proceed to solve or get around this problem? is there any debug
options i can use?
I am not able to reproduce without jsch... i tried simple ssh with a
single command.
Can someone point me in the right direction?
Thanks on advance
Offer Baruch
Tobia Conforto
2016-05-18 08:26:24 UTC
Permalink
What exactly are you doing after channel.connect()?
Do you have any try/catch, read-write loops, and such?
Can you post the rest of the code until channel.disconnect()?

Tobia
Post by Offer Baruch
can someone confirm you can actually see this?
i would like to add some more data to this issue.
Channel channel = session.openChannel("exec");
((ChannelExec) channel).setPty(true);
((ChannelExec) channel).setPtyType("dumb");
((ChannelExec) channel).setCommand(command);
connOut = channel.getOutputStream();
connIn = channel.getInputStream();
connErr = ((ChannelExec) channel).getErrStream();
channel.connect();
when i mark the setPty to false everything works fine...
when it is true i get the random error...
i tries different terminal types but that did not solve anything...
please i really need some help here...
thanks
Offer Baruch
Hello
I am new to the list but using JSCH for a long time accessing redhat and sles...
Lately i started using it to access ubuntu but i am having a very strange issue.
The code i am using is shared for all distors and versions but specifically on ubuntu every once in about 100 commands i issue (in average) i get back an empty string.
No exceptions, input stream simply reaches EOF.
I was able to recreate the issue running the same command 400 times. Specifically "echo offer".
In average 4 times will return an empty string. In the real world this happen randomly on a different command each time.
It only happens with ubuntu (i am using 16.04).
I am using an exec session to run my commands with jsch .53.
Does this ring a bell to anyone?
How can i proceed to solve or get around this problem? is there any debug options i can use?
I am not able to reproduce without jsch... i tried simple ssh with a single command.
Can someone point me in the right direction?
Thanks on advance
Offer Baruch
Tobia Conforto
2016-05-18 09:48:08 UTC
Permalink
Then I believe it's a race condition.

You are looping until connIn.read() returns -1, but that only means that *for now* there is no more data to read. It doesn't mean that the remote command is done. So if the remote server takes a few millisecond more to run the command, your Java program will get a -1 from read() and exit the loop, instead of waiting for any more data.

I suggest you study the examples provided with JSch, starting from the one about Exec[1], where you will see the only condition that can ever leave the main while(true) is basically: channel.isClosed() && ! in.available()>0

Tobia

[1] http://www.jcraft.com/jsch/examples/Exec.java.html
while (!endOfIn) {
i = connIn.read(tmp, 0, tempBufLen);
if (i < 0) {
endOfIn = true;
break;
}
...
}
although this is surrounded by try and catch there is no exception raised...
i simply returns -1.
the command is in fact get run on the host... i just loose the output of the command.
Offer Baruch
2016-05-18 10:30:33 UTC
Permalink
but read should block until you get data or you reach EOF... it shouldn't
return -1 if there might be anymore data...
as -1 (End Of File) was returned that means that no more data is available.

anyway i will give it a try and report back.
Post by Tobia Conforto
Then I believe it's a race condition.
You are looping until connIn.read() returns -1, but that only means that
*for now* there is no more data to read. It doesn't mean that the remote
command is done. So if the remote server takes a few millisecond more to
run the command, your Java program will get a -1 from read() and exit the
loop, instead of waiting for any more data.
I suggest you study the examples provided with JSch, starting from the one
about Exec[1], where you will see the only condition that can ever leave
the main while(true) is basically: channel.isClosed() && ! in.available()>0
Tobia
[1] http://www.jcraft.com/jsch/examples/Exec.java.html
while (!endOfIn) {
i = connIn.read(tmp, 0, tempBufLen);
if (i < 0) {
endOfIn = true;
break;
}
...
}
although this is surrounded by try and catch there is no exception
raised...
i simply returns -1.
the command is in fact get run on the host... i just loose the output of
the command.
Offer Baruch
2016-05-18 10:45:27 UTC
Permalink
ok... tried it as it is in the example and i still get the same result...
every once in a while i get nothing back. that is the channel is closed and
there is no available stuff to read.
Post by Offer Baruch
but read should block until you get data or you reach EOF... it shouldn't
return -1 if there might be anymore data...
as -1 (End Of File) was returned that means that no more data is available.
anyway i will give it a try and report back.
On Wed, May 18, 2016 at 12:48 PM, Tobia Conforto <
Post by Tobia Conforto
Then I believe it's a race condition.
You are looping until connIn.read() returns -1, but that only means that
*for now* there is no more data to read. It doesn't mean that the remote
command is done. So if the remote server takes a few millisecond more to
run the command, your Java program will get a -1 from read() and exit the
loop, instead of waiting for any more data.
I suggest you study the examples provided with JSch, starting from the
one about Exec[1], where you will see the only condition that can ever
leave the main while(true) is basically: channel.isClosed() && !
in.available()>0
Tobia
[1] http://www.jcraft.com/jsch/examples/Exec.java.html
while (!endOfIn) {
i = connIn.read(tmp, 0, tempBufLen);
if (i < 0) {
endOfIn = true;
break;
}
...
}
although this is surrounded by try and catch there is no exception
raised...
i simply returns -1.
the command is in fact get run on the host... i just loose the output
of the command.
Offer Baruch
2016-05-18 10:47:03 UTC
Permalink
oh.. and the channel exist status is 0.
Post by Offer Baruch
ok... tried it as it is in the example and i still get the same result...
every once in a while i get nothing back. that is the channel is closed
and there is no available stuff to read.
Post by Offer Baruch
but read should block until you get data or you reach EOF... it shouldn't
return -1 if there might be anymore data...
as -1 (End Of File) was returned that means that no more data is available.
anyway i will give it a try and report back.
On Wed, May 18, 2016 at 12:48 PM, Tobia Conforto <
Post by Tobia Conforto
Then I believe it's a race condition.
You are looping until connIn.read() returns -1, but that only means that
*for now* there is no more data to read. It doesn't mean that the remote
command is done. So if the remote server takes a few millisecond more to
run the command, your Java program will get a -1 from read() and exit the
loop, instead of waiting for any more data.
I suggest you study the examples provided with JSch, starting from the
one about Exec[1], where you will see the only condition that can ever
leave the main while(true) is basically: channel.isClosed() && !
in.available()>0
Tobia
[1] http://www.jcraft.com/jsch/examples/Exec.java.html
while (!endOfIn) {
i = connIn.read(tmp, 0, tempBufLen);
if (i < 0) {
endOfIn = true;
break;
}
...
}
although this is surrounded by try and catch there is no exception
raised...
i simply returns -1.
the command is in fact get run on the host... i just loose the output
of the command.
Tobia Conforto
2016-05-18 10:48:58 UTC
Permalink
but read should block until you get data or you reach EOF... it shouldn't return -1 if there might be anymore data...
as -1 (End Of File) was returned that means that no more data is available.
You are probably right. I'm not that familiar with Java I/O streams' contracts.
tried it as it is in the example and i still get the same result...
every once in a while i get nothing back. that is the channel is closed and there is no available stuff to read.
You might want to try executing a command like "sleep 1; echo Hello; sleep 1; echo World" just to rule out any race conditions in your code.

Otherwise, I'll have to defer to the JSch experts here.

Tobia
Offer Baruch
2016-05-18 12:19:22 UTC
Permalink
well the sleep works... but that does not mean the race condition is not in
the JSCH code...
as i said i changed the loop as in the example and it still happens...
Post by Offer Baruch
but read should block until you get data or you reach EOF... it
shouldn't return -1 if there might be anymore data...
Post by Offer Baruch
as -1 (End Of File) was returned that means that no more data is
available.
You are probably right. I'm not that familiar with Java I/O streams' contracts.
Post by Offer Baruch
tried it as it is in the example and i still get the same result...
every once in a while i get nothing back. that is the channel is closed
and there is no available stuff to read.
You might want to try executing a command like "sleep 1; echo Hello; sleep
1; echo World" just to rule out any race conditions in your code.
Otherwise, I'll have to defer to the JSch experts here.
Tobia
Loading...