diff --git a/caddy/caddy_test.go b/caddy/caddy_test.go index e359e3d4b..aaa600d64 100644 --- a/caddy/caddy_test.go +++ b/caddy/caddy_test.go @@ -3,6 +3,7 @@ package caddy_test import ( "bytes" "fmt" + "math/rand/v2" "net/http" "os" "path/filepath" @@ -11,6 +12,7 @@ import ( "sync" "sync/atomic" "testing" + "time" "github.com/caddyserver/caddy/v2" "github.com/caddyserver/caddy/v2/caddytest" @@ -1473,6 +1475,72 @@ func TestDd(t *testing.T) { ) } +// test to force the opcache segfault race condition under concurrency (~1.7s) +func TestOpcacheReset(t *testing.T) { + tester := caddytest.NewTester(t) + tester.InitServer(` + { + skip_install_trust + admin localhost:2999 + http_port `+testPort+` + metrics + + frankenphp { + num_threads 40 + php_ini { + opcache.enable 1 + opcache.log_verbosity_level 4 + } + } + } + + localhost:`+testPort+` { + php { + root ../testdata + worker { + file sleep.php + match /sleep* + num 20 + } + } + } + `, "caddyfile") + + wg := sync.WaitGroup{} + numRequests := 100 + wg.Add(numRequests) + for i := 0; i < numRequests; i++ { + + // introduce some random delay + if rand.IntN(10) > 8 { + time.Sleep(time.Millisecond * 10) + } + + go func() { + // randomly call opcache_reset + if rand.IntN(10) > 5 { + tester.AssertGetResponse( + "http://localhost:"+testPort+"/opcache_reset.php", + http.StatusOK, + "opcache reset done", + ) + wg.Done() + return + } + + // otherwise call sleep.php with random sleep and work values + tester.AssertGetResponse( + fmt.Sprintf("http://localhost:%s/sleep.php?sleep=%d&work=%d", testPort, i, i), + http.StatusOK, + fmt.Sprintf("slept for %d ms and worked for %d iterations", i, i), + ) + wg.Done() + }() + } + + wg.Wait() +} + func TestLog(t *testing.T) { tester := caddytest.NewTester(t) tester.InitServer(` diff --git a/frankenphp.c b/frankenphp.c index d048e576b..d586cb536 100644 --- a/frankenphp.c +++ b/frankenphp.c @@ -73,6 +73,7 @@ bool should_filter_var = 0; __thread uintptr_t thread_index; __thread bool is_worker_thread = false; __thread zval *os_environment = NULL; +zif_handler orig_opcache_reset; void frankenphp_update_local_thread_context(bool is_worker) { is_worker_thread = is_worker; @@ -340,6 +341,13 @@ PHP_FUNCTION(frankenphp_getenv) { } } /* }}} */ +/* {{{ thread-safe opcache reset */ +PHP_FUNCTION(frankenphp_opcache_reset) { + go_schedule_opcache_reset(thread_index); + + RETVAL_TRUE; +} /* }}} */ + /* {{{ Fetch all HTTP request headers */ PHP_FUNCTION(frankenphp_request_headers) { ZEND_PARSE_PARAMETERS_NONE(); @@ -593,6 +601,15 @@ PHP_MINIT_FUNCTION(frankenphp) { php_error(E_WARNING, "Failed to find built-in getenv function"); } + // Override opcache_reset + func = zend_hash_str_find_ptr(CG(function_table), "opcache_reset", + sizeof("opcache_reset") - 1); + if (func != NULL && func->type == ZEND_INTERNAL_FUNCTION) { + orig_opcache_reset = ((zend_internal_function *)func)->handler; + ((zend_internal_function *)func)->handler = + ZEND_FN(frankenphp_opcache_reset); + } + return SUCCESS; } @@ -1254,7 +1271,11 @@ int frankenphp_reset_opcache(void) { zend_function *opcache_reset = zend_hash_str_find_ptr(CG(function_table), ZEND_STRL("opcache_reset")); if (opcache_reset) { + php_request_startup(); + ((zend_internal_function *)opcache_reset)->handler = orig_opcache_reset; zend_call_known_function(opcache_reset, NULL, NULL, NULL, 0, NULL, NULL); + ((zend_internal_function *)opcache_reset)->handler = ZEND_FN(frankenphp_opcache_reset); + php_request_shutdown((void *)0); } return 0; diff --git a/frankenphp.go b/frankenphp.go index 693870e1d..2d6a6511a 100644 --- a/frankenphp.go +++ b/frankenphp.go @@ -32,11 +32,14 @@ import ( "runtime" "strings" "sync" + "sync/atomic" "syscall" "time" "unsafe" // debug on Linux //_ "github.com/ianlancetaylor/cgosymbolizer" + + "github.com/dunglas/frankenphp/internal/state" ) type contextKeyStruct struct{} @@ -56,8 +59,9 @@ var ( contextKey = contextKeyStruct{} serverHeader = []string{"FrankenPHP"} - isRunning bool - onServerShutdown []func() + isRunning bool + threadsAreRestarting atomic.Bool + onServerShutdown []func() // Set default values to make Shutdown() idempotent globalMu sync.Mutex @@ -753,6 +757,116 @@ func go_is_context_done(threadIndex C.uintptr_t) C.bool { return C.bool(phpThreads[threadIndex].frankenPHPContext().isDone) } +//export go_schedule_opcache_reset +func go_schedule_opcache_reset(threadIndex C.uintptr_t) { + if threadsAreRestarting.CompareAndSwap(false, true) { + go restartThreadsAndOpcacheReset(true) + } +} + +// restart all threads for an opcache_reset +func restartThreadsAndOpcacheReset(withRegularThreads bool) { + // disallow scaling threads while restarting workers + scalingMu.Lock() + defer scalingMu.Unlock() + + threadsToRestart := drainThreads(withRegularThreads) + + opcacheResetWg := sync.WaitGroup{} + for _, thread := range threadsToRestart { + thread.state.Set(state.OpcacheResetting) + opcacheResetWg.Go(func() { + thread.state.WaitFor(state.OpcacheResettingDone) + }) + } + + opcacheResetWg.Wait() + + for _, thread := range threadsToRestart { + thread.drainChan = make(chan struct{}) + thread.state.Set(state.Ready) + } + + threadsAreRestarting.Store(false) +} + +func drainThreads(withRegularThreads bool) []*phpThread { + var ( + ready sync.WaitGroup + drainedThreads []*phpThread + ) + + for _, worker := range workers { + worker.threadMutex.RLock() + ready.Add(len(worker.threads)) + + for _, thread := range worker.threads { + if !thread.state.RequestSafeStateChange(state.Restarting) { + ready.Done() + + // no state change allowed == thread is shutting down + // we'll proceed to restart all other threads anyway + continue + } + close(thread.drainChan) + drainedThreads = append(drainedThreads, thread) + + go func(thread *phpThread) { + thread.state.WaitFor(state.Yielding) + ready.Done() + }(thread) + } + + worker.threadMutex.RUnlock() + } + + if withRegularThreads { + regularThreadMu.RLock() + ready.Add(len(regularThreads)) + + for _, thread := range regularThreads { + if !thread.state.RequestSafeStateChange(state.Restarting) { + ready.Done() + + // no state change allowed == thread is shutting down + // we'll proceed to restart all other threads anyway + continue + } + close(thread.drainChan) + drainedThreads = append(drainedThreads, thread) + + go func(thread *phpThread) { + thread.state.WaitFor(state.Yielding) + ready.Done() + }(thread) + } + + regularThreadMu.RUnlock() + } + + ready.Wait() + + return drainedThreads +} + +func scheduleOpcacheReset(thread *phpThread) { + fc, _ := newDummyContext("/opcache_reset") + + if workerThread, ok := thread.handler.(*workerThread); ok { + workerThread.dummyFrankenPHPContext = fc + defer func() { workerThread.dummyFrankenPHPContext = nil }() + } + + if regularThread, ok := thread.handler.(*regularThread); ok { + regularThread.contextHolder.frankenPHPContext = fc + defer func() { regularThread.contextHolder.frankenPHPContext = nil }() + } + + globalLogger.Info("resetting opcache", "thread", thread.name()) + C.frankenphp_reset_opcache() + globalLogger.Info("opcache reset completed", "thread", thread.name()) +} + // ExecuteScriptCLI executes the PHP script passed as parameter. // It returns the exit status code of the script. func ExecuteScriptCLI(script string, args []string) int { diff --git a/internal/state/state.go b/internal/state/state.go index b15c008bc..3eba4dc47 100644 --- a/internal/state/state.go +++ b/internal/state/state.go @@ -24,6 +24,8 @@ const ( // States necessary for restarting workers Restarting State = "restarting" Yielding State = "yielding" + OpcacheResetting State = "opcache resetting" + OpcacheResettingDone State = "opcache reset done" // States necessary for transitioning between different handlers TransitionRequested State = "transition requested" diff --git a/phpmainthread_test.go b/phpmainthread_test.go index 7e6bf32c1..b54647ae0 100644 --- a/phpmainthread_test.go +++ b/phpmainthread_test.go @@ -96,7 +96,7 @@ func TestTransitionThreadsWhileDoingRequests(t *testing.T) { var ( isDone atomic.Bool - wg sync.WaitGroup + wg sync.WaitGroup ) numThreads := 10 diff --git a/testdata/opcache_reset.php b/testdata/opcache_reset.php new file mode 100644 index 000000000..b19a80bf4 --- /dev/null +++ b/testdata/opcache_reset.php @@ -0,0 +1,9 @@ +