php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #43695 Asynchronous socket connection timing issue
Submitted: 2007-12-28 08:37 UTC Modified: 2008-09-09 01:00 UTC
Votes:7
Avg. Score:4.0 ± 1.6
Reproduced:5 of 7 (71.4%)
Same Version:1 (20.0%)
Same OS:4 (80.0%)
From: nicolas dot legland at free dot fr Assigned:
Status: No Feedback Package: Streams related
PHP Version: 5.3CVS-2007-12-28 (snap) OS: Windows XP Professional SP2
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2007-12-28 08:37 UTC] nicolas dot legland at free dot fr
Description:
------------
Asynchronously connecting a TCP socket stream to a filtered port times out, but closing the resource using fclose() blocks for 0.5 second.

Asynchronously connecting a TCP socket stream to a closed port ends up with the server explicitly refusing the connection by a RST ACK packet. Using stream_select() returns the stream resource as having had an except but trying to close the resource using fclose() blocks for 0.5 second too.

No network transfer was captured by Wireshark apart from the initial SYN packet in either case. Using fclose() on a successfully asynchronously connected TCP socket stream to an open port returns instantaneously.

If you don't explicitly fclose() the resources of failed connections, the same 0.5 seconds lag appears at the end of the script execution for each one. PHP probably cleanly frees resources at shutdown, but it can reach several seconds when several sockets have failed.

When using non-blocking BSD socket with the sockets extension, no delay of any kind is noticed.

Reproduce code:
---------------
<?php

	// Send a [SYN] packet to scanme.nmap.org:70
	$time = microtime(true);
	$resource = stream_socket_client('tcp://'.gethostbyname('scanme.nmap.org').':70', $null = null, $null, 0, STREAM_CLIENT_ASYNC_CONNECT | STREAM_CLIENT_CONNECT);
	stream_set_blocking($resource, 0);
	echo 'Open	'.number_format((microtime(true) - $time), 6).PHP_EOL;

	// Wait 3 seconds for a [SYN, ACK] packet
	$time = microtime(true);
	$read = $write = $except = array($resource);
	stream_select($read, $write, $except, 3);
	echo 'Wait	'.number_format((microtime(true) - $time), 6).'	read '.count($read).', write '.count($write).', except '.count($except).PHP_EOL;

	// Cancel connection
	$time = microtime(true);
	fclose($resource);
	echo 'Close	'.number_format((microtime(true) - $time), 6).PHP_EOL;

?>

Expected result:
----------------
Open    0.010317
Wait    1.588449        read 0, write 0, except 1
Close   0.000099

Actual result:
--------------
Open    0.010002
Wait    1.547396        read 0, write 0, except 1
Close   0.510161

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-12-28 14:02 UTC] nicolas dot legland at free dot fr
Description:
------------

When using sockets through the "sockets" extension, everything works fine with no delay to notice.

Reproduce code:
---------------
<?php

	// Load socket extension
	dl((('dll' === PHP_SHLIB_SUFFIX) ? 'php_' : '').'sockets.'.PHP_SHLIB_SUFFIX);

	// Send a [SYN] packet to scanme.nmap.org:70
	$time = microtime(true);
	$resource = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
	socket_set_nonblock($resource);
	@socket_connect($resource, gethostbyname('scanme.nmap.org'), 70);
	echo 'Open	'.number_format((microtime(true) - $time), 6).PHP_EOL;

	// Wait 3 seconds for a [SYN, ACK] packet
	$time = microtime(true);
	$read = $write = $except = array($resource);
	socket_select($read, $write, $except, 3);
	echo 'Wait	'.number_format((microtime(true) - $time), 6).'	read '.count($read).', write '.count($write).', except '.count($except).PHP_EOL;

	// Cancel connection
	$time = microtime(true);
	socket_close($resource);
	echo 'Close	'.number_format((microtime(true) - $time), 6).PHP_EOL;

?>

Result:
-------
Open    0.030620
Wait    1.518440        read 0, write 0, except 1
Close   0.000108
 [2007-12-28 14:17 UTC] koollman at gmail dot com
Works fine on linux, so it may be windows related:

#socket example, edited to remove the dl() call
$ php5 socket.php 
Open    0.002049
Wait    0.194022        read
1, write 1, except
0
Close   0.000044

#stream example
$ php5 stream.php 
Open    0.007108
Wait    0.211123        read
1, write 1, except
0
Close   0.000048

#php5 from http://hype.sourceforge.jp
$ php5 --version
PHP 5.2.3-1~edh.0 (cli) (built: Jun 10 2007 11:07:12) 
Copyright (c) 1997-2007 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2007 Zend Technologies

#vanilla linux kernel
$ uname -r
2.6.22
 [2008-09-09 01:00 UTC] php-bugs at lists dot php dot net
No feedback was provided for this bug for over a week, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Mar 19 06:01:30 2024 UTC