php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #42497 fgets causes PHP hang on socket file pointer
Submitted: 2007-08-31 15:51 UTC Modified: 2007-10-22 11:28 UTC
Votes:3
Avg. Score:5.0 ± 0.0
Reproduced:3 of 3 (100.0%)
Same Version:2 (66.7%)
Same OS:1 (33.3%)
From: donald at designknights dot com Assigned:
Status: Not a bug Package: Network related
PHP Version: 5.2.4 OS: SUSE linux 10.1
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: donald at designknights dot com
New email:
PHP Version: OS:

 

 [2007-08-31 15:51 UTC] donald at designknights dot com
Description:
------------
this is a script to connect to a pop3 server and parse emails.

$connection = fsockopen($popServer, $port, $error ,$string ,$timeout);
	$response = fgets($connection, $buffer);
	$newLine = "\r\n";
	$localhost = "www.designknights.com";
	if(empty($connection))	{
	   echo "Failed to connect: $response";
	   exit;
	}
	else {
	   $logArray['connection'] = "Connected to: $response";
	}
	echo "sending username<br>";
	ob_flush();
	flush();
	fputs($connection, "USER $username". $newLine);
	$response = fgets($connection, $buffer);
	$logArray['userresponse'] = "$response";
	fputs($connection, "PASS $password". $newLine);
	$response = fgets($connection, $buffer);
	$logArray['passresponse'] = "$response";
	if (substr($response,0,3)!="+OK"){
		echo "Mail server said: ".$response;
		exit;
	}
	fputs($connection,"LIST".$newLine);
	$msg_array = array();
	$response = fgets($connection,$buffer);
	$logArray['listresponse'] = "$response";
   	$messages = explode(" ",$response);
	$nummessages = $messages['1'];
	echo "number of messages =".$nummessages;
	stream_set_blocking($connection,1);
	for ($i=1;$i<=$nummessages;$i++){
		fputs($connection,"retr ".$i.$newLine,$buffer);
		$message = '';
		$line = '';

//This loop is where it hangs
          
               while (!feof($connection){
			$line = fgets($connection,$buffer);
        	        echo "<br> line = $line";
			ob_flush();
			flush();
			
		}
		$message_blown = explode(":",$message);
		echo "<br> message $i = <br>";
		print_r($message_blown);
	} 		
	echo "return = ".print_r($messages)."<br>";
	print_r($logArray);
	fclose($connection);

Reproduce code:
---------------
See above

Expected result:
----------------
It should to my understanding put the lines into the var names $message then explode that message into $message_blown using : as a separator. 

Actual result:
--------------
it hangs and the max execution time kills php5 while running the script.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-08-31 18:37 UTC] donald at designknights dot com
i have stream_set_blocking = 1 and used stream_get_line instead of fgets
this seems to have solved the race condition.
 [2007-10-22 11:28 UTC] jani@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php


 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Tue Jul 01 17:01:34 2025 UTC