Skip to content

Commit 7d05cb9

Browse files
handle nil thread handler
1 parent f4211e1 commit 7d05cb9

File tree

4 files changed

+27
-5
lines changed

4 files changed

+27
-5
lines changed

debugstate.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,17 @@ func threadDebugState(thread *phpThread) ThreadDebugState {
6565
}
6666

6767
thread.handlerMu.RLock()
68-
defer thread.handlerMu.RUnlock()
68+
handler := thread.handler
69+
thread.handlerMu.RUnlock()
6970

70-
fc := thread.handler.frankenPHPContext()
71+
if handler == nil {
72+
return s
73+
}
74+
75+
thread.contextMu.RLock()
76+
defer thread.contextMu.RUnlock()
77+
78+
fc := handler.frankenPHPContext()
7179
if fc == nil || fc.request == nil || fc.responseWriter == nil {
7280
return s
7381
}

phpthread.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ type phpThread struct {
2222
drainChan chan struct{}
2323
handlerMu sync.RWMutex
2424
handler threadHandler
25+
contextMu sync.RWMutex
2526
state *state.ThreadState
2627
requestCount atomic.Int64
2728
}
@@ -127,10 +128,13 @@ func (thread *phpThread) context() context.Context {
127128

128129
func (thread *phpThread) name() string {
129130
thread.handlerMu.RLock()
130-
name := thread.handler.name()
131-
thread.handlerMu.RUnlock()
131+
defer thread.handlerMu.RUnlock()
132132

133-
return name
133+
if thread.handler == nil {
134+
return "unknown"
135+
}
136+
137+
return thread.handler.name()
134138
}
135139

136140
// Pin a string that is not null-terminated

threadregular.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,10 @@ func (handler *regularThread) waitForRequest() string {
8989
case ch = <-handler.thread.requestChan:
9090
}
9191

92+
handler.thread.contextMu.Lock()
9293
handler.ctx = ch.ctx
9394
handler.contextHolder.frankenPHPContext = ch.frankenPHPContext
95+
handler.thread.contextMu.Unlock()
9496
handler.state.MarkAsWaiting(false)
9597

9698
// set the scriptFilename that should be executed
@@ -99,8 +101,10 @@ func (handler *regularThread) waitForRequest() string {
99101

100102
func (handler *regularThread) afterRequest() {
101103
handler.contextHolder.frankenPHPContext.closeContext()
104+
handler.thread.contextMu.Lock()
102105
handler.contextHolder.frankenPHPContext = nil
103106
handler.ctx = nil
107+
handler.thread.contextMu.Unlock()
104108
}
105109

106110
func handleRequestWithRegularPHPThreads(ch contextHolder) error {

threadworker.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,10 @@ func tearDownWorkerScript(handler *workerThread, exitStatus int) {
131131
// make sure to close the worker request context
132132
if handler.workerFrankenPHPContext != nil {
133133
handler.workerFrankenPHPContext.closeContext()
134+
handler.thread.contextMu.Lock()
134135
handler.workerFrankenPHPContext = nil
135136
handler.workerContext = nil
137+
handler.thread.contextMu.Unlock()
136138
}
137139

138140
// on exit status 0 we just run the worker script again
@@ -235,8 +237,10 @@ func (handler *workerThread) waitForWorkerRequest() (bool, any) {
235237
case requestCH = <-handler.worker.requestChan:
236238
}
237239

240+
handler.thread.contextMu.Lock()
238241
handler.workerContext = requestCH.ctx
239242
handler.workerFrankenPHPContext = requestCH.frankenPHPContext
243+
handler.thread.contextMu.Unlock()
240244
handler.state.MarkAsWaiting(false)
241245

242246
if globalLogger.Enabled(requestCH.ctx, slog.LevelDebug) {
@@ -295,8 +299,10 @@ func go_frankenphp_finish_worker_request(threadIndex C.uintptr_t, retval *C.zval
295299
thread.requestCount.Add(1)
296300

297301
fc.closeContext()
302+
thread.contextMu.Lock()
298303
thread.handler.(*workerThread).workerFrankenPHPContext = nil
299304
thread.handler.(*workerThread).workerContext = nil
305+
thread.contextMu.Unlock()
300306

301307
if globalLogger.Enabled(ctx, slog.LevelDebug) {
302308
if fc.request == nil {

0 commit comments

Comments
 (0)