php / php/php-src

fclose 500ms delay on a listening socket on Windows

Open
#19,696 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bug Category: Streams OS: Windows Status: Needs Triage
Dominant language
C
Stars
40.4k
Forks
8.1k
Avg merge
2d 13h
Merged PRs (30d)
96

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with xp_socket.c and the php_sockop_close path mentioned in the report, then trace how stream_socket_server() closes a listening socket on Windows. Reproduce the PHP 8.4.12 Windows timing comparison and inspect the existing socket tests for a suitable regression case. Done means the stream server closes without the reported 500ms delay while socket_close() behavior remains unchanged.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, php
Domain
networking
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.