Commit bab62816b16 for php

commit bab62816b16183f69e5b22a143bc569fac674483
Merge: e51fa6acb52 0e5694756fc
Author: Arnaud Le Blanc <arnaud.lb@gmail.com>
Date:   Mon Nov 16 15:20:14 2026 +0100

    Merge branch 'PHP-8.6'

    * PHP-8.6:
      Fix GH-8739: OPcache restart corrupts SHM in threaded SAPIs (#24276)

diff --cc ext/opcache/ZendAccelerator.c
index f221282136d,daa5e07e892..dd5f451a728
--- a/ext/opcache/ZendAccelerator.c
+++ b/ext/opcache/ZendAccelerator.c
@@@ -342,6 -340,62 +342,60 @@@ static inline bool accel_restart_is_act
  	return false;
  }

+ #if defined(ZTS) && !defined(ZEND_WIN32)
+ /* fcntl() locks belong to the process, so under ZTS they cannot tell which threads
+  * use SHM (GH-8739). The threads of this process that use SHM are counted here, and
+  * the process holds the reader lock (byte 1) whenever the count is not zero, so other
+  * processes still see it. A restart needs a zero count and a write lock on byte 1.
+  * The 0 <-> 1 transitions and the restart are serialized by accel_zts_readers_mutex,
+  * other changes of the count are lock-free. */
+ static MUTEX_T accel_zts_readers_mutex = NULL;
 -static zend_atomic_int accel_zts_readers;
++static atomic_int accel_zts_readers;
+ static time_t accel_zts_next_warning;
+
+ static bool accel_zts_lock_reader_byte(int cmd, int type)
+ {
+ 	struct flock lock;
+
+ 	lock.l_type = type;
+ 	lock.l_whence = SEEK_SET;
+ 	lock.l_start = 1;
+ 	lock.l_len = 1;
+
+ 	return fcntl(lock_file, cmd, &lock) != -1;
+ }
+
+ /* Adds (+1, only from RINIT) or releases (-1) a reference of the calling thread.
+  * Does not use ZCG() as it is also called from accel_globals_dtor(). */
+ static bool accel_zts_readers_add(int delta)
+ {
+ 	int count;
+
 -#ifndef HAVE_NO_ATOMICS
 -	count = zend_atomic_int_load_ex(&accel_zts_readers);
++	count = atomic_load(&accel_zts_readers);
+ 	while (count > 0 && count + delta > 0) {
 -		if (zend_atomic_int_compare_exchange_ex(&accel_zts_readers, &count, count + delta)) {
++		if (atomic_compare_exchange_strong(&accel_zts_readers, &count, count + delta)) {
+ 			return true;
+ 		}
+ 	}
 -#endif
+
+ 	tsrm_mutex_lock(accel_zts_readers_mutex);
 -	count = zend_atomic_int_load_ex(&accel_zts_readers);
++	count = atomic_load(&accel_zts_readers);
+ 	/* waits while another process restarts the cache (it holds the write lock) */
+ 	if (count == 0 && !accel_zts_lock_reader_byte(F_SETLKW, F_RDLCK)) {
+ 		zend_accel_error(ACCEL_LOG_DEBUG, "UpdateC(+1):  %s (%d)", strerror(errno), errno);
+ 		tsrm_mutex_unlock(accel_zts_readers_mutex);
+ 		return false;
+ 	}
 -	while (!zend_atomic_int_compare_exchange_ex(&accel_zts_readers, &count, count + delta));
++	while (!atomic_compare_exchange_strong(&accel_zts_readers, &count, count + delta));
+ 	if (count + delta == 0) {
+ 		/* the last reader of this process; unlock before another thread may relock */
+ 		accel_zts_lock_reader_byte(F_SETLK, F_UNLCK);
+ 	}
+ 	tsrm_mutex_unlock(accel_zts_readers_mutex);
+ 	return true;
+ }
+ #endif
+
  /* Creates a read lock for SHM access */
  static inline zend_result accel_activate_add(void)
  {
@@@ -987,6 -1053,48 +1053,48 @@@ static inline bool accel_is_inactive(vo
  	return false;
  }

+ #if defined(ZTS) && !defined(ZEND_WIN32)
+ /* Replaces accel_is_inactive() under ZTS; called under zend_shared_alloc_lock().
+  * On success the readers mutex and the write lock on byte 1 are held until
+  * accel_zts_restart_end(): no thread of this process can become a reader (the count is
+  * zero and may only grow from zero under the mutex), and other processes can neither
+  * hold the reader lock nor take it. */
+ static bool accel_zts_restart_begin(void)
+ {
+ 	int readers;
+ 	time_t now;
+
+ 	tsrm_mutex_lock(accel_zts_readers_mutex);
 -	readers = zend_atomic_int_load_ex(&accel_zts_readers);
++	readers = atomic_load(&accel_zts_readers);
+ 	if (readers == 0 && accel_zts_lock_reader_byte(F_SETLK, F_WRLCK)) {
+ 		return true;
+ 	}
+ 	tsrm_mutex_unlock(accel_zts_readers_mutex);
+
+ 	now = time(NULL);
+ 	if (readers == 0) {
+ 		/* readers in other processes: as without ZTS, force_restart_timeout kills them,
+ 		 * here with all their threads */
+ 		(void) accel_is_inactive();
+ 	} else if (ZCG(accel_directives).force_restart_timeout && ZCSG(force_restart_time)
+ 			&& now >= ZCSG(force_restart_time) && now >= accel_zts_next_warning) {
+ 		/* Threads cannot be killed and this process must not kill itself: the restart
+ 		 * waits for the running requests, new ones run uncached meanwhile */
+ 		zend_accel_error(ACCEL_LOG_WARNING, "Restart delayed for %ld seconds by %d threads of process %d still using the cache",
+ 			(long)(now - ZCSG(force_restart_time) + ZCG(accel_directives).force_restart_timeout), readers, getpid());
+ 		accel_zts_next_warning = now + ZCG(accel_directives).force_restart_timeout;
+ 	}
+ 	return false;
+ }
+
+ static void accel_zts_restart_end(void)
+ {
+ 	accel_zts_lock_reader_byte(F_SETLK, F_UNLCK);
+ 	accel_zts_next_warning = 0;
+ 	tsrm_mutex_unlock(accel_zts_readers_mutex);
+ }
+ #endif
+
  static zend_result zend_get_stream_timestamp(const char *filename, zend_stat_t *statbuf)
  {
  	php_stream_wrapper *wrapper;
@@@ -3022,9 -3131,9 +3134,13 @@@ ZEND_RINIT_FUNCTION(zend_accelerator
  					ZCSG(last_restart_time)++;
  				}
  				accel_restart_leave();
 +
 +				restarted = true;
 +				restart_reason = ZCSG(restart_reason);
++
+ #if defined(ZTS) && !defined(ZEND_WIN32)
+ 				accel_zts_restart_end();
+ #endif
  			}
  		}
  		zend_shared_alloc_unlock();
@@@ -3595,6 -3722,13 +3736,13 @@@ static zend_result accel_post_startup(v

  		/* from this point further, shared memory is supposed to be OK */

+ #if defined(ZTS) && !defined(ZEND_WIN32)
+ 		/* before preloading runs its request and before SAPIs fork their children */
+ 		accel_zts_readers_mutex = tsrm_mutex_alloc();
 -		ZEND_ATOMIC_INT_INIT(&accel_zts_readers, 0);
++		atomic_init(&accel_zts_readers, 0);
+ 		accel_zts_next_warning = 0;
+ #endif
+
  		/* remember the last restart time in the process memory */
  		ZCG(last_restart_time) = ZCSG(last_restart_time);