Skip to content

fix(server): stop waiting for a Kubernetes sandbox deleted during creation - #3118

Merged
Pangjiping merged 2 commits into
opensandbox-group:mainfrom
kongxinjun:fix/k8s-create-wait-stops-on-delete
Nov 26, 2026
Merged

Pangjiping merged 2 commits into
opensandbox-group:mainfrom
kongxinjun:fix/k8s-create-wait-stops-on-delete

Conversation

@kongxinjun

Copy link
Copy Markdown
Contributor

Summary

  • _wait_for_sandbox_ready fails fast on Running, Failed, Terminated, quota and unschedulable, but not on deletion. A BatchSandbox that is being deleted reports Stopping (deletionTimestamp set), and once it is gone get_workload returns nothing, which the loop treats as "not created yet". A sandbox deleted (by the user, or by TTL) while its POST /sandboxes was still waiting kept that request open until POD_READY_TIMEOUT and then answered 504 KUBERNETES::POD_READY_TIMEOUT ("Timeout waiting ... Last state: Stopping").
  • The loop now stops with the same 409 it uses for a sandbox that reaches Terminated before it is ready, when the workload is Stopping or disappears after it was seen. A workload that has not appeared yet is still polled as before.

On a kind cluster (BatchSandbox provider, image that cannot be pulled so the sandbox stays Pending, DELETE 1 s after the create):

create response returned after
before 504 KUBERNETES::POD_READY_TIMEOUT ... Last state: Stopping 60.0 s
after 409 "Sandbox ... was deleted before it was ready (Sandbox is being deleted)" 1.1 s

Testing

  • Not run (explain why)

  • Unit tests

  • Integration tests

  • e2e / manual verification

  • tests/k8s/test_kubernetes_service.py: test_wait_stops_when_sandbox_is_being_deleted and test_wait_stops_when_seen_workload_disappears fail on main (the loop runs to its timeout) and pass with the change; test_wait_keeps_polling_until_workload_first_appears pins the unchanged behaviour for a workload that is not created yet.

  • uv run pytest: 3197 passed, 34 skipped. uv run ruff check: clean. pyright on kubernetes_service.py: same 6 pre-existing errors as main.

  • Manual: kind v1.37 cluster, controller from kubernetes/cmd/controller, server with the BatchSandbox provider; numbers in the table above.

Breaking Changes

  • None
  • Yes (describe impact and migration path)

A create whose sandbox is deleted while it waits now returns 409 immediately instead of 504 after the ready timeout.

Checklist

  • Linked Issue or clearly described motivation
  • Added/updated docs (if needed)
  • Added/updated tests (if needed)
  • Security impact considered
  • Backward compatibility considered

…ation

The create wait loop treated Stopping (deletionTimestamp set) as a
non-terminal state and kept polling a workload that had disappeared, so a
sandbox deleted while its create request was waiting held that request until
POD_READY_TIMEOUT and then answered 504 "Timeout waiting ... Last state:
Stopping".

Fail with 409, as for a sandbox that reaches Terminated first, when the
workload is Stopping or vanishes after it was seen.

Signed-off-by: kongxin <kongxin326@gmail.com>
@github-actions github-actions Bot added component/server size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Nov 26, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 26, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-26T01:38:42.782559Z 2d6d965 PR opened
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@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: 2d6d965136

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment on lines +504 to +506
if last_workload is not None:
# It existed and is gone: deleted while we waited.
raise _deleted_before_ready_error(sandbox_id)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Seed deletion detection from the successful create

When DELETE or TTL removal completes after create_workload returns but before _wait_for_sandbox_ready completes its first successful GET (the caller also attaches PVC owner references before invoking this method), last_workload is still None, so this branch treats every subsequent 404 as “not found yet” and still waits until returning the timeout 504. Pass or seed the already-returned workload_info/known-created state so this immediate-deletion race receives the intended 409 as well.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good catch, this was real. If the DELETE or TTL removal landed between create_workload returning and the first GET, last_workload stayed None, so every 404 counted as "not visible yet" and the request still ran to the 504 (Last state: None).

Fixed in f4e6e6d: _wait_for_sandbox_ready now takes workload_created, and create_sandbox passes True, so the first 404 after a successful create fails with the same 409. This is safe because a cache miss in get_custom_object falls through to a live API GET, so that 404 comes from the API server and not from informer lag. The flag defaults to False, so other callers still poll until the workload first appears.

Test: test_create_sandbox_fails_fast_when_deleted_before_first_read drives create_sandbox with every GET returning 404 and expects a 409 after one read. Before the fix it failed with 504 == 409. There is also a unit test on the wait itself. The full server suite passes (3199 passed, 34 skipped).

…wait

The wait loop only reported "deleted before it was ready" once a GET had
returned the workload. A DELETE or TTL removal that landed between
create_workload returning and the first GET left every read a 404, which
the loop took as "not visible yet" and polled until POD_READY_TIMEOUT
(504, Last state: None).

create_sandbox now tells the wait that the workload already exists, so
the first 404 fails with the same 409. A cache miss falls through to a
live API GET, so that 404 is authoritative rather than informer lag.

Signed-off-by: kongxin <kongxin326@gmail.com>

@Pangjiping Pangjiping left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM

@Pangjiping
Pangjiping merged commit 052765a into opensandbox-group:main Nov 26, 2026
63 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

component/server size/S Denotes a PR that changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants