Skip to content

Commit 60c3e12

Browse files
committed
caddy can shift FrankenPHPModules in memory for some godforsaken reason, can't rely on them staying the same
1 parent 7fffbc2 commit 60c3e12

File tree

6 files changed

+19
-11
lines changed

6 files changed

+19
-11
lines changed

caddy/caddy.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
package caddy
55

66
import (
7+
"crypto/sha256"
8+
"encoding/hex"
79
"encoding/json"
810
"errors"
911
"fmt"
@@ -12,7 +14,6 @@ import (
1214
"strconv"
1315
"strings"
1416
"time"
15-
"unsafe"
1617

1718
"github.com/dunglas/frankenphp/internal/fastabs"
1819

@@ -60,7 +61,7 @@ type workerConfig struct {
6061
// Directories to watch for file changes
6162
Watch []string `json:"watch,omitempty"`
6263
// ModuleID identifies which module created this worker
63-
ModuleID uintptr `json:"module_id,omitempty"`
64+
ModuleID string `json:"module_id,omitempty"`
6465
}
6566

6667
type FrankenPHPApp struct {
@@ -356,6 +357,8 @@ type FrankenPHPModule struct {
356357
ResolveRootSymlink *bool `json:"resolve_root_symlink,omitempty"`
357358
// Env sets an extra environment variable to the given value. Can be specified more than once for multiple environment variables.
358359
Env map[string]string `json:"env,omitempty"`
360+
// ModuleID is the module ID that created this request.
361+
ModuleID string `json:"-"`
359362
// Workers configures the worker scripts to start.
360363
Workers []workerConfig `json:"workers,omitempty"`
361364

@@ -429,9 +432,13 @@ func (f *FrankenPHPModule) Provision(ctx caddy.Context) error {
429432
}
430433
}
431434

435+
data := []byte(f.Root + strings.Join(f.SplitPath, ",") + time.Now().String())
436+
hash := sha256.Sum256(data)
437+
f.ModuleID = hex.EncodeToString(hash[:8])
438+
432439
if len(f.Workers) > 0 {
433-
for i := range f.Workers {
434-
f.Workers[i].ModuleID = uintptr(unsafe.Pointer(f))
440+
for _, w := range f.Workers {
441+
w.ModuleID = f.ModuleID
435442
}
436443
moduleWorkers = append(moduleWorkers, f.Workers...)
437444
}
@@ -471,7 +478,7 @@ func (f *FrankenPHPModule) ServeHTTP(w http.ResponseWriter, r *http.Request, _ c
471478
frankenphp.WithRequestSplitPath(f.SplitPath),
472479
frankenphp.WithRequestPreparedEnv(env),
473480
frankenphp.WithOriginalRequest(&origReq),
474-
frankenphp.WithModuleID(uintptr(unsafe.Pointer(f))),
481+
frankenphp.WithModuleID(f.ModuleID),
475482
)
476483

477484
if err != nil {

context.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ type frankenPHPContext struct {
3333
startedAt time.Time
3434

3535
// The module ID that created this request
36-
moduleID uintptr
36+
moduleID string
3737
}
3838

3939
// fromContext extracts the frankenPHPContext from a context.

frankenphp.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,10 +407,11 @@ func ServeHTTP(responseWriter http.ResponseWriter, request *http.Request) error
407407
// Detect if a worker is available to handle this request
408408
if worker, ok := workers[fc.scriptFilename]; ok {
409409
// can handle with a global worker, or a module worker from the matching module
410-
if worker.moduleID == 0 || worker.moduleID == fc.moduleID {
410+
if worker.moduleID == "" || worker.moduleID == fc.moduleID {
411411
worker.handleRequest(fc)
412412
return nil
413413
}
414+
fc.logger.Warn(fmt.Sprintf("Module ID mismatch: %s != %s", worker.moduleID, fc.moduleID))
414415
}
415416

416417
// If no worker was available send the request to non-worker threads

options.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ type workerOpt struct {
2828
num int
2929
env PreparedEnv
3030
watch []string
31-
moduleID uintptr
31+
moduleID string
3232
}
3333

3434
// WithNumThreads configures the number of PHP threads to start.
@@ -57,7 +57,7 @@ func WithMetrics(m Metrics) Option {
5757
}
5858

5959
// WithWorkers configures the PHP workers to start, moduleID is used to identify the worker for a specific domain
60-
func WithWorkers(name string, fileName string, num int, env map[string]string, watch []string, moduleID uintptr) Option {
60+
func WithWorkers(name string, fileName string, num int, env map[string]string, watch []string, moduleID string) Option {
6161
return func(o *opt) error {
6262
o.workers = append(o.workers, workerOpt{name, fileName, num, PrepareEnv(env), watch, moduleID})
6363

request_options.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ func WithRequestLogger(logger *zap.Logger) RequestOption {
125125
}
126126

127127
// WithModuleID sets the module ID associated with the current request
128-
func WithModuleID(moduleID uintptr) RequestOption {
128+
func WithModuleID(moduleID string) RequestOption {
129129
return func(o *frankenPHPContext) error {
130130
o.moduleID = moduleID
131131

worker.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ type worker struct {
2020
requestChan chan *frankenPHPContext
2121
threads []*phpThread
2222
threadMutex sync.RWMutex
23-
moduleID uintptr
23+
moduleID string
2424
}
2525

2626
var (

0 commit comments

Comments
 (0)