Skip to content

fix(harness): drain session mirrors on close - #2149

Open
richuV wants to merge 2 commits into
agentscope-ai:mainfrom
richuV:codex/session-tree-close-barrier-2147
Open

richuV wants to merge 2 commits into
agentscope-ai:mainfrom
richuV:codex/session-tree-close-barrier-2147

Conversation

@richuV

@richuV richuV commented Jul 12, 2026

Copy link
Copy Markdown

Summary:

  • add a deterministic FIFO barrier for SessionTree mirror work submitted before shutdown
  • drain pending mirrors before HarnessAgent closes its workspace index and delegate
  • replace timing-based mirror waits and cover an in-flight blocking upload

Fixes #2147

Verification: 41 focused Harness/session/memory tests passed; Spotless and git diff check passed.

@richuV
richuV requested a review from a team July 12, 2026 01:16
@CLAassistant

CLAassistant commented Jul 12, 2026 •

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you all sign our Contributor License Agreement before we can accept your contribution.
1 out of 2 committers have signed the CLA.

✅ richuV
❌ u010143241


u010143241 seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You have signed the CLA already but the status is still pending? Let us recheck it.

@codecov

codecov Bot commented Jul 12, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@AgentScopeJavaBot AgentScopeJavaBot 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.

🤖 AI Review

Adds deterministic FIFO barrier (awaitPendingMirrors) to SessionTree for draining pending async mirror operations before HarnessAgent.close(). Correct shutdown ordering, proper interrupt handling, good test coverage replacing unreliable Thread.sleep waits. No issues found.

@AgentScopeJavaBot AgentScopeJavaBot added bug Something isn't working area/harness agentscope-harness (test/runtime support) labels Jul 12, 2026
@oss-maintainer

This comment was marked as abuse.

@oss-maintainer

Copy link
Copy Markdown
Collaborator

⚠️ Merge conflict detected

This PR currently conflicts with main and cannot be merged. Please rebase (or merge main in) and resolve the conflicts:

git fetch origin
git rebase origin/main
# resolve conflicts, then:
git push --force-with-lease

This is a one-time reminder. Feel free to @mention me for a re-review after conflicts are resolved.


Automated notification by github-manager-bot

@oss-maintainer oss-maintainer 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.

Summary

Closing the race between fire-and-forget mirror writes and resource teardown in HarnessAgent.close() is the right goal, and the mockito-based tests that block the mirror executor and assert the drain really waits are a nice touch.

However this work has largely landed on main already, in a bounded form: SessionTree.awaitMirrorQuiescence(long, TimeUnit) is called from HarnessAgent.close() with a 5s timeout (alongside MemoryBackgroundTasks.awaitQuiescence). The PR is also CONFLICTING against main, so a rebase is required either way. Please rebase and reconcile with the existing helper rather than introducing a second, unbounded drain API.

Findings

  • [Warning] SessionTree.java:99 — duplicates awaitMirrorQuiescence(...) already wired into close() on main.
  • [Warning] SessionTree.java:106 — unbounded barrier.await() on the static shared executor can wedge every agent's close() behind one stuck remote write.
  • [Info] HarnessAgent.java:382 — unguarded call lets RejectedExecutionException escape close().

Suggestions

// instead of a second API:
if (!SessionTree.awaitMirrorQuiescence(mirrorDrainTimeout, MIRROR_DRAIN_UNIT)) {
    log.warn("Session mirror drain did not complete before close(); continuing shutdown");
}

Notes

CLA status for the current head: pending. First contribution — thanks for the care in the tests. Please rebase onto main (conflict reminder was already posted) and re-request review.


Automated review by @BOT_SIG@

* The wait is deliberately uninterruptible so that resource shutdown cannot race an in-flight
* upload; the interrupted status is restored before returning.
*/
public static void awaitPendingMirrors() {

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.

[Warning] main already has this exact drain: SessionTree.awaitMirrorQuiescence(long, TimeUnit) (same single-thread executor, submit-and-wait), and HarnessAgent.close() already calls it with a 5s bound plus MemoryBackgroundTasks.awaitQuiescence(5s). Adding a second, unbounded public variant means two overlapping shutdown APIs that will drift. Could you rebase and either drop awaitPendingMirrors() in favour of the existing bounded helper, or state what the unbounded wait buys that the timeout does not (the close()-vs-tempdir race the existing helper documents looks like the same one this PR targets)?

try {
CountDownLatch barrier = new CountDownLatch(1);
MIRROR_EXECUTOR.execute(barrier::countDown);
barrier.await();

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.

[Warning] No bound on barrier.await(). MIRROR_EXECUTOR is static, shared by every SessionTree in the JVM, and each queued task performs remote I/O (store.appendSegment(...), mirrorToFilesystem(...)). A single hung upload therefore blocks close() of every agent — including sessions that have nothing pending — with no way out except a JVM kill. The bounded form used on main (submit(() -> {}).get(timeout, unit)) keeps shutdown latency finite and logs when it is not met. If an unbounded wait is intentional here, please make the timeout configurable and log a warning when the drain exceeds it.

if (ownedWorkspaceIndex != null) {
ownedWorkspaceIndex.close();
}
SessionTree.awaitPendingMirrors();

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.

[Info] The new call sits outside any try/catch, so a RejectedExecutionException from the executor (already shut down during JVM/teardown ordering) now escapes close(). The finally nesting does still close ownedWorkspaceIndex and delegate, but callers that treated close() as non-throwing (e.g. try-with-resources in tests) will start seeing exceptions on shutdown. awaitMirrorQuiescence swallows and logs, which keeps close() total — worth matching.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/harness agentscope-harness (test/runtime support) bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Harness close can return before SessionTree mirror finishes using distributed store

4 participants