Skip to content

fix(pii): load models only in workers and recycle them before memory runs out - #9060

Merged
waleedlatif1 merged 2 commits into
stagingfrom
fix/pii-worker-memory
Oct 15, 2026
Merged

waleedlatif1 merged 2 commits into
stagingfrom
fix/pii-worker-memory

Conversation

@waleedlatif1

Copy link
Copy Markdown
Collaborator

Summary

  • uvicorn's supervisor imports the app before spawning workers, so the module-level engine build left the parent holding an idle extra copy of every spaCy model. Engines now build in the FastAPI lifespan, which only serving processes run
  • With more than one worker, each exits after a jittered request count (PII_MAX_REQUESTS, default 4000 ± PII_MAX_REQUESTS_JITTER 2000, health checks included) and the supervisor respawns it, so per-worker memory growth can't run the task into its hard limit between deploys
  • A single worker never recycles (no supervisor to respawn it); --timeout-graceful-shutdown 300 matches the ALB idle timeout so in-flight redactions finish

Type of Change

  • Bug fix

Testing

  • AST scan: no import-time use of the engine globals remains; uvicorn 0.49 source confirms run() loads the app in the supervisor and keep_subprocess_alive respawns any exited worker
  • CMD shell expansion checked for PII_WORKERS = 8, 1, unset, and malformed (malformed still fails uvicorn arg parsing)
  • Validate on staging after deploy: one "building analyzer" log per worker (not workers + 1), healthy targets, lower starting memory, and workers restarting without 5xx

Checklist

  • Code follows project style guidelines
  • Self-reviewed my changes
  • Tests added/updated and passing (new tests pass the test-audit authoring gate)
  • No new warnings introduced
  • I confirm that I have read and agree to the terms outlined in the Contributor License Agreement (CLA)

@vercel

vercel Bot commented Oct 15, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
docs Ready Ready Preview Oct 15, 2026 1:29am UTC

Request Review

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 2 files

Reply with feedback, questions, or to request a fix.

Turn on auto-fix | Re-trigger cubic

Comment thread docker/pii.Dockerfile Outdated
@greptile-apps

greptile-apps Bot commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[High impact] The PR appears safe to merge; no actionable new issue was established.

Summary

Moves PII engine setup into FastAPI’s lifespan and adds a Docker entrypoint for worker recycling.

  • Each PII worker builds its models when it starts serving.
  • Multi-worker PII containers recycle workers after varied request counts.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A[Docker entrypoint] --> B{More than one worker?}
    B -->|Yes| C[Add request limit and jitter]
    B -->|No| D[Leave recycling disabled]
    C --> E[Start uvicorn]
    D --> E
    E --> F[Worker runs FastAPI lifespan]
    F --> G[Build engines and blank artifacts]
    G --> H[Serve requests]
    H --> I{Request limit reached?}
    I -->|Multiple workers only| J[Drain and exit]
    J --> K[Supervisor starts replacement]
    K --> F
Loading

Reviews (2) · Last reviewed commit: "improvement(pii): move the start command..." · Reviewed by Greptile

…runs out

The uvicorn supervisor imports the app before spawning workers, so the
module-level engine build left it holding an idle ninth copy of every model.
Engines now build in the FastAPI lifespan, which runs only in serving
processes. With more than one worker, each now exits after a jittered request
count and the supervisor respawns it, bounding per-worker memory growth that
otherwise reached the task limit about 11 days after a deploy.
@waleedlatif1
waleedlatif1 force-pushed the fix/pii-worker-memory branch from ab78626 to 5541251 Compare October 15, 2026 01:24
@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@greptile

@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@cubic-dev-ai review this PR

@cubic-dev-ai

cubic-dev-ai Bot commented Oct 15, 2026

Copy link
Copy Markdown
Contributor

@cubic-dev-ai review this PR

@waleedlatif1 I have started the AI code review. It will take a few minutes to complete.

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 3 files

Confidence score: 5/5

  • Automated review surfaced no issues in the provided summaries.
  • No files require special attention.

Turn on auto-fix | Re-trigger cubic

@waleedlatif1
waleedlatif1 merged commit f2d7826 into staging Oct 15, 2026
53 checks passed
@waleedlatif1
waleedlatif1 deleted the fix/pii-worker-memory branch October 15, 2026 02:07

This branch was successfully deployed

1 active deployment
Preview — 5541251b Deployed Oct 15, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant