@@ -3,6 +3,7 @@ package frankenphp
33// #include "frankenphp.h"
44import "C"
55import (
6+ "context"
67 "log/slog"
78 "path/filepath"
89 "time"
@@ -93,13 +94,15 @@ func setupWorkerScript(handler *workerThread, worker *worker) {
9394 handler .dummyContext = fc
9495 handler .isBootingScript = true
9596 clearSandboxedEnv (handler .thread )
96- logger .LogAttrs (nil , slog .LevelDebug , "starting" , slog .String ("worker" , worker .name ), slog .Int ("thread" , handler .thread .threadIndex ))
97+ logger .LogAttrs (context . Background () , slog .LevelDebug , "starting" , slog .String ("worker" , worker .name ), slog .Int ("thread" , handler .thread .threadIndex ))
9798}
9899
99100func tearDownWorkerScript (handler * workerThread , exitStatus int ) {
100101 worker := handler .worker
101102 handler .dummyContext = nil
102103
104+ ctx := context .Background ()
105+
103106 // if the worker request is not nil, the script might have crashed
104107 // make sure to close the worker request context
105108 if handler .workerContext != nil {
@@ -112,7 +115,7 @@ func tearDownWorkerScript(handler *workerThread, exitStatus int) {
112115 // TODO: make the max restart configurable
113116 metrics .StopWorker (worker .name , StopReasonRestart )
114117 handler .backoff .recordSuccess ()
115- logger .LogAttrs (nil , slog .LevelDebug , "restarting" , slog .String ("worker" , worker .name ))
118+ logger .LogAttrs (ctx , slog .LevelDebug , "restarting" , slog .String ("worker" , worker .name ))
116119 return
117120 }
118121
@@ -124,15 +127,15 @@ func tearDownWorkerScript(handler *workerThread, exitStatus int) {
124127 return
125128 }
126129
127- logger .LogAttrs (nil , slog .LevelError , "worker script has not reached frankenphp_handle_request" , slog .String ("worker" , worker .name ))
130+ logger .LogAttrs (ctx , slog .LevelError , "worker script has not reached frankenphp_handle_request" , slog .String ("worker" , worker .name ))
128131
129132 // panic after exponential backoff if the worker has never reached frankenphp_handle_request
130133 if handler .backoff .recordFailure () {
131134 if ! watcherIsEnabled && ! handler .state .is (stateReady ) {
132- logger .LogAttrs (nil , slog .LevelError , "too many consecutive worker failures" , slog .String ("worker" , worker .name ), slog .Int ("failures" , handler .backoff .failureCount ))
135+ logger .LogAttrs (ctx , slog .LevelError , "too many consecutive worker failures" , slog .String ("worker" , worker .name ), slog .Int ("failures" , handler .backoff .failureCount ))
133136 panic ("too many consecutive worker failures" )
134137 }
135- logger .LogAttrs (nil , slog .LevelWarn , "many consecutive worker failures" , slog .String ("worker" , worker .name ), slog .Int ("failures" , handler .backoff .failureCount ))
138+ logger .LogAttrs (ctx , slog .LevelWarn , "many consecutive worker failures" , slog .String ("worker" , worker .name ), slog .Int ("failures" , handler .backoff .failureCount ))
136139 }
137140}
138141
@@ -141,7 +144,8 @@ func (handler *workerThread) waitForWorkerRequest() bool {
141144 // unpin any memory left over from previous requests
142145 handler .thread .Unpin ()
143146
144- logger .LogAttrs (nil , slog .LevelDebug , "waiting for request" , slog .String ("worker" , handler .worker .name ))
147+ ctx := context .Background ()
148+ logger .LogAttrs (ctx , slog .LevelDebug , "waiting for request" , slog .String ("worker" , handler .worker .name ))
145149
146150 // Clear the first dummy request created to initialize the worker
147151 if handler .isBootingScript {
@@ -164,7 +168,7 @@ func (handler *workerThread) waitForWorkerRequest() bool {
164168 var fc * frankenPHPContext
165169 select {
166170 case <- handler .thread .drainChan :
167- logger .LogAttrs (nil , slog .LevelDebug , "shutting down" , slog .String ("worker" , handler .worker .name ))
171+ logger .LogAttrs (ctx , slog .LevelDebug , "shutting down" , slog .String ("worker" , handler .worker .name ))
168172
169173 // flush the opcache when restarting due to watcher or admin api
170174 // note: this is done right before frankenphp_handle_request() returns 'false'
@@ -180,11 +184,11 @@ func (handler *workerThread) waitForWorkerRequest() bool {
180184 handler .workerContext = fc
181185 handler .state .markAsWaiting (false )
182186
183- logger .LogAttrs (nil , slog .LevelDebug , "request handling started" , slog .String ("worker" , handler .worker .name ), slog .String ("url" , fc .request .RequestURI ))
187+ logger .LogAttrs (ctx , slog .LevelDebug , "request handling started" , slog .String ("worker" , handler .worker .name ), slog .String ("url" , fc .request .RequestURI ))
184188
185189 if err := updateServerContext (handler .thread , fc , true ); err != nil {
186190 // Unexpected error or invalid request
187- logger .LogAttrs (nil , slog .LevelDebug , "unexpected error" , slog .String ("worker" , handler .worker .name ), slog .String ("url" , fc .request .RequestURI ), slog .Any ("error" , err ))
191+ logger .LogAttrs (ctx , slog .LevelDebug , "unexpected error" , slog .String ("worker" , handler .worker .name ), slog .String ("url" , fc .request .RequestURI ), slog .Any ("error" , err ))
188192 fc .rejectBadRequest (err .Error ())
189193 handler .workerContext = nil
190194
@@ -212,7 +216,7 @@ func go_frankenphp_finish_worker_request(threadIndex C.uintptr_t) {
212216 fc .closeContext ()
213217 thread .handler .(* workerThread ).workerContext = nil
214218
215- fc .logger .LogAttrs (nil , slog .LevelDebug , "request handling finished" , slog .String ("worker" , fc .scriptFilename ), slog .String ("url" , fc .request .RequestURI ))
219+ fc .logger .LogAttrs (context . Background () , slog .LevelDebug , "request handling finished" , slog .String ("worker" , fc .scriptFilename ), slog .String ("url" , fc .request .RequestURI ))
216220}
217221
218222// when frankenphp_finish_request() is directly called from PHP
@@ -222,5 +226,5 @@ func go_frankenphp_finish_php_request(threadIndex C.uintptr_t) {
222226 fc := phpThreads [threadIndex ].getRequestContext ()
223227 fc .closeContext ()
224228
225- fc .logger .LogAttrs (nil , slog .LevelDebug , "request handling finished" , slog .String ("url" , fc .request .RequestURI ))
229+ fc .logger .LogAttrs (context . Background () , slog .LevelDebug , "request handling finished" , slog .String ("url" , fc .request .RequestURI ))
226230}
0 commit comments