Skip to content

Commit 5c28152

Browse files
handle nil thread handler
1 parent f4211e1 commit 5c28152

File tree

4 files changed

+20
-3
lines changed

4 files changed

+20
-3
lines changed

debugstate.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ func threadDebugState(thread *phpThread) ThreadDebugState {
6767
thread.handlerMu.RLock()
6868
defer thread.handlerMu.RUnlock()
6969

70+
if thread.handler == nil {
71+
return s
72+
}
73+
7074
fc := thread.handler.frankenPHPContext()
7175
if fc == nil || fc.request == nil || fc.responseWriter == nil {
7276
return s

phpthread.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,13 @@ func (thread *phpThread) context() context.Context {
127127

128128
func (thread *phpThread) name() string {
129129
thread.handlerMu.RLock()
130-
name := thread.handler.name()
131-
thread.handlerMu.RUnlock()
130+
defer thread.handlerMu.RUnlock()
132131

133-
return name
132+
if thread.handler == nil {
133+
return "unknown"
134+
}
135+
136+
return thread.handler.name()
134137
}
135138

136139
// 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.handlerMu.Lock()
9293
handler.ctx = ch.ctx
9394
handler.contextHolder.frankenPHPContext = ch.frankenPHPContext
95+
handler.thread.handlerMu.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.handlerMu.Lock()
102105
handler.contextHolder.frankenPHPContext = nil
103106
handler.ctx = nil
107+
handler.thread.handlerMu.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.handlerMu.Lock()
134135
handler.workerFrankenPHPContext = nil
135136
handler.workerContext = nil
137+
handler.thread.handlerMu.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.handlerMu.Lock()
238241
handler.workerContext = requestCH.ctx
239242
handler.workerFrankenPHPContext = requestCH.frankenPHPContext
243+
handler.thread.handlerMu.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.handlerMu.Lock()
298303
thread.handler.(*workerThread).workerFrankenPHPContext = nil
299304
thread.handler.(*workerThread).workerContext = nil
305+
thread.handlerMu.Unlock()
300306

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

0 commit comments

Comments
 (0)