Session, File, And Process Races

Shared sessions, files, and long-running process memory create concurrency outside normal database rows.

Why This Matters

PHP session locks may serialize requests, filesystem writes can tear or overwrite, and worker globals can leak across requests.

Working Model

Shared sessions, files, and long-running process memory create concurrency outside normal database rows. Correctness comes from preserving an explicit invariant across every permitted ordering, not from expecting one observed timing.

Practical Rules

  • Close sessions before slow independent work.
  • Use atomic rename for complete file replacement.
  • Use file locks only with a defined ownership protocol.
  • Avoid request-specific global state in workers.
  • Prefer durable shared services for multi-host coordination.

Failure Modes

  • Holding a session lock during API calls.
  • Writing shared JSON files in place.
  • Assuming local filesystem locks coordinate multiple hosts.
  • Reusing mutable singleton state in worker mode.

Verification

  • Run same-user concurrent requests.
  • Kill a writer during file replacement.
  • Exercise multiple processes and hosts.
  • Reset worker state between requests.

What You Should Be Able To Do

After this lesson, you should be able to explain session locking, atomic file replacement, process coordination, and long-running state leakage, choose a suitable approach for a real PHP project, and verify the result instead of relying on assumptions.

Practice

Practice: Release A Session Lock

A request reads session identity then performs a slow export.

Your answer must:

  • state the intended outcome;
  • show the commands, data flow, or implementation shape;
  • identify at least one unsafe alternative;
  • explain how the result will be verified.
Show solution

Read and validate required values, call session_write_close(), then perform the export without blocking the user's other session requests.

The important part is not memorising one command or vendor screen. The solution makes the invariant, failure behavior, and verification evidence explicit.

Practice: Write A Cache File Atomically

Several workers may refresh one JSON cache file.

Your answer must:

  • state the intended outcome;
  • show the commands, data flow, or implementation shape;
  • identify at least one unsafe alternative;
  • explain how the result will be verified.
Show solution

Write complete content to a unique temporary file, flush as required, then rename atomically within the same filesystem. Coordinate refresh ownership separately.

The important part is not memorising one command or vendor screen. The solution makes the invariant, failure behavior, and verification evidence explicit.

Practice: Audit Worker State

Review mutable static state in a long-running PHP worker.

Your answer must:

  • state the intended outcome;
  • show the commands, data flow, or implementation shape;
  • identify at least one unsafe alternative;
  • explain how the result will be verified.
Show solution

Classify immutable boot configuration versus request state, reset or avoid mutable globals, and run sequential requests that would reveal cross-user leakage.

The important part is not memorising one command or vendor screen. The solution makes the invariant, failure behavior, and verification evidence explicit.