Skip to content

fclose 500ms delay on a listening socket on Windows #19696

@darkyp

Description

@darkyp

Description

The following code:

<?php
	echo "Stream test\n";
	for ($i = 0; $i < 10; $i++) {
		$s = stream_socket_server("tcp://127.0.0.1:0/", $errno, $errstr);
		$start = microtime(1);
		fclose($s);
		echo sprintf("Closed in %.3f\n", microtime(1) - $start);
	}
	
	echo "Socket test\n";
	for ($i = 0; $i < 10; $i++) {
		$s = socket_create(AF_INET, SOCK_STREAM, 0);
		socket_bind($s, "127.0.0.1", 0);
		socket_listen($s);
		$start = microtime(1);
		socket_close($s);
		echo sprintf("Closed in %.3f\n", microtime(1) - $start);
	}
?>

Resulted in this output:

Stream test
Closed in 0.504
Closed in 0.508
Closed in 0.510
Closed in 0.508
Closed in 0.514
Closed in 0.510
Closed in 0.510
Closed in 0.509
Closed in 0.511
Closed in 0.508
Socket test
Closed in 0.000
Closed in 0.000
Closed in 0.000
Closed in 0.000
Closed in 0.000
Closed in 0.000
Closed in 0.000
Closed in 0.000
Closed in 0.000
Closed in 0.000

But I expected this output instead:

Stream test
Closed in 0.000
Closed in 0.000
Closed in 0.000
Closed in 0.000
Closed in 0.000
Closed in 0.000
Closed in 0.000
Closed in 0.000
Closed in 0.000
Closed in 0.000
Socket test
Closed in 0.000
Closed in 0.000
Closed in 0.000
Closed in 0.000
Closed in 0.000
Closed in 0.000
Closed in 0.000
Closed in 0.000
Closed in 0.000
Closed in 0.000

The problem dates back since at least PHP 5.4.0 as described [here]. I answered there but just tested with the most recent PHP for the Windows platform and confirmed it.

In short - I think that xp_socket.c/php_sockop_close should not poll a listening socket before closing it.

PHP Version

PHP 8.4.12 (cli) (built: Aug 26 2025 18:04:08) (ZTS Visual C++ 2022 x64)
Copyright (c) The PHP Group
Zend Engine v4.4.12, Copyright (c) Zend Technologies

Operating System

Windows 10 Pro x64

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions