|
59 | 59 | contextKey = contextKeyStruct{} |
60 | 60 | serverHeader = []string{"FrankenPHP"} |
61 | 61 |
|
62 | | - isRunning bool |
63 | | - restartCounter atomic.Int32 |
64 | | - onServerShutdown []func() |
| 62 | + isRunning bool |
| 63 | + threadsAreRestarting atomic.Bool |
| 64 | + onServerShutdown []func() |
65 | 65 |
|
66 | 66 | // Set default values to make Shutdown() idempotent |
67 | 67 | globalMu sync.Mutex |
@@ -759,49 +759,111 @@ func go_is_context_done(threadIndex C.uintptr_t) C.bool { |
759 | 759 |
|
760 | 760 | //export go_schedule_opcache_reset |
761 | 761 | func go_schedule_opcache_reset(threadIndex C.uintptr_t) { |
762 | | - if restartCounter.CompareAndSwap(0, 1) { |
763 | | - go restartThreadsForOpcacheReset() |
| 762 | + if threadsAreRestarting.CompareAndSwap(false, true) { |
| 763 | + go restartThreadsAndOpcacheReset(true) |
764 | 764 | } |
765 | 765 | } |
766 | 766 |
|
767 | 767 | // restart all threads for an opcache_reset |
768 | | -func restartThreadsForOpcacheReset() { |
| 768 | +func restartThreadsAndOpcacheReset(withRegularThreads bool) { |
769 | 769 | // disallow scaling threads while restarting workers |
770 | 770 | scalingMu.Lock() |
771 | 771 | defer scalingMu.Unlock() |
772 | 772 |
|
773 | | - threadsToRestart := drainWorkerThreads(true) |
| 773 | + threadsToRestart := drainThreads(withRegularThreads) |
| 774 | + |
| 775 | + opcacheResetWg := sync.WaitGroup{} |
| 776 | + for _, thread := range threadsToRestart { |
| 777 | + thread.state.Set(state.OpcacheResetting) |
| 778 | + opcacheResetWg.Go(func() { |
| 779 | + thread.state.WaitFor(state.OpcacheResettingDone) |
| 780 | + }) |
| 781 | + } |
| 782 | + |
| 783 | + opcacheResetWg.Wait() |
774 | 784 |
|
775 | 785 | for _, thread := range threadsToRestart { |
776 | 786 | thread.drainChan = make(chan struct{}) |
777 | 787 | thread.state.Set(state.Ready) |
778 | 788 | } |
| 789 | + |
| 790 | + threadsAreRestarting.Store(false) |
779 | 791 | } |
780 | 792 |
|
781 | | -func scheduleOpcacheReset(thread *phpThread) { |
782 | | - restartCounter.Add(-1) |
783 | | - if restartCounter.Load() != 1 { |
784 | | - return // only the last restarting thread will trigger an actual opcache_reset |
| 793 | +func drainThreads(withRegularThreads bool) []*phpThread { |
| 794 | + var ( |
| 795 | + ready sync.WaitGroup |
| 796 | + drainedThreads []*phpThread |
| 797 | + ) |
| 798 | + |
| 799 | + for _, worker := range workers { |
| 800 | + worker.threadMutex.RLock() |
| 801 | + ready.Add(len(worker.threads)) |
| 802 | + |
| 803 | + for _, thread := range worker.threads { |
| 804 | + if !thread.state.RequestSafeStateChange(state.Restarting) { |
| 805 | + ready.Done() |
| 806 | + |
| 807 | + // no state change allowed == thread is shutting down |
| 808 | + // we'll proceed to restart all other threads anyway |
| 809 | + continue |
| 810 | + } |
| 811 | + close(thread.drainChan) |
| 812 | + drainedThreads = append(drainedThreads, thread) |
| 813 | + |
| 814 | + go func(thread *phpThread) { |
| 815 | + thread.state.WaitFor(state.Yielding) |
| 816 | + ready.Done() |
| 817 | + }(thread) |
| 818 | + } |
| 819 | + |
| 820 | + worker.threadMutex.RUnlock() |
| 821 | + } |
| 822 | + |
| 823 | + if withRegularThreads { |
| 824 | + regularThreadMu.RLock() |
| 825 | + ready.Add(len(regularThreads)) |
| 826 | + |
| 827 | + for _, thread := range regularThreads { |
| 828 | + if !thread.state.RequestSafeStateChange(state.Restarting) { |
| 829 | + ready.Done() |
| 830 | + |
| 831 | + // no state change allowed == thread is shutting down |
| 832 | + // we'll proceed to restart all other threads anyway |
| 833 | + continue |
| 834 | + } |
| 835 | + close(thread.drainChan) |
| 836 | + drainedThreads = append(drainedThreads, thread) |
| 837 | + |
| 838 | + go func(thread *phpThread) { |
| 839 | + thread.state.WaitFor(state.Yielding) |
| 840 | + ready.Done() |
| 841 | + }(thread) |
| 842 | + } |
| 843 | + |
| 844 | + regularThreadMu.RUnlock() |
785 | 845 | } |
786 | | - workerThread, ok := thread.handler.(*workerThread) |
| 846 | + |
| 847 | + ready.Wait() |
| 848 | + |
| 849 | + return drainedThreads |
| 850 | +} |
| 851 | + |
| 852 | +func scheduleOpcacheReset(thread *phpThread) { |
787 | 853 | fc, _ := newDummyContext("/opcache_reset") |
788 | | - if ok && workerThread.worker != nil { |
| 854 | + |
| 855 | + if workerThread, ok := thread.handler.(*workerThread); ok { |
789 | 856 | workerThread.dummyFrankenPHPContext = fc |
790 | 857 | defer func() { workerThread.dummyFrankenPHPContext = nil }() |
791 | 858 | } |
792 | 859 |
|
793 | | - regularThread, ok := thread.handler.(*regularThread) |
794 | | - if ok { |
| 860 | + if regularThread, ok := thread.handler.(*regularThread); ok { |
795 | 861 | regularThread.contextHolder.frankenPHPContext = fc |
796 | 862 | defer func() { regularThread.contextHolder.frankenPHPContext = nil }() |
797 | 863 | } |
798 | 864 |
|
799 | 865 | globalLogger.Info("resetting opcache in all threads") |
800 | 866 | C.frankenphp_reset_opcache() |
801 | | - time.Sleep(200 * time.Millisecond) // opcache_reset grace period |
802 | | - |
803 | | - // all threads should have restarted now |
804 | | - restartCounter.Store(0) |
805 | 867 | } |
806 | 868 |
|
807 | 869 | // ExecuteScriptCLI executes the PHP script passed as parameter. |
|
0 commit comments