Skip to content

fix(proxy): expose drain status for graceful deploys - #564

Merged
Soju06 merged 3 commits into
Soju06:mainfrom
Komzpa:codex/graceful-live-redeploy
May 10, 2026
Merged

Soju06 merged 3 commits into
Soju06:mainfrom
Komzpa:codex/graceful-live-redeploy

Conversation

@Komzpa

@Komzpa Komzpa commented May 7, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

  • keep drain control endpoints reachable while the server is already draining
  • expose /internal/drain/status with draining, bridge_drain_active, and in_flight counters for deploy tooling
  • exclude health/drain observer endpoints from the in-flight counter so drain polling does not keep itself alive
  • update the Helm preStop hook to start drain and poll /internal/drain/status instead of only sleeping a fixed interval

This is a graceful-drain primitive, not a full single-container zero-downtime deploy story: websocket sessions still need an old backend kept alive until they close, and local Docker deployments need a separate stable frontend/proxy for true blue/green cutover.

Tests

  • UV_PROJECT_ENVIRONMENT=/home/kom/.venvs/codex-lb-routing-policy uv run pytest tests/unit/test_graceful_shutdown.py tests/unit/test_health_probes.py tests/unit/test_helm_external_secrets.py -q (Helm rendering tests skipped locally because helm is not installed in this environment)
  • UV_PROJECT_ENVIRONMENT=/home/kom/.venvs/codex-lb-routing-policy uv run ruff check app/core/middleware/inflight.py app/modules/health/api.py tests/unit/test_graceful_shutdown.py tests/unit/test_health_probes.py tests/unit/test_helm_external_secrets.py
  • UV_PROJECT_ENVIRONMENT=/home/kom/.venvs/codex-lb-routing-policy uv run ty check app/core/middleware/inflight.py app/modules/health/api.py tests/unit/test_graceful_shutdown.py tests/unit/test_health_probes.py tests/unit/test_helm_external_secrets.py
  • git diff --check

@Komzpa
Komzpa force-pushed the codex/graceful-live-redeploy branch from 91f90d8 to 337ced8 Compare May 7, 2026 04:50
@Soju06

Soju06 commented May 9, 2026

Copy link
Copy Markdown
Owner

@codex review

1 similar comment
@Soju06

Soju06 commented May 9, 2026

Copy link
Copy Markdown
Owner

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 46396013dd

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread app/modules/health/api.py Outdated
address = ip_address(client_host)
except ValueError:
return False
return address.is_loopback or address.is_private

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Restrict drain control access to loopback only

Tighten _is_internal_client_host because allowing address.is_private means any workload on a private network (for example, another pod in the cluster CIDR) can call /internal/drain/start and force this instance into draining mode, which immediately flips readiness and returns 503 for normal traffic. This commit changed the check from loopback-only to broad private-network trust, so it introduces an availability/security regression in multi-tenant or flat east-west network environments.

Useful? React with 👍 / 👎.

@Soju06

Soju06 commented May 9, 2026

Copy link
Copy Markdown
Owner

Thanks @Komzpa -- the polling-based preStop and the in-flight exclusion for health/drain endpoints are both right. Codex review caught one real availability/security regression I'd like to address before merge:

P1 -- private-network trust is too wide

The widening from loopback-only to address.is_private lets any workload sharing the cluster CIDR (or any RFC1918 / CGN-NAT host on a flat lab network) call POST /internal/drain/start and immediately flip readiness to 503 for normal traffic. In a multi-tenant cluster or any environment where the pod is not the only thing on 10.0.0.0/8 / 172.16.0.0/12 / 192.168.0.0/16, that's a denial-of-service surface.

The Helm preStop hook calls http://127.0.0.1:{httpPort}, so it's already on loopback inside the same pod. So the original {"127.0.0.1", "::1", "localhost"} allowlist already covers the in-pod use case. What was the failure mode that motivated widening it? If there's a legit reason (sidecar pattern, host-network mode, etc.), let's solve it more narrowly.

Suggested shapes, ranked by least-invasive first:

1. Revert to loopback-only

If the in-pod loopback path works in your environment, revert _is_internal_client_host to the original allowlist. Cleanest.

2. Env-configurable allowlist

If there is a sidecar or DaemonSet caller that needs access, add an env like CODEX_LB_DRAIN_TRUSTED_CIDRS (CSV of CIDR ranges, default empty) and only widen access when an operator explicitly opts in:

def _is_internal_client_host(client_host):
    if client_host in {"127.0.0.1", "::1", "localhost"}:
        return True
    if client_host is None:
        return False
    trusted = _get_trusted_drain_cidrs()  # env-driven
    if not trusted:
        return False
    try:
        addr = ip_address(client_host)
    except ValueError:
        return False
    return any(addr in net for net in trusted)

3. Shared secret / bearer

If we want to keep this purely network-level, an env-driven CODEX_LB_DRAIN_TOKEN checked via header (when set) gives operators a cluster-side knob without trusting the network at all.

Option 1 is what I'd push for. The Helm preStop already loops over loopback so I don't see what we lose. Option 2 is the right escape hatch if you have a legit non-loopback caller you want to keep working.

The rest of the PR (status endpoint, in-flight middleware excludes, drain endpoints reachable while draining, helm preStop with status polling, README update) all look correct.

Let me know which shape you want and I'll re-run @codex review after the new commit. Don't merge yet.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep it up!

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@Soju06

Soju06 commented May 10, 2026

Copy link
Copy Markdown
Owner

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. What shall we delve into next?

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@Soju06

Soju06 commented May 10, 2026

Copy link
Copy Markdown
Owner

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Can't wait for the next one!

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@Soju06
Soju06 merged commit a2fca41 into Soju06:main May 10, 2026
18 checks passed
@Soju06

Soju06 commented May 10, 2026

Copy link
Copy Markdown
Owner

@all-contributors please add @Komzpa for code, test

@allcontributors

Copy link
Copy Markdown
Contributor

@Soju06

@Komzpa already contributed before to code, test

xirothedev added a commit to xirothedev/codex-lb that referenced this pull request May 10, 2026
…l fetch timeouts, image rewrite)

Upstream batch v1.16.0 highlights:
- proxy: expose drain status for graceful deploys (Soju06#564)
- proxy: narrow budget-safe gate to primary usage (Soju06#561)
- proxy: handle model fetch timeouts (Soju06#541)
- proxy: inline-rewrite input_image file references (Soju06#571)
- proxy: retry transient stream timeouts (Soju06#551)
- proxy: use DEFAULT_HOME_DIR for oversized response.create dumps (Soju06#575)
- accounts: split compact quota row + recover quota status (Soju06#562, Soju06#559)
- db: size background pool for burst traffic (Soju06#563)
- upstream: drop top_p for gpt-5 (Soju06#538)

Conflict resolved:
- app/modules/proxy/service.py: kept fork's multi-line import + FailurePhase
  (used by deterministic failover at line 8294).

Merge cleanup:
- app/main.py: removed pre-existing duplicate InFlightMiddleware class +
  duplicate _is_benign_metrics_bind_failure helper that shadowed canonical
  versions and broke new upstream test (test_in_flight_middleware_does_not_count_drain_status).

Verification:
- pytest tests/unit: 1459 passed, 17 failed (all pre-existing on backup
  branch backup/pre-upstream-merge-20260511 — date-bound API key tests,
  test_db_migrate.py SQLite env, settings duplicate validators).
- openspec validate --specs: 19/19 passed.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
vitalNohj pushed a commit to vitalNohj/codex-lb that referenced this pull request Jul 17, 2026
* fix(proxy): expose drain status for graceful deploys

* fix(health): keep drain start loopback-only

* fix(helm): preserve drain deregistration wait
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.

2 participants