EnvironmentScope changes which initialize calls wait for each other. Who still waits behind a slow sandbox start, what close holds back, and why the lock stops at the process boundary.
The change
ADK Python 2.15.0, published on 19 November 2026, adds EnvironmentScope to its execution environments. An environment, the object behind ADK's environment and skill toolsets that runs commands or code for an agent, can now say which agent runs share one of its resources: SHARED, the default and the previous behaviour, USER or SESSION. The toolsets use that to decide which initialize calls wait for each other. The release notes summarise the intent as isolating sandboxes per user or session "without making other concurrent sessions wait during sandbox startup". The commit, 691ecbe, dated 16 November 2026, also states plainly that no built-in environment changes its scope; the feature is for environments you write.
This guide explains what the lock key is under each scope, who still waits behind a slow sandbox start, what a close does to calls in flight, and what the scope cannot protect. A companion lab simulates the same timeline with controls for scope, timings, a close request and a second process.
Why initialize needs a lock at all
The toolsets call the environment's initialize before every tool call, not just the first, and pass the calling run's context. That lets an environment that keeps a sandbox per session look up the session's sandbox, for example through ctx.state, and create it when it is missing. Once it exists, initialize should return quickly.
Without a lock, two concurrent tool calls in the same session could both find the sandbox missing and both create one. So the toolsets serialise initialize calls. Before 2.15.0 they serialised all calls for an environment, which is safe and, for an environment with one sandbox per session, slow: while Jenya's session creates its sandbox, Geetha's tool call in a different session waits for no reason.
The 2.15.0 code keys the lock by scope. For SHARED the key is empty, so every call shares one lock. For USER it is the app name and user id. For SESSION it is app name, user id and session id. Calls with different keys run at the same time; calls with the same key run one at a time.
The worked timeline
The companion model runs five tool calls in one process. A cold initialize, one that creates a sandbox, takes 8 seconds; a warm one takes 0.1 seconds. The environment keeps one sandbox per session.
- c1 at 0.0 s: Jenya, session s1
- c2 at 0.5 s: Geetha, session s2
- c3 at 1.0 s: Jenya again, session s1
- c4 at 1.5 s: Tanishka, session s3
- c5 at 2.0 s: Geetha, a second session s4
The time each call waits before its initialize starts:
scope c1 c2 c3 c4 c5
SHARED 0.0 7.5 15.0 14.6 22.1
USER 0.0 0.0 7.0 0.0 6.5
SESSION 0.0 0.0 7.0 0.0 0.0Under SHARED, every call queues behind every cold start. Tanishka's first call waits 14.6 seconds because two other sessions created sandboxes before hers, and Geetha's second session waits 22.1 seconds.
Under USER, different users proceed in parallel, but Geetha's second session still waits 6.5 seconds behind her first, because both sessions share her user key.
Under SESSION, the only call that waits is c3, Jenya's second call in the session whose sandbox is still being created. That wait is the one the lock exists for: c3 then runs a warm initialize and finds the sandbox ready.
Choose the scope from the resource
The scope must match the granularity of the resource, not the latency you want. The guide in the release says it directly: an environment that keeps a resource per session or user says so through its scope.
The model shows what happens when the two disagree. Keep the sandbox per user, but declare SESSION scope, and Geetha's two sessions run initialize concurrently. Both find her sandbox missing, and the model records a duplicate creation for c5. The lock was narrower than the resource, so it did not protect it. Declare USER for a per-user resource and the duplicate disappears.
The documentation adds two obligations for an environment that narrows its scope. Calls for different users or sessions may now run concurrently, "possibly on different threads", so anything the environment shares internally, such as an API client or a cache of sandbox handles, needs its own lock. And the scope must not change during the environment's life. A wrapper around another environment should return the wrapped environment's scope; otherwise it is locked as SHARED, as is any value that is not an EnvironmentScope.
A scoped environment also needs a session. With USER or SESSION scope, a tool call whose context has no session raises an error before initialize is called, because there is no user or session to key the lock by.
What close still serializes
Closing a toolset does not respect the scope. A close waits for every initialize call that is in flight, whichever user or session it belongs to, and holds back new calls from the moment it starts waiting, so a steady stream of arrivals cannot postpone it forever.
The model asks for a close at 3.0 s under SESSION scope. Four sandbox creations are running, and the last finishes at 10.0 s, so the close runs from 10.0 s to 11.0 s. A sixth call, Jenya's in s1 at 4.0 s, arrives while the close is waiting. It is held at the gate, starts at 11.0 s and, because the close removed the sandboxes, pays a cold start again. Its wait is 7.0 seconds, entirely caused by the close.
The ADK code counts a call as in flight once it has passed the close gate, including while it waits for its own key's lock. That detail matters for capacity planning: a close during a burst waits for the whole burst that has already been admitted.
The lock stops at the process
The toolsets' locks are ordinary in-process locks. The documentation is explicit: "The toolsets serialize these calls within one process. When the agent runs as several instances, calls that reach different instances are not serialized."
The model's last scenario sends two calls for Jenya's session s1 to two processes, A at 0.0 s and B at 0.3 s. Neither waits, and both create a sandbox for s1. If the sandbox lives in a shared backend, such as a remote sandbox service keyed by session, the session now has two, or one creation fails against the other. Scope does not help here, because neither process can see the other's lock.
If you run more than one instance, either route all of a session's requests to the same instance, or make sandbox creation idempotent at the backend: create with a key derived from the session, and treat "already exists" as success. A distributed lock is a third option, with its own failure modes around expiry and fencing.
Errors from initialize
The 2.15.0 guide also documents failure: if initialize raises, for example because a sandbox cannot be created, the tool call raises the same exception, which reaches on_tool_error_callback like any other tool exception. Returning a result from that callback lets the model see the failure and carry on, and the next tool call calls initialize again. With a narrow scope, one session's failing sandbox no longer blocks other sessions while it retries.
Migrating an existing environment
- Identify the resource your environment's
initializecreates and whether it is per environment, per user or per session. - Override
scopeto return the matching value, and nothing narrower. - Audit the state your
initializeshares across calls, and lock it for concurrent access from several threads. - Make
initializeidempotent and fast on the warm path, since it runs before every tool call. - If you deploy several instances, make sandbox creation idempotent at the backend or use session affinity.
- Test with concurrent sessions, not just one, and include a close during activity.
Limits of the model
The companion code is a discrete-time model of the behaviour described in the 2.15.0 source and guide, not ADK code. It serves waiting calls in arrival order; ADK polls its locks, so real ordering under contention is not guaranteed. Durations are assumed, and real sandbox start times vary widely.
Verification
The companion example has seven tests: SHARED makes every session wait behind cold starts, SESSION serialises only one session, USER serialises one user's sessions, a scope narrower than the resource creates it twice, a close waits for in-flight calls and holds new ones, locks do not reach another process, and a scoped environment without a session is refused. Run python -m unittest -v.
Run the example yourself
The discrete-time lock model, the six scenarios and seven tests. It is a model, not ADK code. Companion code on GitHub; its README lists the prerequisites, exact commands, expected output and limits.
Interactive experiment
Environment initialize locks: SHARED, USER and SESSION scope on a timeline. Synthetic inputs computed in your browser; read the lab notes on assumptions and limits before drawing conclusions.