|  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
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
From: andreas dot buchenrieder at check24 dot de
New email:
PHP Version: OS:


 [2017-06-26 15:57 UTC] andreas dot buchenrieder at check24 dot de
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:

$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) {
    if (!ssh2_auth_password($connection, $username, $password)) {
    $sftp = ssh2_sftp($connection);
    $handle = @fopen('ssh2.sftp://' . intval($sftp) . $path . $username, 'w+');
    if ($handle === false) {


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2017-06-26 20:15 UTC]
-Package: opcache +Package: ssh2
 [2017-06-26 21:24 UTC] mhagstrand at gmail dot com
This appears to be a duplicate of
 [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-2020 The PHP Group
All rights reserved.
Last updated: Thu Apr 09 21:01:23 2020 UTC