Skip to content

Commit

Permalink
ensure worker failures do not count fatal errors during the request (#…
Browse files Browse the repository at this point in the history
…1336)

* ensure worker failures do not count fatal errors during the request

* only count towards the backoff if it was not in a request
  • Loading branch information
withinboredom authored Jan 18, 2025
1 parent 0681c63 commit d0b259d
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion thread-worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type workerThread struct {
fakeRequest *http.Request
workerRequest *http.Request
backoff *exponentialBackoff
inRequest bool // true if the worker is currently handling a request
}

func convertToWorkerThread(thread *phpThread, worker *worker) {
Expand Down Expand Up @@ -130,14 +131,15 @@ func tearDownWorkerScript(handler *workerThread, exitStatus int) {

// on exit status 1 we apply an exponential backoff when restarting
metrics.StopWorker(worker.fileName, StopReasonCrash)
if handler.backoff.recordFailure() {
if !handler.inRequest && handler.backoff.recordFailure() {
if !watcherIsEnabled {
logger.Panic("too many consecutive worker failures", zap.String("worker", worker.fileName), zap.Int("failures", handler.backoff.failureCount))
}
logger.Warn("many consecutive worker failures", zap.String("worker", worker.fileName), zap.Int("failures", handler.backoff.failureCount))
}
}

// waitForWorkerRequest is called during frankenphp_handle_request in the php worker script.
func (handler *workerThread) waitForWorkerRequest() bool {
// unpin any memory left over from previous requests
handler.thread.Unpin()
Expand Down Expand Up @@ -172,6 +174,7 @@ func (handler *workerThread) waitForWorkerRequest() bool {
if c := logger.Check(zapcore.DebugLevel, "request handling started"); c != nil {
c.Write(zap.String("worker", handler.worker.fileName), zap.String("url", r.RequestURI))
}
handler.inRequest = true

if err := updateServerContext(handler.thread, r, false, true); err != nil {
// Unexpected error or invalid request
Expand All @@ -185,15 +188,20 @@ func (handler *workerThread) waitForWorkerRequest() bool {

return handler.waitForWorkerRequest()
}

return true
}

// go_frankenphp_worker_handle_request_start is called at the start of every php request served.
//
//export go_frankenphp_worker_handle_request_start
func go_frankenphp_worker_handle_request_start(threadIndex C.uintptr_t) C.bool {
handler := phpThreads[threadIndex].handler.(*workerThread)
return C.bool(handler.waitForWorkerRequest())
}

// go_frankenphp_finish_worker_request is called at the end of every php request served.
//
//export go_frankenphp_finish_worker_request
func go_frankenphp_finish_worker_request(threadIndex C.uintptr_t) {
thread := phpThreads[threadIndex]
Expand All @@ -202,6 +210,7 @@ func go_frankenphp_finish_worker_request(threadIndex C.uintptr_t) {

maybeCloseContext(fc)
thread.handler.(*workerThread).workerRequest = nil
thread.handler.(*workerThread).inRequest = false

if c := fc.logger.Check(zapcore.DebugLevel, "request handling finished"); c != nil {
c.Write(zap.String("worker", fc.scriptFilename), zap.String("url", r.RequestURI))
Expand Down

0 comments on commit d0b259d

Please sign in to comment.