Interactive engineering lab

Environment initialize locks: SHARED, USER and SESSION scope on a timeline

Simulate tool calls under SHARED, USER and SESSION initialize locks with a slow sandbox start, a close and a second process; read waits and duplicates.

By Sandeep Belgavi · Published 2026-11-24

Scenario: an agent with a code sandbox per session serves help-desk staff at the fictional Belgavi's Book Store; each tool call initializes the environment first. Synthetic inputs; every calculation stays in this page and nothing is sent anywhere.

Experiment workspace

Decision trace

    What this lab models

    ADK Python 2.15.0 locks initialize calls by EnvironmentScope: one lock for the whole environment with SHARED, one per app and user with USER, one per app, user and session with SESSION. A close waits for every initialize call in flight and holds new ones. The lab runs five tool calls from Jenya, Geetha and Tanishka in one process on a timeline: a cold initialize creates a sandbox, a warm one takes 0.1 s. You choose the scope, the granularity of the sandbox the environment actually keeps, the cold-start time, a close request, a second process and a call without a session.

    How to experiment

    Start with SESSION scope and a per-session sandbox: only c3, the second call in Jenya's session, waits. Switch to SHARED: every call queues behind every cold start. Switch to USER: Geetha's second session waits behind her first. Now keep SESSION scope and set the sandbox to per user: the model reports a duplicate start for c5, because the lock is narrower than the resource. Turn on the close request and move its time: in-flight calls finish first, and the call that arrives one second after the request is held until the close ends. Send c3 to a second process: both processes create the same sandbox. Remove c4's session to see the scoped environment refuse the call.

    Expected outcomes to check

    Defaults (SESSION, per session, 8 s cold): waits 0, 0, 7.0, 0, 0 s. SHARED: 0, 7.5, 15.0, 14.6, 22.1 s. USER: c5 waits 6.5 s. SESSION scope with a per-user sandbox: one duplicate start (c5). Close at 3 s: close runs 10.0-11.0 s and c6 starts cold at 11.0 s. Second process: one cross-process duplicate for Jenya's session. No session with USER or SESSION scope: refused before initialize. These match the companion Python example.

    Trace, reset and export

    The trace lists every arrival, lock acquisition with its wait, finish, close event and duplicate start in time order. Start trace, Next step, Show all and the slider control how much is visible. Reset inputs restores the defaults. Export JSON saves the inputs, the per-call table, the duplicates and the trace.

    Limits

    A discrete-time model in tenths of a second, not ADK code. Waiting calls are served first come, first served; ADK polls its locks and does not promise an order. Durations are assumptions. The second-process case assumes the sandbox lives in a backend both processes share; with sandboxes local to each process the result is two separate sandboxes rather than a conflict.

    Original lab by Sandeep Belgavi. Views are my own and do not represent any employer, past or present.