Skip to content

fix(blaze): reclaim inactive operation locks - #2291

Draft
WeissonHan wants to merge 1 commit into
alibaba:mainfrom
WeissonHan:fix/blaze/reclaim-operation-locks
Draft

fix(blaze): reclaim inactive operation locks#2291
WeissonHan wants to merge 1 commit into
alibaba:mainfrom
WeissonHan:fix/blaze/reclaim-operation-locks

Conversation

@WeissonHan

@WeissonHan WeissonHan commented Aug 6, 2026

Copy link
Copy Markdown
Collaborator

Why

The sandbox manager kept a strong operation-lock reference for every UUID it
had seen. Even after all holders and waiters finished, the registry retained
the lock object, so a long-running daemon accumulated historical entries.

Before this change, the table grew with every distinct sandbox ID and startup
preallocated permanent locks for persisted sandboxes. After this change, the
registry retains one shared lock only while a holder or waiter still owns it,
then reclaims expired entries through bounded work on later lookups.

What changed

  • Store weak references in the per-sandbox operation-lock registry.
  • Maintain a cleanup queue and inspect at most four candidates per lookup.
  • Stop preallocating permanent locks for persisted sandboxes at startup.
  • Preserve one shared lock for concurrent requests using the same UUID.
  • Add focused reclamation, bounded-cleanup, and 32-thread same-ID sharing tests.

Commit

  1. d13cbf664349reclaim inactive operation locks. Replaces permanent
    strong registry ownership with weak references, adds fixed-batch cleanup,
    removes startup preallocation, and defines the reclamation and same-ID
    serialization invariants with focused tests.

These changes belong in one PR because the weak-reference representation,
bounded cleanup rule, startup behavior, and concurrent-sharing tests jointly
correct one in-memory ownership invariant. The PR does not change sandbox
state, runtime ownership, storage behavior, or request semantics.

Still to do

  1. None for the issue scope. Expired entries are intentionally reclaimed by
    later lock lookups instead of requiring a background task or synchronous
    cleanup when the final holder exits.

Related issue

closes #2289

User / Agent impact

No public API behavior changes. A long-running daemon can release inactive
per-sandbox lock objects while preserving serialization between concurrent
operations for the same sandbox.

Risk and compatibility

  • Public CLI, API, configuration, or documented behavior changed
  • Privileged or security-sensitive behavior changed
  • Cross-component contract changed
  • Migration or rollback guidance is needed

Low risk. The change is limited to the manager's in-memory operation-lock
registry.

Validation

Exact commit: d13cbf6643497242d240b086d7dcf1553f9ef1dd

Parent: 717e63161d02967749c715a1ee6e7fc12c946ab5

Tree: 1208e998fd3f59cccfd70824a0e67f3ddc9fb61e

The exact commit was exported with git archive; the verified archive SHA-256
is:

596cbdb39ee08a9c727b76749ee90f701aff08f63016ce739340b4203ce05d21

Fresh Linux x86_64 source and separate initially empty default/all-feature
targets passed:

  • cargo fmt --all -- --check
  • default and all-feature locked workspace all-target builds
  • default and all-feature strict workspace Clippy
  • cargo test --workspace --locked — 52 core + 206 daemon tests
  • cargo test --workspace --all-features --locked — 52 core + 216 daemon
    tests
  • default and all-feature strict rustdoc
  • cargo test -p blazed --locked operation_lock -- --nocapture — 4/4
  • commitlint 19.8.1, trailer parsing, parent-to-head git diff --check, and
    public-boundary scan

All listed tests completed with zero failures. Hosted Components, PR Lint, and CLA checks passed. Complete-PR review also passed without findings for d13cbf6643: review request and review result.

Documentation and rollback

No documentation changes are required because the public contract and
persisted data are unchanged. Reverting the single commit restores permanent
strong registry ownership without requiring data conversion.

Copy link
Copy Markdown
Collaborator Author

@codex review Please review exact head 59bdb10c4dbf2198a7d5c891331ac4d2742487ae. This PR lets inactive per-sandbox operation locks be reclaimed while concurrent requests for the same UUID continue to share one lock.

@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: 59bdb10c4d

ℹ️ 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".

Ok(locks) => locks,
Err(poisoned) => poisoned.into_inner(),
};
locks.retain(|_, lock| lock.strong_count() > 0);

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 Avoid scanning every live lock on each acquisition

When operations for many different sandbox UUIDs overlap, every acquisition scans all currently live entries while holding the global synchronous registry mutex. A burst reaching N active sandboxes therefore performs Θ(N²) weak-count checks and serializes async request workers behind progressively longer scans, regressing the previous O(1) lookup path; prune dead entries periodically or amortize cleanup instead of retaining the entire map on every request.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Resolved in the current PR #2291 head b33b23be97676307d4d9446b7e48f831a06c5e3e.

The registry now keeps a cleanup queue and examines at most four candidates per lock lookup. Live entries are rotated to the back and expired weak entries are removed, so acquisition no longer scans the complete live map while requests for the same UUID still upgrade the same lock.

The new operation_lock_pruning_is_bounded regression verifies the fixed work budget, alongside the reclamation and 32-thread same-ID sharing tests. The exact head passed the Linux default/all-feature build, strict Clippy, workspace tests (52+110 / 52+120), strict rustdoc, and all 3 focused operation-lock tests.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Resolved in d13cbf664349.

The published revision retains the fixed-batch cleanup queue: each operation-lock lookup examines at most four queued candidates while holding the registry mutex. Live entries are rotated to the back, expired weak entries are removed, and concurrent requests for the same UUID still upgrade the same shared lock. It therefore avoids the full live-table scan identified in this review.

Regression coverage: operation_lock_pruning_is_bounded proves the fixed cleanup budget; the reclamation and 32-thread same-ID tests cover inactive-entry cleanup and serialization. The exact commit passed the Linux default/all-feature build, strict Clippy, workspace tests (52+206 / 52+216), strict rustdoc, and the focused operation_lock filter (4/4).

Remaining limitation: expired entries are reclaimed opportunistically by later lookups rather than immediately when the final holder exits; this is intentional and does not weaken the per-sandbox serialization guarantee.

@WeissonHan
WeissonHan force-pushed the fix/blaze/reclaim-operation-locks branch from 59bdb10 to b33b23b Compare August 6, 2026 20:28

Copy link
Copy Markdown
Collaborator Author

@codex review Please review exact head b33b23be97676307d4d9446b7e48f831a06c5e3e. This revision replaces full-table operation-lock pruning with a fixed-batch cleanup queue while preserving same-UUID serialization.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Bravo.

Reviewed commit: b33b23be97

ℹ️ 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".

The per-sandbox lock registry keeps one Arc for every UUID it has seen.
A long-running daemon therefore retains lock objects after operations finish.

Store weak references and inspect a fixed batch of registry entries during
each lookup. Inactive locks are reclaimed without scanning the complete live
registry on every acquisition. Concurrent requests for one UUID still upgrade
the same lock, preserving per-sandbox serialization.

This only changes the in-memory operation-lock lifecycle. Sandbox state,
runtime ownership, and request behavior remain unchanged.

Fixes: b66e714 ("feat(blaze): manage recoverable sandboxes")
Signed-off-by: Weisson Han <wenshu.hx@linux.alibaba.com>
@WeissonHan
WeissonHan force-pushed the fix/blaze/reclaim-operation-locks branch from b33b23b to d13cbf6 Compare August 10, 2026 09:06

Copy link
Copy Markdown
Collaborator Author

@codex review This PR reclaims inactive per-sandbox operation locks with bounded cleanup while preserving same-sandbox operation serialization. Please review the complete pull request: every commit, the cumulative diff against the base branch, and the implementation, tests, and documentation as one submitted change.

@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: d13cbf6643

ℹ️ 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".

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.

[blaze] fix: reclaim inactive operation locks

1 participant