|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #60555 Resource handling failure - maybe an garbage collection issue
Submitted: 2011-12-18 18:53 UTC Modified: 2012-06-22 08:48 UTC
From: andi at a3non dot org Assigned:
Status: Wont fix Package: ssh2 (PECL)
PHP Version: 5.3.8 OS: Debian6.0.3 - 2.6.32-5-amd64
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2011-12-18 18:53 UTC] andi at a3non dot org
using the ssh2_sftp() function to create an resource to access an sftp server causes an resource error when using the functions in OOP context and storing the resource handle returned by ssh2_sftp() into an LOCAL var. using class global vars, everything works as expected

it seems that the resource reference created by ssh2_sftp() getting closed by the garbage collection when leaving the constructor. it might be an core error into the php garbage collector - it should update references which are used by other variables

::installed SSH2 Extension
pecl::ssh2 0.11.3 (beta)

::configure options
./configure --with-freetype-dir=/usr/lib --enable-gd-native-ttf --enable-exif --enable-zip  --with-bz2 --with-zlib --enable-ftp --with-gd --with-jpeg-dir=/usr/lib --with-png-dir=/usr/lib --with-mysql=/usr/lib --with-mhash --with-pdo-mysql --with-sqlite --with-openssl --enable-mbstring --enable-debug=no --with-config-file-path=/etc/php --enable-calendar

::php ini changes

Test script:
// this code causes the failure
class failure{
	public $this->resourceIdentifier;
	public function __construct(){
		// get sftp con
		$sftpcon = ssh2_sftp($connection);
		// set resource handle
		$this->resourceIdentifier = "ssh2.sftp://$sftpcon";
	public function open(){
		// causes an error: Warning: opendir(): 8 is not a valid SSH2 SFTP resource
		$handle = opendir($this->resourceIdentifier);

// this is a workaround by using a class variable
class workaround{
	public $this->resourceIdentifier;

	public function __construct(){
		// get sftp con -> class global var
		$this->_sftpcon = ssh2_sftp($connection);
		// set resource handle
		$this->resourceIdentifier = "ssh2.sftp://$this->_sftpcon";

	public function open(){
		// works fine..
		$handle = opendir($this->resourceIdentifier);

Expected result:
$handle should be an valid directory handle!

Actual result:
Warning: opendir(): 8 is not a valid SSH2 SFTP resource


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2012-06-16 12:31 UTC]
-Status: Open +Status: Feedback -Type: Bug +Type: Feature/Change Request
 [2012-06-16 12:31 UTC]
Hmm.. I've been thinking about this one.

Obviously, the "ssh2.sftp://$sftpcon" to create an identifier is not creating a 
reference to $ftpcon, and it shouldn't. $sftpcon is cast to a string, creating a 
new variable, then concatenated to the literal 'ssh2.sftp://'.

The sftp stream code finds back the resource based on the id in the string. It's 
an ugly hack, but it works brilliantly.

A possible solution might be that all sftp are kept internally too, with an extra 
method ssh2_sftp_close() to close an sftp subsystem resource. This is now done on 
unsetting the resource, which happens to variables in the local scope when you 
leave the function.

Another solution is to use the stream context parameter for all operations on 
streams to contain a reference to the sftp resource. I think that is not what 
stream context was originally designed for.

For now, as this is not a blocking issue, I won't do anything about this. If you 
don't agree with my reasoning feel free to reply.
 [2012-06-22 08:48 UTC]
-Status: Feedback +Status: Wont fix
PHP Copyright © 2001-2023 The PHP Group
All rights reserved.
Last updated: Sun Jun 04 08:03:42 2023 UTC