php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #74816 Multiple ssh2-sftp connections will all point to the first one
Submitted: 2017-06-26 15:57 UTC Modified: 2017-06-27 15:20 UTC
From: andreas dot buchenrieder at check24 dot de Assigned:
Status: Open Package: ssh2 (PECL)
PHP Version: 7.1.6 OS: Linux
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2017-06-26 15:57 UTC] andreas dot buchenrieder at check24 dot de
Description:
------------
PHP-Version: 7.1.6-2+0~20170614060700.1+jessie~1.gbp831871

I open multiple ssh2 connections in a loop. In my case the server itself is always the same, but the username/password changes. So I cannot say if this error happens if the server changes as well

Basically each file should be put on a different account
With opcache enabled, every file is uploaded on the first connection, even though it should always be a new one. The streams meta data even shows a new resource id for each connection

This does not happen in PHP 5.6 or with opcache disabled

Test script:
---------------
<?php

$configs = [
    ['host', 'username', 'password', '/path/'],
    // ...
];

while (true) {
    $index = array_rand($configs);
    list($host, $username, $password, $path) = $configs[$index];
    $port = 22;
    $connection = ssh2_connect($host, 22);
    if ($connection === false) {
        continue;
    }
    if (!ssh2_auth_password($connection, $username, $password)) {
        continue;
    }
    $sftp = ssh2_sftp($connection);
    $handle = @fopen('ssh2.sftp://' . intval($sftp) . $path . $username, 'w+');
    if ($handle === false) {
        continue;
    }
    print_r(stream_get_meta_data($handle));
    fclose($handle);
}



Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2017-06-26 20:15 UTC] langemeijer@php.net
-Package: opcache +Package: ssh2
 [2017-06-26 21:24 UTC] mhagstrand at gmail dot com
This appears to be a duplicate of https://bugs.php.net/bug.php?id=74667
 [2017-06-27 14:43 UTC] andreas dot buchenrieder at check24 dot de
I just checked and it seems I was wrong about suspecting opcache to be the reason.
The error persists even if opcache is not even loaded

So much for a workaround :(
 [2017-06-27 15:20 UTC] andreas dot buchenrieder at check24 dot de
I think I localized the issue.

The test script provided is missing the key point. I kept the once opened sftp resources in an array to save the costs of reconnecting again and again

So somewhere in my script was a line like $pool["$username@$host"] = $sftp;

By keeping the resource in an array outside the loop the error occurred, even if I never reused the cached connection. If I keep only the ssh connection and reinstantiate the sftp resource new every time, the error disappeared.
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Tue May 21 02:01:26 2019 UTC