Skip to content

Commit 8d9ce15

Browse files
fix: log worker failures (#1437)
* Small fixes on error. * Adds comments. --------- Co-authored-by: Alliballibaba <alliballibaba@gmail.com>
1 parent 409c0fd commit 8d9ce15

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

frankenphp.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,7 @@ func go_ub_write(threadIndex C.uintptr_t, cBuf *C.char, length C.int) (C.size_t,
405405
}
406406

407407
if fc.responseWriter == nil {
408+
// probably starting a worker script, log the output
408409
fc.logger.Info(writer.(*bytes.Buffer).String())
409410
}
410411

@@ -466,10 +467,15 @@ func addHeader(fc *frankenPHPContext, cString *C.char, length C.int) {
466467
func go_write_headers(threadIndex C.uintptr_t, status C.int, headers *C.zend_llist) C.bool {
467468
fc := phpThreads[threadIndex].getRequestContext()
468469

469-
if fc.isDone || fc.responseWriter == nil {
470+
if fc.isDone {
470471
return C.bool(false)
471472
}
472473

474+
if fc.responseWriter == nil {
475+
// probably starting a worker script, pretend we wrote headers so PHP still calls ub_write
476+
return C.bool(true)
477+
}
478+
473479
current := headers.head
474480
for current != nil {
475481
h := (*C.sapi_header_struct)(unsafe.Pointer(&(current.data)))

threadworker.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ func tearDownWorkerScript(handler *workerThread, exitStatus int) {
112112
}
113113

114114
// on exit status 0 we just run the worker script again
115-
if exitStatus == 0 {
115+
if exitStatus == 0 && !handler.isBootingScript {
116116
// TODO: make the max restart configurable
117117
metrics.StopWorker(worker.fileName, StopReasonRestart)
118118
handler.backoff.recordSuccess()
@@ -122,10 +122,19 @@ func tearDownWorkerScript(handler *workerThread, exitStatus int) {
122122
return
123123
}
124124

125-
// on exit status 1 we apply an exponential backoff when restarting
125+
// worker has thrown a fatal error or has not reached frankenphp_handle_request
126126
metrics.StopWorker(worker.fileName, StopReasonCrash)
127-
if handler.isBootingScript && handler.backoff.recordFailure() {
128-
if !watcherIsEnabled {
127+
128+
if !handler.isBootingScript {
129+
// fatal error (could be due to timeouts, etc.)
130+
return
131+
}
132+
133+
logger.Error("worker script has not reached frankenphp_handle_request", zap.String("worker", worker.fileName))
134+
135+
// panic after exponential backoff if the worker has never reached frankenphp_handle_request
136+
if handler.backoff.recordFailure() {
137+
if !watcherIsEnabled && !handler.state.is(stateReady) {
129138
logger.Panic("too many consecutive worker failures", zap.String("worker", worker.fileName), zap.Int("failures", handler.backoff.failureCount))
130139
}
131140
logger.Warn("many consecutive worker failures", zap.String("worker", worker.fileName), zap.Int("failures", handler.backoff.failureCount))

0 commit comments

Comments
 (0)