Prefork mode state isolation and request-scoped cleanup in Fiber v3
0 reputation · 02 Oct 2025, 07:56 UTC
0 reputation · 02 Oct 2025, 07:56 UTC
Goal is to clarify request-scoped storage cleanup and state isolation guarantees for Fiber v3 applications running with Prefork enabled and custom middleware that references App-level state. The configuration transition from single-process to Prefork changes the process model and initialization semantics compared with the default single-process mode.
Fiber v3 reorganizes router and context APIs and enforces stricter context usage relative to v2. Prefork is documented as spawning multiple OS processes behind a listener, which alters how global state, middleware initialization and graceful shutdown are handled. The documentation describes the process model change but does not specify cleanup timing for request-scoped values or isolation guarantees for App-level state accessed by middleware across workers.
What cleanup guarantees does Fiber v3 provide for request-scoped storage when Prefork is enabled? Are App-level values initialized once per worker or shared across workers, and is that isolation documented? How should middleware that uses App-level state be structured to avoid cross-request leakage under Prefork?
35775 reputation · 02 Oct 2025, 09:38 UTC
Fiber v3 does not provide a Prefork-specific cleanup callback or deterministic finalizer for request-scoped storage. Cleanup of request-scoped values is the normal Go handler lifetime: ctx.Locals lives on *fiber.Ctx for the duration of the current request/handler goroutine and becomes eligible for garbage collection once the handler returns. Prefork does not change that lifetime.
App-level values are not shared live across Prefork workers. Prefork creates separate OS processes from a shared listener. After fork, process memory is copy-on-write, so each worker has its own independent copy of the address space. Mutations in one worker are not visible to others unless stored in an external shared store.
Whether an App-level value is initialized once per worker is an application sequencing concern, not a documented Fiber isolation guarantee. State built before the listener starts is fork-copied into each worker image. State initialized after fork or lazily on first use is independent per worker.
Confirmed:
ctx.Locals is request-scoped to the current *fiber.Ctx and handler goroutine.Likely but version-sensitive:
app.Listen with Prefork enabled. Before listen = fork-copied baseline per worker. After fork / lazy = per-worker independent initialization.*fiber.Ctx, ctx.Locals values, or request-scoped pointers in App-level or global variables.defer inside the same handler: DB rows, files, timers. Do not rely on worker shutdown for per-request cleanup.os.Getpid() and a per-worker counter incremented on each request. Counters should be per-pid, not cumulative across workers.ctx.Locals keys and assert the same Locals value is never observed in a different request.One missing diagnostic detail that changes the recommendation: is your App-level state constructed before app.Listen is called with Prefork enabled, or is it initialized lazily inside middleware/handler after the fork? Pre-listen construction means fork-copied baseline per worker; post-fork/lazy means per-worker independent state.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.