Skip to content

[runtime][java] Fix async shutdown ordering and stabilize scheduling tests - #1448

Merged
wenjin272 merged 1 commit into
apache:mainfrom
wenjin272:codex/async-test-cleanup
Nov 25, 2026
Merged

wenjin272 merged 1 commit into
apache:mainfrom
wenjin272:codex/async-test-cleanup

Conversation

@wenjin272

@wenjin272 wenjin272 commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Linked issue: Fixes #1447

Purpose of change

Outcome

Stop queued async calls from starting when cancellation frees a worker during executor shutdown. Stabilize the Java async tests while retaining recovery, cancellation, and late-subgraph-event coverage. The only production change is executor shutdown ordering; the gather scheduler is unchanged.

Runtime flow

close()
  -> stop the worker pool from taking queued work
  -> cancel tracked submissions
  -> wait for workers and their cleanup to finish

Key decisions

  • Stop the pool before cancelling submissions. Cancelling a running Future first can free a worker while queued tasks are still eligible to run.
  • Start the required calls and confirm their state before invoking timed gather in the timeout tests. These cases exercise the existing scheduler's real deadlines. The operator test likewise starts the child before timing its wait.
  • Check lifecycle notification ordering and pairing without requiring a fixed number of handoffs. Remove minimum remote-poll counts; reconciliation accepts an existing task that has already completed.

Behavioral Semantics

Interaction decisions

Condition Handling
Worker is occupied and another call is queued during shutdown Stop the pool before cancellation can free the worker; cancel the queued submission.
Callable reacts to the shutdown interrupt before its Future is cancelled Exceptional completion is valid; closing still waits for worker cleanup.
Call completes before timeout cancellation wins Keep its actual outcome.
Durable call has not started when the batch times out Return a timeout outcome and leave its durable slot pending for replay.
Started subgraph emits events after its caller times out Existing cancellation handling discards late events and retires the scheduled actions.

Behavioral contracts

Shutdown stops queued work from being picked up as a consequence of cancelling running submissions, and close() waits for worker exit cleanup. Batch timeout handling preserves available outcomes and leaves unstarted durable calls replayable. The remaining changes adjust test synchronization and assertions without changing these contracts.

Failure behavior

Running work is interrupted during shutdown; its Future may be cancelled or complete exceptionally. Unfinished batch calls receive timeout outcomes. Ordinary call failures retain the existing result-recording behavior, while fatal/framework failures continue to propagate. No new retry or fallback behavior is introduced.

Tests

Contract Coverage
Shutdown cancels queued work and waits for cleanup AsyncBatchExecutorTest.closeCancelsSubmissionsAndWaitsForManagedThreadCleanup
Completed results survive a batch timeout JavaRunnerContextImplDurableExecuteAsyncTest.testGatherTimeoutKeepsCompletedOutcomes; MixedFutureGatherTest completion-versus-cancellation cases
Unstarted durable calls remain replayable Durable async tests for unsubmitted and queued calls, including the real fallback executor
Late subgraph events do not fail the operator SubgraphTimeoutTest, for chat and internal subagents
Recovery restores lifecycle notifications in valid order TaskLifecycleListenerNotificationTest
External subagent results and idempotent reconciliation remain correct ExternalAgentSubagentSetupTest against the local HTTP stub

The six affected test classes passed after clean rebuilds:

JDK Passed Conditionally skipped
11 86 16
17 86 16
21 104 4

Spotless and git diff --check passed. Full-reactor tests, Python/cross-language suites, and a live external agent service were not rerun.

Test synchronization details
  • The lifecycle suspension fixture releases its callable from the transfer notification; restore assertions allow varying numbers of paired transfer/prepared notifications.
  • Timeout fixtures prepare calls and start the selected suppliers before timed gather. Test wrappers return those existing completion signals when gather calls start(), avoiding duplicate submission. Latches establish which suppliers are running; remaining calls are either queued in the real pool or left unsubmitted. Gather then reaches its real deadline.
  • The late-event operator fixture starts the graph and waits for the child to enter before invoking timed gather. The child remains blocked until the caller has returned from gather.
  • Shutdown assertions accept interruption winning the race with explicit Future cancellation, while still checking that queued callable bodies do not execute.

API

No public API, configuration, or persisted-state format changes. Implementation and test changes are confined to Java; Python and YAML APIs are unchanged.

Documentation

  • doc-needed
  • doc-not-needed
  • doc-included

Was this patch authored or co-authored using generative AI tooling?

  • Yes
  • No

Generated-by: Codex CLI 0.153.4 (GPT-6; exact model version unavailable)

@wenjin272 wenjin272 added priority/major Default priority of the PR or issue. doc-not-needed Your PR changes do not impact docs java Pull requests that update java code fixVersion/1.0.0 The feature or bug should be implemented/fixed in the 1.0.0 version. labels Nov 25, 2026
@github-actions github-actions Bot added doc-not-needed Your PR changes do not impact docs and removed doc-not-needed Your PR changes do not impact docs labels Nov 25, 2026
@wenjin272
wenjin272 force-pushed the codex/async-test-cleanup branch from 2de6665 to 41ad17b Compare November 25, 2026 02:54
@github-actions github-actions Bot added doc-not-needed Your PR changes do not impact docs and removed doc-not-needed Your PR changes do not impact docs labels Nov 25, 2026
assertThat(runningInterrupted.getCount()).isZero();
// shutdownNow may interrupt the callable before its result is explicitly cancelled.
assertThat(running.isCompletedExceptionally()).isTrue();
assertThat(queued.isCancelled()).isTrue();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Would it make sense to move the queued.isCancelled() and queuedCalls assertions just after closing.get(...)? I checked this locally by adding a latch immediately after shutdownNow() to pause the thread running close(). Once the worker signalled cleanupStarted, queued.isCancelled() was still false. After I released the latch and close() returned, it was true.

Checking those two assertions after closing.get(...) would avoid depending on which thread gets scheduled first. The closing.isDone() check can stay before releaseCleanup.countDown() to verify that close() waits for cleanup.

…tests

Stop the worker pool before cancelling tracked submissions. Stabilize tests
by establishing call state before timed gather, checking lifecycle pairing,
and removing fixed polling-count assumptions. Keep the gather scheduler
unchanged.

Generated-by: Codex CLI 0.153.4 (GPT-6; exact model version unavailable)

Co-authored-by: Codex <codex@openai.com>
@wenjin272
wenjin272 force-pushed the codex/async-test-cleanup branch from 41ad17b to 8ea2484 Compare November 25, 2026 05:39

@eemario eemario left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for the update! Moving the queued-task assertions after closing.get(...) addresses my concern. The test still verifies that close() waits for worker cleanup. LGTM.

@wenjin272
wenjin272 merged commit 555cf39 into apache:main Nov 25, 2026
55 of 56 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

doc-not-needed Your PR changes do not impact docs fixVersion/1.0.0 The feature or bug should be implemented/fixed in the 1.0.0 version. java Pull requests that update java code priority/major Default priority of the PR or issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Queued async calls can execute during executor shutdown

2 participants