Commit 2206dc5bdc0 for php

commit 2206dc5bdc0c24d72ffb597c5c636a5f64957521
Author: Artem Ukrainskiy <art.ukrainskiy@gmail.com>
Date:   Tue Nov 17 23:15:49 2026 +0300

    ext/posix, ext/sockets: Reset the last error at the end of a request (#25532)

    posix_get_last_error() and socket_last_error() without a socket could
    return an error left by a previous request in the same worker, because
    nothing reset POSIX_G(last_error) and SOCKETS_G(last_error) when a
    request ended. Reset them in RSHUTDOWN.

diff --git a/NEWS b/NEWS
index 77cf23c0c13..70354ee97c6 100644
--- a/NEWS
+++ b/NEWS
@@ -366,6 +366,10 @@ PHP                                                                        NEWS
   . Fixed over-read when watching a packed array element. (Ilia Alshanetsky)
   . Fixed double free of watch element chains. (Arnaud)

+- POSIX:
+  . Fixed bug GH-25530 (posix_get_last_error() returns an error from a
+    previous request). (Artem Ukrainskiy)
+
 - Readline:
   . Fixed incorrect handling of non-ASCII pending_input bytes in readline_info(),
     which could cause readline() to return false prematurely. (Weilin Du)
@@ -410,6 +414,8 @@ PHP                                                                        NEWS
 - Sockets:
   . Fixed bug GH-24748 (socket_set_option() crashes with declared object
     properties). (Lazizbek Ergashev)
+  . Fixed bug GH-25530 (socket_last_error() without a socket returns an error
+    from a previous request). (Artem Ukrainskiy)

 - SPL:
   . Fixed bug GH-24264 (Unwind/GracefulExit discarded in spl_iterators.c).
diff --git a/ext/posix/posix.c b/ext/posix/posix.c
index b659207f67b..536c9009326 100644
--- a/ext/posix/posix.c
+++ b/ext/posix/posix.c
@@ -77,6 +77,15 @@ static PHP_MINIT_FUNCTION(posix)
 }
 /* }}} */

+/* {{{ PHP_RSHUTDOWN_FUNCTION(posix) */
+static PHP_RSHUTDOWN_FUNCTION(posix)
+{
+	POSIX_G(last_error) = 0;
+
+	return SUCCESS;
+}
+/* }}} */
+
 /* {{{ posix_module_entry */
 zend_module_entry posix_module_entry = {
 	STANDARD_MODULE_HEADER,
@@ -85,7 +94,7 @@ zend_module_entry posix_module_entry = {
 	PHP_MINIT(posix),
 	NULL,
 	NULL,
-	NULL,
+	PHP_RSHUTDOWN(posix),
 	PHP_MINFO(posix),
 	PHP_POSIX_VERSION,
 	PHP_MODULE_GLOBALS(posix),
diff --git a/ext/posix/tests/posix_get_last_error_request_scope.phpt b/ext/posix/tests/posix_get_last_error_request_scope.phpt
new file mode 100644
index 00000000000..65e3507cb66
--- /dev/null
+++ b/ext/posix/tests/posix_get_last_error_request_scope.phpt
@@ -0,0 +1,35 @@
+--TEST--
+posix_get_last_error() does not return an error from a previous request
+--EXTENSIONS--
+posix
+--CONFLICTS--
+server
+--FILE--
+<?php
+include __DIR__ . '/../../../sapi/cli/tests/php_cli_server.inc';
+
+$code = <<<'PHP'
+if (isset($_GET['fail'])) {
+    var_dump(posix_kill(2147483647, 0));
+    var_dump(posix_get_last_error() !== 0);
+} else {
+    var_dump(posix_get_last_error());
+}
+PHP;
+
+$args = ['-d', 'extension_dir=' . ini_get('extension_dir')];
+$args[] = '-d';
+$args[] = 'extension=posix.' . PHP_SHLIB_SUFFIX;
+if (php_ini_loaded_file()) {
+    $args[] = '-c';
+    $args[] = php_ini_loaded_file();
+}
+php_cli_server_start($code, 'index.php', $args);
+
+echo file_get_contents('http://' . PHP_CLI_SERVER_ADDRESS . '/?fail=1');
+echo file_get_contents('http://' . PHP_CLI_SERVER_ADDRESS . '/');
+?>
+--EXPECT--
+bool(false)
+bool(true)
+int(0)
diff --git a/ext/sockets/sockets.c b/ext/sockets/sockets.c
index c1e7ead5546..6a4a961657d 100644
--- a/ext/sockets/sockets.c
+++ b/ext/sockets/sockets.c
@@ -509,6 +509,8 @@ static PHP_RSHUTDOWN_FUNCTION(sockets)
 		SOCKETS_G(strerror_buf) = NULL;
 	}

+	SOCKETS_G(last_error) = 0;
+
 	return SUCCESS;
 }
 /* }}} */
diff --git a/ext/sockets/tests/socket_last_error_request_scope.phpt b/ext/sockets/tests/socket_last_error_request_scope.phpt
new file mode 100644
index 00000000000..4f505616564
--- /dev/null
+++ b/ext/sockets/tests/socket_last_error_request_scope.phpt
@@ -0,0 +1,37 @@
+--TEST--
+socket_last_error() does not return an error from a previous request
+--EXTENSIONS--
+sockets
+--CONFLICTS--
+server
+--FILE--
+<?php
+include __DIR__ . '/../../../sapi/cli/tests/php_cli_server.inc';
+
+$code = <<<'PHP'
+if (isset($_GET['fail'])) {
+    $socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
+    // 192.0.2.1 is reserved for documentation, so it isn't a local address
+    var_dump(@socket_bind($socket, '192.0.2.1', 0));
+    var_dump(socket_last_error() !== 0);
+} else {
+    var_dump(socket_last_error());
+}
+PHP;
+
+$args = ['-d', 'extension_dir=' . ini_get('extension_dir')];
+$args[] = '-d';
+$args[] = 'extension=' . (PHP_OS_FAMILY === 'Windows' ? 'php_' : '') . 'sockets.' . PHP_SHLIB_SUFFIX;
+if (php_ini_loaded_file()) {
+    $args[] = '-c';
+    $args[] = php_ini_loaded_file();
+}
+php_cli_server_start($code, 'index.php', $args);
+
+echo file_get_contents('http://' . PHP_CLI_SERVER_ADDRESS . '/?fail=1');
+echo file_get_contents('http://' . PHP_CLI_SERVER_ADDRESS . '/');
+?>
+--EXPECT--
+bool(false)
+bool(true)
+int(0)