sshtools / maverick-synergy

Next Generation Java SSH API
https://jadaptive.com
GNU Lesser General Public License v3.0
96 stars 26 forks source link

divide by zero exception #34

Closed TheAustinSwamy closed 2 years ago

TheAustinSwamy commented 3 years ago

Line 962 in file https://github.com/sshtools/maverick-synergy/blob/master/maverick-synergy-client/src/main/java/com/sshtools/client/sftp/SftpChannel.java

Notice a division by zero exception when log level is set to Info.

if(Log.isInfoEnabled()) { Log.info("Optimized write to {} took {} seconds at {}", filename, seconds, IOUtils.toByteSize(transfered / seconds, 1)); }

ludup commented 2 years ago

This was already fixed back in July and the fix is available in the 3.0.5-SNAPSHOT.

jamoros commented 2 years ago

I get the same error. I just grabbed the latest version.

ludup commented 2 years ago

A stack trace and more information would be preferable to a "me too" post. That way I can constructively look at the evidence rather than blindly go through the code expecting to find the issue you claim to see.

jamoros commented 2 years ago

Hey Lee, you're absolutely right and I apologize. I was on my way out of the office and just responded quickly. Here is a snippet of my code:

 ssh.runTask(new SftpClientTask(ssh) {
            @Override
            protected void doSftp() {

                for (String filePath : externalFileName) {

                    try {
                        SftpFileAttributes get = get(filePath, localFolder);
                        if (get != null) {
                            // Why is there an arithmitic exception?
                            return;
                        }
                    } catch (TransferCancelledException | PermissionDeniedException | SftpStatusException
                            | SshException | IOException ex) {
                        System.out.println(ex.getMessage());
                    }
                }
            }
        });

And here is the log:

25 Dec 2021 18:27:15,365 [     pool-3-thread-1]  DEBUG - Sent SSH_MSG_CHANNEL_OPEN channel=0 channelType=session
25 Dec 2021 18:27:15,427 [     pool-5-thread-1]  DEBUG - Received SSH_MSG_CHANNEL_OPEN_CONFIRMATION channel=0 remote=0 remotepacket=35840 remotewindow=33554432
25 Dec 2021 18:27:15,429 [     pool-5-thread-1]  DEBUG - Sent SSH_MSG_CHANNEL_REQUEST request=subsystem wantReply=true channel=0 remote=0 localWindow=1024000 remoteWindow=33554432
25 Dec 2021 18:27:15,487 [     pool-3-thread-1]  DEBUG - Received SSH_MSG_CHANNEL_SUCCESS channel=0 remote=0 localWindow=1024000 remoteWindow=33554432
25 Dec 2021 18:27:15,489 [     pool-3-thread-1]  DEBUG - Sent SSH_MSG_CHANNEL_DATA seq=11 len=9 channel=0 remote=0 localWindow=1024000 remoteWindow=33554423
25 Dec 2021 18:27:15,546 [     pool-5-thread-1]  DEBUG - Received SSH_MSG_CHANNEL_DATA len=9 channel=0 remote=0 localWindow=1023991 remoteWindow=33554423
25 Dec 2021 18:27:15,559 [     pool-5-thread-1]  DEBUG - Sent SSH_MSG_CHANNEL_DATA seq=12 len=70 channel=0 remote=0 localWindow=1023991 remoteWindow=33554353
25 Dec 2021 18:27:15,633 [     pool-6-thread-1]  DEBUG - Received SSH_MSG_CHANNEL_DATA len=33 channel=0 remote=0 localWindow=1023958 remoteWindow=33554353
25 Dec 2021 18:27:15,634 [     pool-5-thread-1]  DEBUG - Sent SSH_MSG_CHANNEL_DATA seq=13 len=70 channel=0 remote=0 localWindow=1023958 remoteWindow=33554283
25 Dec 2021 18:27:15,693 [     pool-3-thread-1]  DEBUG - Received SSH_MSG_CHANNEL_DATA len=33 channel=0 remote=0 localWindow=1023925 remoteWindow=33554283
25 Dec 2021 18:27:15,694 [     pool-5-thread-1]  DEBUG - Sent SSH_MSG_CHANNEL_DATA seq=14 len=78 channel=0 remote=0 localWindow=1023925 remoteWindow=33554205
25 Dec 2021 18:27:15,767 [     pool-5-thread-1]  DEBUG - Received SSH_MSG_CHANNEL_DATA len=14 channel=0 remote=0 localWindow=1023911 remoteWindow=33554205
25 Dec 2021 18:27:15,769 [     pool-4-thread-1]  DEBUG - Firing EVENT_SFTP_FILE_OPENED success=true
25 Dec 2021 18:27:15,770 [     pool-3-thread-1]  DEBUG - Sent SSH_MSG_CHANNEL_DATA seq=15 len=26 channel=0 remote=0 localWindow=1023911 remoteWindow=33554179
25 Dec 2021 18:27:15,836 [     pool-3-thread-1]  DEBUG - Received SSH_MSG_CHANNEL_DATA len=23 channel=0 remote=0 localWindow=1023888 remoteWindow=33554179
25 Dec 2021 18:27:15,837 [     pool-4-thread-1]  DEBUG - Received SSH_FXP_DATA channel=0 requestId=4 offset=0 blocksize=10
25 Dec 2021 18:27:15,838 [     pool-5-thread-1]  DEBUG - Sent SSH_MSG_CHANNEL_DATA seq=16 len=14 channel=0 remote=0 localWindow=1023888 remoteWindow=33554165
25 Dec 2021 18:27:15,900 [     pool-5-thread-1]  DEBUG - Received SSH_MSG_CHANNEL_DATA len=25 channel=0 remote=0 localWindow=1023863 remoteWindow=33554165
25 Dec 2021 18:27:15,900 [     pool-4-thread-1]  DEBUG - Firing EVENT_SFTP_FILE_CLOSED success=true
25 Dec 2021 18:27:15,900 [     pool-4-thread-1]  ERROR - Connection task failed with an error
java.lang.ArithmeticException: / by zero
    at com.sshtools.client.sftp.SftpChannel.performOptimizedRead(SftpChannel.java:1245)
    at com.sshtools.client.sftp.SftpClient.get(SftpClient.java:1536)
    at com.sshtools.client.sftp.SftpClient.get(SftpClient.java:1105)
    at com.sshtools.client.sftp.SftpClient.get(SftpClient.java:1185)
    at com.sshtools.client.sftp.SftpClient.get(SftpClient.java:1205)
    at com.sshtools.client.sftp.SftpClientTask.get(SftpClientTask.java:660)
    at com.krfs.utils.MaverickSftpLargeTest$1.doSftp(MaverickSftpLargeTest.java:125)
    at com.sshtools.client.sftp.SftpClientTask.doTask(SftpClientTask.java:55)
    at com.sshtools.common.ssh.ConnectionAwareTask.run(ConnectionAwareTask.java:45)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at java.base/java.lang.Thread.run(Thread.java:834)

25 Dec 2021 18:27:15,901 [                main]   INFO - Disconnect /10.0.2.15:43298 By Application
Exception: IOException
Exception message: Task did not succeed
25 Dec 2021 18:27:15,903 [     pool-5-thread-1]  DEBUG - Sent SSH_MSG_DISCONNECT reason=11 By Application

The file still downloads but I get that exception. I've only tested with downloading one file (its very small) but I will be testing a different implementation this week now that I am aware of the stat function.

EDIT: I completed my new implementation in which works exactly how I need it, but when using:

 SftpFileAttributes file = get(folderPath + "/" + fileName, localFolder);

I still get the exception. Hope this helps to get a fix in, I'm wondering if it has something to do with the transfer time/file size.

java.lang.ArithmeticException: / by zero
    at com.sshtools.client.sftp.SftpChannel.performOptimizedRead(SftpChannel.java:1245)
    at com.sshtools.client.sftp.SftpClient.get(SftpClient.java:1536)

Using version 3.0.5-FINAL.

Thank you!

ludup commented 2 years ago

It appears that the merge to bring in this issue from develop branch failed and the release got built without some of the issues I had fixed. I've now merged and rebuilt a new release 3.0.6 which should include all the fixes committed thus far.