adk-python icon indicating copy to clipboard operation
adk-python copied to clipboard

ModelArmorPlugin.close() cancels in-flight screening on sibling runners sharing the plugin

Open rladeira opened this issue 3 weeks ago • 3 comments

Describe the bug

When multiple Runner instances share the same App (and therefore the same ModelArmorPlugin instance) on the same asyncio event loop—such as during adk eval (LocalEvalService.perform_inference running concurrent cases up to parallelism=4 via EvaluationGenerator), or in a web server handling concurrent requests—closing one runner tears down the gRPC channel for all runners on that loop.

In-flight screening RPCs (sanitize_user_prompt, sanitize_model_response, or tool output screening) on sibling runners fail with asyncio.CancelledError. Because CancelledError inherits from BaseException rather than Exception, it bypasses ModelArmorPlugin's except Exception: screening failure handling (neither fail-closed nor alerting is triggered). The cancellation bubbles up to the runner task, where Runner._cleanup_root_task swallows it as an external cancellation (Root node ... was cancelled by an external cancellation.), silently truncating the turn with final_response=None.

To Reproduce

  1. Configure an App with a root agent and ModelArmorPlugin.
  2. Launch two Runner(app=app) instances on the same event loop.
  3. Start an invocation on the first runner so that an input or output screening call is in flight.
  4. Close the second runner (await runner2.close()).
  5. The first runner's in-flight screening aborts with asyncio.CancelledError, logging Root node ... was cancelled by an external cancellation., and the turn produces no output events.

Expected behavior

Closing a runner should not close the shared transport while other runners or subsequent invocations on that event loop are active.

Additional context

Since commit c9d2c48b (#7644), ModelArmorPlugin builds clients per event loop via per_loop_value. In ADK architecture, per-loop clients (e.g. GoogleLlm.api_client, VertexAiSessionService, VertexAiMemoryBankService) are scoped to the event loop and cleaned up when the loop itself is closed or garbage collected, rather than on individual runner teardown.

rladeira avatar Nov 09 '26 20:11 rladeira

@rladeira Reproduced it on 2.14.1 with two runners sharing one App: closing the second runner cancels the first one’s screening call, and the turn just ends with the “external cancellation” log and no output.

One workaround I tried: keep close() popping the per-loop client, but only close its transport after any running screening calls on it finish (a small counter around _sanitize_user_prompt/_sanitize_model_response). The turn then finished normally and the channel still got closed.

Could you check whether that, or your approach in #8068, holds up in your adk eval runs with parallelism > 1? Also, which ADK and Python versions are you on?

Please test it thoroughly with a real gRPC channel before we move the PR forward. It may also be worth failing closed on CancelledError so a dropped screening call doesn’t pass silently.

surajksharma07 avatar Nov 13 '26 06:11 surajksharma07

Thanks for reproducing this, @surajksharma07!

Environment: Python 3.14.10, google-adk 2.14.0 (and main with #8068), google-cloud-modelarmor 0.7.1, grpcio 1.83.0.

Live gRPC verification:

  • 12 concurrent InMemoryRunners sharing one App against a live regional Model Armor endpoint: on 2.14.0, 10/12 runs aborted with Root node ... was cancelled by an external cancellation.; with #8068, 12/12 completed reusing a single grpc.aio channel.
  • adk eval --parallelism 4 (12 multi-turn cases, 22 turns with live prompt + response screening): 22/22 turns completed with 0 cancellations.

In-flight counter vs. #8068: If close() closes the client whenever in_flight == 0, a runner finishing while a sibling runner is between before_model_callback and after_model_callback (waiting on the LLM or a tool) still tears down the loop's channel, forcing repeated grpc.aio channel creation and TLS handshakes across sibling turns and subsequent eval cases. Scoping the client to the event loop matches VertexAiSessionService and VertexAiMemoryBankService and avoids both cancellation and channel churn.

On CancelledError: Catching CancelledError (BaseException) inside screening callbacks would swallow genuine cooperative asyncio task cancellations (such as request timeouts or client disconnects). Once close() stops tearing down the shared channel, CancelledError only occurs when the caller actually cancels the task.

rladeira avatar Nov 13 '26 12:11 rladeira

Thanks @rladeira, the live numbers settle it. You’re right that the in-flight counter would still tear down the channel between a sibling’s before and after callbacks, and that catching CancelledError would swallow real cancellations, so I’m dropping both suggestions. Scoping the client to the loop, like the Vertex session and memory clients, is the cleaner fix.

Merged 2c51b77 onto current main (761538a) and it goes in clean. The model_armor tests pass with -W error::ResourceWarning, and my two-runner repro now gets ['Hello!'] where main loses the turn. Two sync Runner.run() calls still build a fresh client each, so #7644 holds. Three runners in a row on one loop now share one client instead of building and closing three.

On @mangeshraut712’s point about deterministic teardown on a long-lived loop, I agree it belongs in _event_loop_cache for all per-loop clients rather than in this plugin. Please do open that follow-up issue and link it here.

Nothing else needed from you. #8068 is ready for a maintainer to approve the workflows and review.

surajksharma07 avatar Nov 17 '26 20:11 surajksharma07