SFTP Upload Hangs

Get help with using the PHP Secure Communications Library.

Moderator: Nuxius

Forum rules
The purpose of this forum is to provide support for phpseclib, a pure PHP SSH / SFTP / RSA library.

Posts by new users are held in a moderation queue and are not publicly visible until the post is approved.

SFTP Upload Hangs

Postby KCC » Thu Apr 26, 2012 3:14 pm

Hello,

I am encountering an issue with the SFTP portion of phpseclib where the put() function will sometimes hang indefinitely.

The put call uses the local file method, and is wrapped around logging to show the beginning and end, like this:

print "SFTP START PUT $file\n";
$put = $sftp->put($destination,$file,NET_SFTP_LOCAL_FILE);
print "SFTP PUT COMPLETE\n";

When the SFTP connection is made, the socket timeout is set like this:
stream_set_timeout($sftp->fsock, 30);

It is relatively rare, but perhaps 1 in 500 times the put command will never complete and the script continues on for hours or days until stopped manually. I suspect it is caused by the remote server either dropping the connection or failing in some way but I have not seen a way to program for that scenario.

Thank you.
KCC
Traveler
 
Posts: 14
Joined: Thu Apr 26, 2012 3:07 pm

Re: SFTP Upload Hangs

Postby TerraFrost » Sat Apr 28, 2012 11:41 am

I'm gonna try to add some real-time logging to SSH2.php. SFTP.php has it but not SSH2.php. The problem with logging as is is that you can only get it after the script has finished running. You could use register_shutdown_function() as well, with the Net_SSH2 object being declared global within it, but then if you do Ctrl + C, it won't be called, so it seems like real-time logging is the only option here.
TerraFrost
Legendary Guard
 
Posts: 12218
Joined: Wed Dec 04, 2002 6:37 am

Re: SFTP Upload Hangs

Postby TerraFrost » Mon Apr 30, 2012 6:32 pm

I've added real-time logging to the latest SVN of Net_SSH2. Quoting from the comments in the code:

Code: Select all
(NET_SSH2_LOG_REALTIME)
                // dump the output out realtime; packets may be interspersed with non packets,
                // passwords won't be filtered out and select other packets may not be correctly
                // identified

(NET_SSH2_LOG_REALTIME_FILE)
                // basically the same thing as NET_SSH2_LOG_REALTIME with the caveat that NET_SSH2_LOG_REALTIME_FILE
                // needs to be defined and that the resultant log file will be capped out at NET_SSH2_LOG_MAX_SIZE.
                // the earliest part of the log file is denoted by the first <<< START >>> and is not going to necessarily
                // at the beginning of the file


An example of how to use it:

Code: Select all
<?php
include('Net/SSH2.php');

define('NET_SSH2_LOG_REALTIME_FILE', '/path/to/test2.txt');
define('NET_SSH2_LOGGING', NET_SSH2_LOG_REALTIME_FILE);

$ssh = new Net_SSH2('www.domain.tld');
$ssh->login('username', 'password');

$ssh->exec('ls -la');

I haven't done an equivalent mode for SFTP because SFTP runs on top of SSH2 and because, in so far as we know, the problem could be with the SSH2 layer and not at the SFTP layer. If the problem were at the SFTP layer the SSH2 logs would reveal that but not vice versa.
TerraFrost
Legendary Guard
 
Posts: 12218
Joined: Wed Dec 04, 2002 6:37 am

Re: SFTP Upload Hangs

Postby KCC » Tue May 01, 2012 2:08 pm

Thank you, I will update to the latest SVN code and let you know what happens.

The issue actually occurred yesterday but I noticed something I haven't seen before, the put() command never completed and these lines appeared in the STDERR stream:
[put() started here]
PHP Notice: Invalid HMAC in /usr/share/pear/Net/SSH2.php on line 1890
PHP Notice: Connection closed by server in /usr/share/pear/Net/SSH2.php on line 2017
PHP Notice: Expected SSH_FXP_STATUS in /usr/share/pear/Net/SFTP.php on line 1136


The put() command never returned and the code was hung indefinitely after those 3 error lines.

Thanks
KCC
Traveler
 
Posts: 14
Joined: Thu Apr 26, 2012 3:07 pm

Re: SFTP Upload Hangs

Postby TerraFrost » Tue May 01, 2012 2:41 pm

That actually helps a lot I think. I'll try to make an update tomorrow. Thanks!!
TerraFrost
Legendary Guard
 
Posts: 12218
Joined: Wed Dec 04, 2002 6:37 am

Re: SFTP Upload Hangs

Postby TerraFrost » Thu May 03, 2012 11:16 am

Does this do the trick?:

Code: Select all
#
#-----[ FIND ]------------------------------------------
#
        $this->_read_put_responses($i);

        if ($mode == NET_SFTP_LOCAL_FILE) {
            fclose($fp);
        }
#
#-----[ REPLACE WITH ]----------------------------------
#
        if (!$this->_read_put_responses($i)) {
            return false;
        }

        if ($mode == NET_SFTP_LOCAL_FILE) {
            fclose($fp);
        }
TerraFrost
Legendary Guard
 
Posts: 12218
Joined: Wed Dec 04, 2002 6:37 am

Re: SFTP Upload Hangs

Postby KCC » Thu May 03, 2012 2:45 pm

I applied the changes you quoted above, the circumstances to reproduce are somewhat random so I will watch it for the next few days and post back the outcome.

Thank you
KCC
Traveler
 
Posts: 14
Joined: Thu Apr 26, 2012 3:07 pm

Re: SFTP Upload Hangs

Postby KCC » Wed May 16, 2012 1:26 pm

Hello,

The issue popped up again today, this was the error log:
PHP Notice: Invalid HMAC in /usr/share/pear/Net/SSH2.php on line 1890
PHP Notice: Connection closed by server in /usr/share/pear/Net/SSH2.php on line 2017
PHP Notice: Expected SSH_FXP_STATUS in /usr/share/pear/Net/SFTP.php on line 1139
PHP Fatal error: Out of memory (allocated 1569193984) (tried to allocate 1565705251 bytes) in /usr/share/pear/Net/SSH2.php on line 1875

I have not noticed the memory line until today, the max memory configuration is 4GB.

Thanks
KCC
Traveler
 
Posts: 14
Joined: Thu Apr 26, 2012 3:07 pm

Re: SFTP Upload Hangs

Postby TerraFrost » Mon May 28, 2012 3:14 am

Sorry for the delay - my main laptop has been undergoing a warranty repair and meh.

Anyway, your line 1139 doesn't match my line 1139 (latest SVN). Can you post your SFTP.php?

My line 1139 is in the _chmod_recursive() function. It's "return false;". Here's the surrounding code:

Code: Select all
                if ($i >= 50) {
                    if (!$this->_read_put_responses($i)) {
                        return false;
                    }
                    $i = 0;
                }
TerraFrost
Legendary Guard
 
Posts: 12218
Joined: Wed Dec 04, 2002 6:37 am

Re: SFTP Upload Hangs

Postby KCC » Mon Jun 11, 2012 8:34 pm

Sorry for the delay, here is the attachment.

Thanks again
Last edited by KCC on Tue Jun 12, 2012 1:08 pm, edited 1 time in total.
KCC
Traveler
 
Posts: 14
Joined: Thu Apr 26, 2012 3:07 pm

Re: SFTP Upload Hangs

Postby TerraFrost » Mon Jun 11, 2012 8:50 pm

Looks like you forgot to include the attachment in your post.
TerraFrost
Legendary Guard
 
Posts: 12218
Joined: Wed Dec 04, 2002 6:37 am

Re: SFTP Upload Hangs

Postby KCC » Tue Jun 12, 2012 1:13 pm

Sorry, looks like it doesn't take .php extensions, the file should be attached now, thanks.
Attachments
SFTP.txt
(56 KiB) Downloaded 56 times
KCC
Traveler
 
Posts: 14
Joined: Thu Apr 26, 2012 3:07 pm

Re: SFTP Upload Hangs

Postby TerraFrost » Sat Jun 16, 2012 11:42 pm

Maybe try this?:
Code: Select all
#
#-----[ FIND ]------------------------------------------
#
Net/SFTP.php
#
#-----[ FIND ]------------------------------------------
#
            extract(unpack('Nstatus', $this->_string_shift($response, 4)));
            if ($status != NET_SFTP_STATUS_OK) {
                extract(unpack('Nlength', $this->_string_shift($response, 4)));
                $this->sftp_errors[] = $this->status_codes[$status] . ': ' . $this->_string_shift($response, $length);
                break;
            }
#
#-----[ REPLACE WITH ]----------------------------------
#
            extract(unpack('Nstatus', $this->_string_shift($response, 4)));
            if ($status != NET_SFTP_STATUS_OK) {
                extract(unpack('Nlength', $this->_string_shift($response, 4)));
                $this->sftp_errors[] = $this->status_codes[$status] . ': ' . $this->_string_shift($response, $length);
                return false;
            }

If that doesn't work... I'm curious what the output of this would be:

Code: Select all
#
#-----[ FIND ]------------------------------------------
#
Net/SFTP.php
#
#-----[ FIND ]------------------------------------------
#
        while ($sent < $size) {
#
#-----[ AFTER, ADD ]------------------------------------
#
            echo "$sent < $size\r\n";

Also, would you happen to know the size of the file that you're trying to upload that's causing the script to hang?
TerraFrost
Legendary Guard
 
Posts: 12218
Joined: Wed Dec 04, 2002 6:37 am

Re: SFTP Upload Hangs

Postby KCC » Mon Jun 18, 2012 8:38 pm

I have applied the first patch and will monitor it for changes. I've seen the hang occur on file sizes from 2-20MB's.
Thanks again.
KCC
Traveler
 
Posts: 14
Joined: Thu Apr 26, 2012 3:07 pm

Re: SFTP Upload Hangs

Postby KCC » Fri Jul 06, 2012 5:36 pm

Hello,

The issue has continued to occur, these 3 lines always output when it happens now:
PHP Notice: Invalid HMAC in /usr/share/pear/Net/SSH2.php on line 1890
PHP Notice: Connection closed by server in /usr/share/pear/Net/SSH2.php on line 2017
PHP Notice: Expected SSH_FXP_STATUS in /usr/share/pear/Net/SFTP.php on line 1139

I will add in the second part that logs the size and see what happens with that.
Thanks
KCC
Traveler
 
Posts: 14
Joined: Thu Apr 26, 2012 3:07 pm

Next

Return to phpseclib support

Who is online

Users browsing this forum: No registered users and 1 guest