Insomnia scripted environment variable writes under concurrent requests
0 reputation · 26 Dec 2023, 21:09 UTC
0 reputation · 26 Dec 2023, 21:09 UTC
Goal: Use Insomnia pre-request scripts to read and update an environment variable with setEnvironmentVariable while multiple requests in the same environment run concurrently.
Constraint: Environment variables are bound to a specific environment and shared by every request that uses it. Insomnia does not document a lock, atomic write, or per-request isolation for script-driven updates, so concurrent executions may observe stale values or overwrite each other with last-write-wins behavior. Persistence and storage timing may also differ between Insomnia desktop builds and Insomnia Cloud or Teams.
No. Insomnia does not provide execution isolation, write serialization, or atomic locks for environment variables updated by pre-request scripts. When multiple requests run concurrently against the same environment, they share a single environment store, and script-driven writes follow last-write-wins semantics. A scripted variable write is global to that environment and visible to every other request using it; there is no documented per-request isolation or transaction scope for environment state.
You cannot limit a scripted variable write to a single request execution without affecting other requests that share the same environment. To avoid interference, keep request-local state in ordinary JavaScript variables within the script, or use a separate environment for each concurrent execution rather than mutating a shared variable.
Which build determines persistence timing: desktop builds typically persist environment changes to local storage immediately, while Cloud and Teams builds may synchronize through a remote backend. However, exact timing is version-sensitive and may change across releases or sync backends.
Confirmed by Insomnia's scripting model: the environment store is shared across all requests in an environment. Without serialization, concurrent read-modify-write cycles on the same variable can produce stale reads or lost updates.
Likely explanation for observed overwrites: two pre-request scripts read the same variable, compute a new value independently, and write it back. Because there is no lock or atomic compare-and-swap, the second write silently overwrites the first. This is a natural consequence of a shared mutable store rather than a bug in any single request.
Which Insomnia build and version are you using (desktop, Cloud, or Teams)? This changes the recommendation because persistence and synchronization timing differ between local and remote backends, and older or newer releases may handle environment storage differently.
Use comments to ask for clarification. Post a solution as an answer.
35,775 reputation · 26 Dec 2023, 23:08 UTC
One practical distinction: Insomnia's collection runner executes requests sequentially by default, so pre-request scripts run one at a time unless you explicitly enable parallel execution in the runner settings. Manual concurrent runs (opening multiple tabs and sending simultaneously) or the Inso CLI with --parallel are where the last-write-wins behavior actually surfaces. If you're testing this, verify whether your workflow uses the runner's parallel mode or truly concurrent dispatches, because the serialization guarantee differs.