-
Notifications
You must be signed in to change notification settings - Fork 43
fix: move sandbox rollback defer before timeout to prevent resource leak #258
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
volcano-sh-bot
merged 1 commit into
volcano-sh:main
from
Sanchit2662:fix/sandbox-creation-timeout-resource-leak
Apr 8, 2026
+21
−14
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -160,16 +160,9 @@ func (s *Server) createSandbox(ctx context.Context, dynamicClient dynamic.Interf | |
| } | ||
| } | ||
|
|
||
| var createdSandbox *sandboxv1alpha1.Sandbox | ||
| select { | ||
| case result := <-resultChan: | ||
| createdSandbox = result.Sandbox | ||
| klog.V(2).Infof("sandbox %s/%s running", createdSandbox.Namespace, createdSandbox.Name) | ||
| case <-time.After(2 * time.Minute): // consistent with router settings | ||
| klog.Warningf("sandbox %s/%s create timed out", sandbox.Namespace, sandbox.Name) | ||
| return nil, fmt.Errorf("sandbox creation timed out") | ||
| } | ||
|
|
||
| // Register rollback BEFORE waiting for the sandbox to become ready. | ||
| // This ensures the K8s resource and store placeholder are cleaned up on | ||
| // timeout, pod-IP failure, or store-update failure — not just on post-creation errors. | ||
| needRollbackSandbox := true | ||
| sandboxRollbackFunc := func() { | ||
| ctxTimeout, cancel := context.WithTimeout(context.Background(), 30*time.Second) | ||
|
|
@@ -180,17 +173,21 @@ func (s *Server) createSandbox(ctx context.Context, dynamicClient dynamic.Interf | |
| err = deleteSandboxClaim(ctxTimeout, dynamicClient, sandboxClaim.Namespace, sandboxClaim.Name) | ||
| if err != nil { | ||
| klog.Infof("sandbox claim %s/%s rollback failed: %v", sandboxClaim.Namespace, sandboxClaim.Name, err) | ||
| return | ||
| } else { | ||
| klog.Infof("sandbox claim %s/%s rollback succeeded", sandboxClaim.Namespace, sandboxClaim.Name) | ||
| } | ||
| klog.Infof("sandbox claim %s/%s rollback succeeded", sandboxClaim.Namespace, sandboxClaim.Name) | ||
| } else { | ||
| // Rollback Sandbox | ||
| err = deleteSandbox(ctxTimeout, dynamicClient, sandbox.Namespace, sandbox.Name) | ||
| if err != nil { | ||
| klog.Infof("sandbox %s/%s rollback failed: %v", sandbox.Namespace, sandbox.Name, err) | ||
| return | ||
| } else { | ||
| klog.Infof("sandbox %s/%s rollback succeeded", sandbox.Namespace, sandbox.Name) | ||
| } | ||
| klog.Infof("sandbox %s/%s rollback succeeded", sandbox.Namespace, sandbox.Name) | ||
| } | ||
| // Clean up the store placeholder so it does not pollute GC queries | ||
| if delErr := s.storeClient.DeleteSandboxBySessionID(ctxTimeout, sandboxEntry.SessionID); delErr != nil { | ||
|
||
| klog.Infof("sandbox %s/%s store placeholder cleanup failed: %v", sandbox.Namespace, sandbox.Name, delErr) | ||
| } | ||
| } | ||
| defer func() { | ||
|
|
@@ -200,6 +197,16 @@ func (s *Server) createSandbox(ctx context.Context, dynamicClient dynamic.Interf | |
| sandboxRollbackFunc() | ||
| }() | ||
|
|
||
| var createdSandbox *sandboxv1alpha1.Sandbox | ||
| select { | ||
| case result := <-resultChan: | ||
| createdSandbox = result.Sandbox | ||
| klog.V(2).Infof("sandbox %s/%s running", createdSandbox.Namespace, createdSandbox.Name) | ||
| case <-time.After(2 * time.Minute): // consistent with router settings | ||
| klog.Warningf("sandbox %s/%s create timed out", sandbox.Namespace, sandbox.Name) | ||
| return nil, fmt.Errorf("sandbox creation timed out") | ||
| } | ||
|
|
||
| // agent-sandbox create pod with same name as sandbox if no warmpool is used | ||
| // so here we try to get pod IP by sandbox name first | ||
| // if warmpool is used, the pod name is stored in sandbox's annotation `agents.x-k8s.io/sandbox-pod-name` | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The rollback registration is still performed after the Kubernetes resource creation calls (
createSandboxClaimorcreateSandbox). If these calls fail, the function returns early before thedeferis registered, which means the store placeholder created at line 147 will not be cleaned up. To ensure all resources are properly rolled back on any failure, move thesandboxRollbackFuncdefinition and itsdeferregistration to immediately follow the successfulStoreSandboxcall (around line 151).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This makes sense in general. But there is gc that will reclaim the expired sandbox, it is no harm.