Skip to content

perf(orchestrator): add IP index to sandbox map and remove status sta…#2429

Draft
jakubno wants to merge 2 commits intomainfrom
perf/sandbox-ip-index
Draft

perf(orchestrator): add IP index to sandbox map and remove status sta…#2429
jakubno wants to merge 2 commits intomainfrom
perf/sandbox-ip-index

Conversation

@jakubno
Copy link
Copy Markdown
Member

@jakubno jakubno commented Apr 17, 2026

…te machine

Replace the O(n) linear scan in GetByHostPort with an O(1) lookup via a secondary ipIndex map keyed by host IP string.

Remove the SandboxStatus state machine (Starting/Running/Stopping), the status field on Sandbox, and IsRunning(). The map now uses two maps with clear roles: live (running sandboxes for Get/Items/Count) and ipIndex (IP reverse lookups). Remove takes *Sandbox directly through the cleanup closure instead of looking it up by ID.

Rename MarkRunning -> Activate, MarkStopping -> Deactivate.

…te machine

Replace the O(n) linear scan in GetByHostPort with an O(1) lookup
via a secondary ipIndex map keyed by host IP string.

Remove the SandboxStatus state machine (Starting/Running/Stopping),
the status field on Sandbox, and IsRunning(). The map now uses two
maps with clear roles: live (running sandboxes for Get/Items/Count)
and ipIndex (IP reverse lookups). Remove takes *Sandbox directly
through the cleanup closure instead of looking it up by ID.

Rename MarkRunning -> Activate, MarkStopping -> Deactivate.
@cursor
Copy link
Copy Markdown

cursor bot commented Apr 17, 2026

PR Summary

Medium Risk
Medium risk because it changes sandbox lifecycle bookkeeping and map eviction semantics; mistakes could leave sandboxes undiscoverable (or still discoverable) during shutdown/cleanup paths and affect routing by ID/IP.

Overview
This PR replaces the sandbox map’s status-based filtering with two explicit indexes: a live map used for Get/Items/Count and an ipIndex used for O(1) GetByHostPort lookups. It removes the SandboxStatus/IsRunning state machine, renames MarkRunning/MarkStopping to Activate/Deactivate, and changes cleanup to call Remove(ctx, *Sandbox) (with pointer-guarded removal) while ensuring deactivated sandboxes remain IP-resolvable until final removal.

Reviewed by Cursor Bugbot for commit f0c43a6. Bugbot is set up for automated code reviews on this repo. Configure here.

s.OnRemove(ctx, sbx)
})
}
go m.trigger(ctx, func(ctx context.Context, s MapSubscriber) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

OnRemove is fired unconditionally, even when neither RemoveCb actually removed the sandbox. Previously guarded with if removed.

If sandbox creation fails between Insert (adds to ipIndex) and Activate (adds to live), cleanup calls Remove and OnRemove fires without a prior OnInsert. Subscribers maintaining paired insert/remove state receive an unmatched OnRemove.

Both RemoveCb calls return bool - the trigger should be conditional on either returning true.

Copy link
Copy Markdown

@cursor cursor bot left a comment

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit f0c43a6. Configure here.

}
go m.trigger(ctx, func(ctx context.Context, s MapSubscriber) {
s.OnRemove(ctx, sbx)
})
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Remove unconditionally fires OnRemove without checking removal

Medium Severity

Remove unconditionally fires OnRemove subscribers regardless of whether the sandbox was actually found and removed from ipIndex or live. The old code guarded this with if removed { ... }, only notifying subscribers when a removal actually occurred. Now, OnRemove can fire even when both RemoveCb calls are no-ops (pointer identity mismatch or sandbox not present), and can fire without a matching OnInsert (e.g., creation failed before Activate). This breaks the lifecycle contract for MapSubscriber.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit f0c43a6. Configure here.

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