Skip to content

fix(engine): guard session client maps during GC - #13474

Merged
sipsma merged 3 commits into
dagger:mainfrom
sipsma:fix-panic-thread-202606161740
Jun 16, 2026
Merged

sipsma merged 3 commits into
dagger:mainfrom
sipsma:fix-panic-thread-202606161740

Conversation

@sipsma

@sipsma sipsma commented Jun 16, 2026 •

Copy link
Copy Markdown
Contributor

Problem

The engine could crash with a fatal Go runtime panic, triggered from background client-DB garbage collection:

fatal error: concurrent map iteration and map write

goroutine 73 [running]:
internal/runtime/maps.fatal(...)
internal/runtime/maps.(*Iter).Next(...)
github.com/dagger/dagger/engine/server.(*Server).activeClientIDs(...)
      /app/engine/server/server.go:911
github.com/dagger/dagger/engine/server.(*Server).gcClientDBs(...)
      /app/engine/server/server.go:900
created by github.com/dagger/dagger/engine/server.NewServer

gcClientDBs runs activeClientIDs on a background ticker. activeClientIDs iterated each session's sess.clients map while holding only daggerSessionsMu, but getOrInitClient writes that map under sess.clientMu. So GC could race client (and nested-client) initialization and hit the fatal concurrent map iteration/write.

Follow-up problems

While fixing the above, a related init-window race surfaced: a new session is published into srv.daggerSessions before initializeDaggerSession populates fields such as clients and mainClientCallerID under stateMu. Readers like clientFromIDs and Clients() could find the session pointer and read those fields while holding only daggerSessionsMu or clientMu, racing initialization.

A review also caught a lock-order inversion in main-client cleanup: getOrInitClient takes the session lock before the client lock, while the cleanup closure took the client lock before trying to remove the session. An overlapping cleanup and new request for the same main client could therefore block each other.

Fix

GC / map iteration:

  • activeClientIDs snapshots the sessions under daggerSessionsMu and releases it before taking any per-session lock — this avoids a lock-order inversion against removeDaggerSession, which holds stateMu then takes daggerSessionsMu — then reads each initialized session's client map under stateMu + clientMu.
  • removeDaggerSession snapshots sess.clients under clientMu before spawning teardown goroutines.

Init-window race:

  • For newly created sessions, getOrInitClient locks stateMu before publishing the session pointer into srv.daggerSessions, so concurrent readers block until initialization completes instead of observing partially initialized fields. This is safe because the session is still unreachable by other goroutines at that point, so the lock never blocks.
  • clientFromIDs snapshots the session under daggerSessionsMu, releases it, gates on session state under stateMu, then reads clients under clientMu.
  • Clients() similarly snapshots the sessions, then reads mainClientCallerID under stateMu for initialized sessions only.

Cleanup lock ordering:

  • Main-client cleanup now takes stateMu before client.stateMu, matching getOrInitClient, and releases client.stateMu before session removal.
  • Non-main cleanup only needs client.stateMu to decrement its active count, so it avoids taking the session lock.

Testing

  • Added race regression coverage for activeClientIDs racing concurrent session-client mutation.
  • Added race regression coverage for clientFromIDs racing concurrent session initialization.
  • go test ./engine/server
  • go test -race ./engine/server

sipsma added 2 commits June 16, 2026 11:51
activeClientIDs (run from the gcClientDBs ticker) ranged sess.clients
while holding only daggerSessionsMu, racing getOrInitClient which mutates
that map under clientMu and triggering a fatal "concurrent map iteration
and map write".

Snapshot the sessions under daggerSessionsMu, release it, then read each
session's clients under stateMu+clientMu, skipping non-initialized
sessions. Releasing daggerSessionsMu before taking stateMu preserves the
existing lock order (removeDaggerSession holds stateMu then takes
daggerSessionsMu) and avoids deadlock. Apply the same clientMu-guarded
snapshot in removeDaggerSession, and add a race regression test.

Signed-off-by: Erik Sipsma <[email protected]>
A daggerSession was published into srv.daggerSessions before
initializeDaggerSession populated fields such as clients and
mainClientCallerID under stateMu, so readers holding only
daggerSessionsMu/clientMu could observe and data-race on those fields
mid-initialization.

- getOrInitClient: lock the new session's stateMu before publishing the
  pointer, closing the uninitialized-publication gap. The lock is on a
  freshly created, still-unpublished session, so it never contends and
  cannot invert against removeDaggerSession's stateMu->daggerSessionsMu
  order.
- clientFromIDs: snapshot the session under daggerSessionsMu, release it,
  then gate on session state under stateMu before reading clients under
  clientMu, handling removed/uninitialized states explicitly.
- Clients: snapshot sessions, then read mainClientCallerID under stateMu
  for initialized sessions only.
- Add TestClientFromIDsConcurrentSessionInitialization, reproducing the
  field race under -race.

Signed-off-by: Erik Sipsma <[email protected]>
@sipsma sipsma added this to the v0.21.7 milestone Jun 16, 2026
@sipsma
sipsma merged commit c50b37d into dagger:main Jun 16, 2026
77 of 78 checks passed
marcosnils pushed a commit that referenced this pull request Jun 16, 2026
* fix(engine): guard sess.clients iteration to avoid concurrent map panic

activeClientIDs (run from the gcClientDBs ticker) ranged sess.clients
while holding only daggerSessionsMu, racing getOrInitClient which mutates
that map under clientMu and triggering a fatal "concurrent map iteration
and map write".

Snapshot the sessions under daggerSessionsMu, release it, then read each
session's clients under stateMu+clientMu, skipping non-initialized
sessions. Releasing daggerSessionsMu before taking stateMu preserves the
existing lock order (removeDaggerSession holds stateMu then takes
daggerSessionsMu) and avoids deadlock. Apply the same clientMu-guarded
snapshot in removeDaggerSession, and add a race regression test.

Signed-off-by: Erik Sipsma <[email protected]>

* fix(engine): guard session fields against init-window race

A daggerSession was published into srv.daggerSessions before
initializeDaggerSession populated fields such as clients and
mainClientCallerID under stateMu, so readers holding only
daggerSessionsMu/clientMu could observe and data-race on those fields
mid-initialization.

- getOrInitClient: lock the new session's stateMu before publishing the
  pointer, closing the uninitialized-publication gap. The lock is on a
  freshly created, still-unpublished session, so it never contends and
  cannot invert against removeDaggerSession's stateMu->daggerSessionsMu
  order.
- clientFromIDs: snapshot the session under daggerSessionsMu, release it,
  then gate on session state under stateMu before reading clients under
  clientMu, handling removed/uninitialized states explicitly.
- Clients: snapshot sessions, then read mainClientCallerID under stateMu
  for initialized sessions only.
- Add TestClientFromIDsConcurrentSessionInitialization, reproducing the
  field race under -race.

Signed-off-by: Erik Sipsma <[email protected]>

* fix(engine): avoid client cleanup lock inversion

Signed-off-by: Erik Sipsma <[email protected]>

---------

Signed-off-by: Erik Sipsma <[email protected]>
marcosnils pushed a commit that referenced this pull request Jun 16, 2026
* fix(engine): guard sess.clients iteration to avoid concurrent map panic

activeClientIDs (run from the gcClientDBs ticker) ranged sess.clients
while holding only daggerSessionsMu, racing getOrInitClient which mutates
that map under clientMu and triggering a fatal "concurrent map iteration
and map write".

Snapshot the sessions under daggerSessionsMu, release it, then read each
session's clients under stateMu+clientMu, skipping non-initialized
sessions. Releasing daggerSessionsMu before taking stateMu preserves the
existing lock order (removeDaggerSession holds stateMu then takes
daggerSessionsMu) and avoids deadlock. Apply the same clientMu-guarded
snapshot in removeDaggerSession, and add a race regression test.

Signed-off-by: Erik Sipsma <[email protected]>

* fix(engine): guard session fields against init-window race

A daggerSession was published into srv.daggerSessions before
initializeDaggerSession populated fields such as clients and
mainClientCallerID under stateMu, so readers holding only
daggerSessionsMu/clientMu could observe and data-race on those fields
mid-initialization.

- getOrInitClient: lock the new session's stateMu before publishing the
  pointer, closing the uninitialized-publication gap. The lock is on a
  freshly created, still-unpublished session, so it never contends and
  cannot invert against removeDaggerSession's stateMu->daggerSessionsMu
  order.
- clientFromIDs: snapshot the session under daggerSessionsMu, release it,
  then gate on session state under stateMu before reading clients under
  clientMu, handling removed/uninitialized states explicitly.
- Clients: snapshot sessions, then read mainClientCallerID under stateMu
  for initialized sessions only.
- Add TestClientFromIDsConcurrentSessionInitialization, reproducing the
  field race under -race.

Signed-off-by: Erik Sipsma <[email protected]>

* fix(engine): avoid client cleanup lock inversion

Signed-off-by: Erik Sipsma <[email protected]>

---------

Signed-off-by: Erik Sipsma <[email protected]>
marcosnils pushed a commit that referenced this pull request Jun 16, 2026
* fix(engine): guard sess.clients iteration to avoid concurrent map panic

activeClientIDs (run from the gcClientDBs ticker) ranged sess.clients
while holding only daggerSessionsMu, racing getOrInitClient which mutates
that map under clientMu and triggering a fatal "concurrent map iteration
and map write".

Snapshot the sessions under daggerSessionsMu, release it, then read each
session's clients under stateMu+clientMu, skipping non-initialized
sessions. Releasing daggerSessionsMu before taking stateMu preserves the
existing lock order (removeDaggerSession holds stateMu then takes
daggerSessionsMu) and avoids deadlock. Apply the same clientMu-guarded
snapshot in removeDaggerSession, and add a race regression test.

Signed-off-by: Erik Sipsma <[email protected]>

* fix(engine): guard session fields against init-window race

A daggerSession was published into srv.daggerSessions before
initializeDaggerSession populated fields such as clients and
mainClientCallerID under stateMu, so readers holding only
daggerSessionsMu/clientMu could observe and data-race on those fields
mid-initialization.

- getOrInitClient: lock the new session's stateMu before publishing the
  pointer, closing the uninitialized-publication gap. The lock is on a
  freshly created, still-unpublished session, so it never contends and
  cannot invert against removeDaggerSession's stateMu->daggerSessionsMu
  order.
- clientFromIDs: snapshot the session under daggerSessionsMu, release it,
  then gate on session state under stateMu before reading clients under
  clientMu, handling removed/uninitialized states explicitly.
- Clients: snapshot sessions, then read mainClientCallerID under stateMu
  for initialized sessions only.
- Add TestClientFromIDsConcurrentSessionInitialization, reproducing the
  field race under -race.

Signed-off-by: Erik Sipsma <[email protected]>

* fix(engine): avoid client cleanup lock inversion

Signed-off-by: Erik Sipsma <[email protected]>

---------

Signed-off-by: Erik Sipsma <[email protected]>
marcosnils pushed a commit that referenced this pull request Jun 16, 2026
* fix(engine): guard sess.clients iteration to avoid concurrent map panic

activeClientIDs (run from the gcClientDBs ticker) ranged sess.clients
while holding only daggerSessionsMu, racing getOrInitClient which mutates
that map under clientMu and triggering a fatal "concurrent map iteration
and map write".

Snapshot the sessions under daggerSessionsMu, release it, then read each
session's clients under stateMu+clientMu, skipping non-initialized
sessions. Releasing daggerSessionsMu before taking stateMu preserves the
existing lock order (removeDaggerSession holds stateMu then takes
daggerSessionsMu) and avoids deadlock. Apply the same clientMu-guarded
snapshot in removeDaggerSession, and add a race regression test.

Signed-off-by: Erik Sipsma <[email protected]>

* fix(engine): guard session fields against init-window race

A daggerSession was published into srv.daggerSessions before
initializeDaggerSession populated fields such as clients and
mainClientCallerID under stateMu, so readers holding only
daggerSessionsMu/clientMu could observe and data-race on those fields
mid-initialization.

- getOrInitClient: lock the new session's stateMu before publishing the
  pointer, closing the uninitialized-publication gap. The lock is on a
  freshly created, still-unpublished session, so it never contends and
  cannot invert against removeDaggerSession's stateMu->daggerSessionsMu
  order.
- clientFromIDs: snapshot the session under daggerSessionsMu, release it,
  then gate on session state under stateMu before reading clients under
  clientMu, handling removed/uninitialized states explicitly.
- Clients: snapshot sessions, then read mainClientCallerID under stateMu
  for initialized sessions only.
- Add TestClientFromIDsConcurrentSessionInitialization, reproducing the
  field race under -race.

Signed-off-by: Erik Sipsma <[email protected]>

* fix(engine): avoid client cleanup lock inversion

Signed-off-by: Erik Sipsma <[email protected]>

---------

Signed-off-by: Erik Sipsma <[email protected]>
marcosnils pushed a commit that referenced this pull request Jun 17, 2026
* fix(engine): guard sess.clients iteration to avoid concurrent map panic

activeClientIDs (run from the gcClientDBs ticker) ranged sess.clients
while holding only daggerSessionsMu, racing getOrInitClient which mutates
that map under clientMu and triggering a fatal "concurrent map iteration
and map write".

Snapshot the sessions under daggerSessionsMu, release it, then read each
session's clients under stateMu+clientMu, skipping non-initialized
sessions. Releasing daggerSessionsMu before taking stateMu preserves the
existing lock order (removeDaggerSession holds stateMu then takes
daggerSessionsMu) and avoids deadlock. Apply the same clientMu-guarded
snapshot in removeDaggerSession, and add a race regression test.

Signed-off-by: Erik Sipsma <[email protected]>

* fix(engine): guard session fields against init-window race

A daggerSession was published into srv.daggerSessions before
initializeDaggerSession populated fields such as clients and
mainClientCallerID under stateMu, so readers holding only
daggerSessionsMu/clientMu could observe and data-race on those fields
mid-initialization.

- getOrInitClient: lock the new session's stateMu before publishing the
  pointer, closing the uninitialized-publication gap. The lock is on a
  freshly created, still-unpublished session, so it never contends and
  cannot invert against removeDaggerSession's stateMu->daggerSessionsMu
  order.
- clientFromIDs: snapshot the session under daggerSessionsMu, release it,
  then gate on session state under stateMu before reading clients under
  clientMu, handling removed/uninitialized states explicitly.
- Clients: snapshot sessions, then read mainClientCallerID under stateMu
  for initialized sessions only.
- Add TestClientFromIDsConcurrentSessionInitialization, reproducing the
  field race under -race.

Signed-off-by: Erik Sipsma <[email protected]>

* fix(engine): avoid client cleanup lock inversion

Signed-off-by: Erik Sipsma <[email protected]>

---------

Signed-off-by: Erik Sipsma <[email protected]>
marcosnils pushed a commit that referenced this pull request Jun 17, 2026
* fix(engine): guard sess.clients iteration to avoid concurrent map panic

activeClientIDs (run from the gcClientDBs ticker) ranged sess.clients
while holding only daggerSessionsMu, racing getOrInitClient which mutates
that map under clientMu and triggering a fatal "concurrent map iteration
and map write".

Snapshot the sessions under daggerSessionsMu, release it, then read each
session's clients under stateMu+clientMu, skipping non-initialized
sessions. Releasing daggerSessionsMu before taking stateMu preserves the
existing lock order (removeDaggerSession holds stateMu then takes
daggerSessionsMu) and avoids deadlock. Apply the same clientMu-guarded
snapshot in removeDaggerSession, and add a race regression test.

Signed-off-by: Erik Sipsma <[email protected]>

* fix(engine): guard session fields against init-window race

A daggerSession was published into srv.daggerSessions before
initializeDaggerSession populated fields such as clients and
mainClientCallerID under stateMu, so readers holding only
daggerSessionsMu/clientMu could observe and data-race on those fields
mid-initialization.

- getOrInitClient: lock the new session's stateMu before publishing the
  pointer, closing the uninitialized-publication gap. The lock is on a
  freshly created, still-unpublished session, so it never contends and
  cannot invert against removeDaggerSession's stateMu->daggerSessionsMu
  order.
- clientFromIDs: snapshot the session under daggerSessionsMu, release it,
  then gate on session state under stateMu before reading clients under
  clientMu, handling removed/uninitialized states explicitly.
- Clients: snapshot sessions, then read mainClientCallerID under stateMu
  for initialized sessions only.
- Add TestClientFromIDsConcurrentSessionInitialization, reproducing the
  field race under -race.

Signed-off-by: Erik Sipsma <[email protected]>

* fix(engine): avoid client cleanup lock inversion

Signed-off-by: Erik Sipsma <[email protected]>

---------

Signed-off-by: Erik Sipsma <[email protected]>
marcosnils pushed a commit that referenced this pull request Jun 17, 2026
* fix(engine): guard sess.clients iteration to avoid concurrent map panic

activeClientIDs (run from the gcClientDBs ticker) ranged sess.clients
while holding only daggerSessionsMu, racing getOrInitClient which mutates
that map under clientMu and triggering a fatal "concurrent map iteration
and map write".

Snapshot the sessions under daggerSessionsMu, release it, then read each
session's clients under stateMu+clientMu, skipping non-initialized
sessions. Releasing daggerSessionsMu before taking stateMu preserves the
existing lock order (removeDaggerSession holds stateMu then takes
daggerSessionsMu) and avoids deadlock. Apply the same clientMu-guarded
snapshot in removeDaggerSession, and add a race regression test.

Signed-off-by: Erik Sipsma <[email protected]>

* fix(engine): guard session fields against init-window race

A daggerSession was published into srv.daggerSessions before
initializeDaggerSession populated fields such as clients and
mainClientCallerID under stateMu, so readers holding only
daggerSessionsMu/clientMu could observe and data-race on those fields
mid-initialization.

- getOrInitClient: lock the new session's stateMu before publishing the
  pointer, closing the uninitialized-publication gap. The lock is on a
  freshly created, still-unpublished session, so it never contends and
  cannot invert against removeDaggerSession's stateMu->daggerSessionsMu
  order.
- clientFromIDs: snapshot the session under daggerSessionsMu, release it,
  then gate on session state under stateMu before reading clients under
  clientMu, handling removed/uninitialized states explicitly.
- Clients: snapshot sessions, then read mainClientCallerID under stateMu
  for initialized sessions only.
- Add TestClientFromIDsConcurrentSessionInitialization, reproducing the
  field race under -race.

Signed-off-by: Erik Sipsma <[email protected]>

* fix(engine): avoid client cleanup lock inversion

Signed-off-by: Erik Sipsma <[email protected]>

---------

Signed-off-by: Erik Sipsma <[email protected]>
sipsma added a commit to sipsma/dagger that referenced this pull request Jun 23, 2026
The active-clients API (engine.clients, polled by the cloud keepalive)
and the client-DB GC ticker acquired each session's stateMu. That lock
is held across the entire session/client initialization and across
removeDaggerSession teardown (which has a 60s safeguard). A single
session stuck initializing or tearing down therefore stalled the
active-clients enumeration for ALL sessions, so the keepalive could age
past the cloud's 15s inactivity timeout and the consolidator would
suspend a live engine, crashing connected clients.

Hard-cut the session/client lock model so observer paths never touch a
per-session lifecycle lock:

- Session identity (sessionID, mainClientCallerID) is immutable, set at
  construction before the session is published into the registry.
- Session state is an atomic, read lock-free by observers.
- The per-session lock (renamed stateMu -> lifecycleMu) serializes only
  init and teardown; no observer path acquires it.
- Clients / activeClientIDs / clientFromIDs read atomic state + immutable
  identity (plus a brief clientMu for the client map), never lifecycleMu,
  so they can never stall on a session in init or teardown.
- Teardown stores state=removed first, leaves the session in the registry
  as a tombstone during cleanup, and pointer-conditionally deletes it only
  after lifecycleMu is released. This also fixes a latent same-id
  resurrection race in the old code, which deleted the session from the
  registry before releasing its session-scoped cache.
- getOrInitClient fast-path returns a retryable error for a removed
  tombstone without taking lifecycleMu; the new session locks lifecycleMu
  before publication (init-owner guarantee); clients are inserted only
  after full initialization; the client DB is opened outside clientMu.
- GracefulStop snapshots session pointers into a slice before iterating.

Adds -race regression tests for the active-clients stall, the same-id
fast-return on a removed tombstone, clientFromIDs state gating, and a
lifecycle/observer concurrency stress test; adapts the dagger#13474
concurrent-map and init-window race tests to the new field types.

Signed-off-by: Erik Sipsma <[email protected]>
sipsma added a commit that referenced this pull request Jun 25, 2026
…#13542)

The active-clients API (engine.clients, polled by the cloud keepalive)
and the client-DB GC ticker acquired each session's stateMu. That lock
is held across the entire session/client initialization and across
removeDaggerSession teardown (which has a 60s safeguard). A single
session stuck initializing or tearing down therefore stalled the
active-clients enumeration for ALL sessions, so the keepalive could age
past the cloud's 15s inactivity timeout and the consolidator would
suspend a live engine, crashing connected clients.

Hard-cut the session/client lock model so observer paths never touch a
per-session lifecycle lock:

- Session identity (sessionID, mainClientCallerID) is immutable, set at
  construction before the session is published into the registry.
- Session state is an atomic, read lock-free by observers.
- The per-session lock (renamed stateMu -> lifecycleMu) serializes only
  init and teardown; no observer path acquires it.
- Clients / activeClientIDs / clientFromIDs read atomic state + immutable
  identity (plus a brief clientMu for the client map), never lifecycleMu,
  so they can never stall on a session in init or teardown.
- Teardown stores state=removed first, leaves the session in the registry
  as a tombstone during cleanup, and pointer-conditionally deletes it only
  after lifecycleMu is released. This also fixes a latent same-id
  resurrection race in the old code, which deleted the session from the
  registry before releasing its session-scoped cache.
- getOrInitClient fast-path returns a retryable error for a removed
  tombstone without taking lifecycleMu; the new session locks lifecycleMu
  before publication (init-owner guarantee); clients are inserted only
  after full initialization; the client DB is opened outside clientMu.
- GracefulStop snapshots session pointers into a slice before iterating.

Adds -race regression tests for the active-clients stall, the same-id
fast-return on a removed tombstone, clientFromIDs state gating, and a
lifecycle/observer concurrency stress test; adapts the #13474
concurrent-map and init-window race tests to the new field types.

Signed-off-by: Erik Sipsma <[email protected]>
matipan pushed a commit to matipan/dagger that referenced this pull request Aug 10, 2026
…dagger#13542)

The active-clients API (engine.clients, polled by the cloud keepalive)
and the client-DB GC ticker acquired each session's stateMu. That lock
is held across the entire session/client initialization and across
removeDaggerSession teardown (which has a 60s safeguard). A single
session stuck initializing or tearing down therefore stalled the
active-clients enumeration for ALL sessions, so the keepalive could age
past the cloud's 15s inactivity timeout and the consolidator would
suspend a live engine, crashing connected clients.

Hard-cut the session/client lock model so observer paths never touch a
per-session lifecycle lock:

- Session identity (sessionID, mainClientCallerID) is immutable, set at
  construction before the session is published into the registry.
- Session state is an atomic, read lock-free by observers.
- The per-session lock (renamed stateMu -> lifecycleMu) serializes only
  init and teardown; no observer path acquires it.
- Clients / activeClientIDs / clientFromIDs read atomic state + immutable
  identity (plus a brief clientMu for the client map), never lifecycleMu,
  so they can never stall on a session in init or teardown.
- Teardown stores state=removed first, leaves the session in the registry
  as a tombstone during cleanup, and pointer-conditionally deletes it only
  after lifecycleMu is released. This also fixes a latent same-id
  resurrection race in the old code, which deleted the session from the
  registry before releasing its session-scoped cache.
- getOrInitClient fast-path returns a retryable error for a removed
  tombstone without taking lifecycleMu; the new session locks lifecycleMu
  before publication (init-owner guarantee); clients are inserted only
  after full initialization; the client DB is opened outside clientMu.
- GracefulStop snapshots session pointers into a slice before iterating.

Adds -race regression tests for the active-clients stall, the same-id
fast-return on a removed tombstone, clientFromIDs state gating, and a
lifecycle/observer concurrency stress test; adapts the dagger#13474
concurrent-map and init-window race tests to the new field types.

Signed-off-by: Erik Sipsma <[email protected]>
(cherry picked from commit 0e21927)
Signed-off-by: Matias Pan <[email protected]>
sipsma added a commit that referenced this pull request Aug 13, 2026
…tdown path (#13884)

* fix(engine): decouple active-clients API from session lifecycle locks (#13542)

The active-clients API (engine.clients, polled by the cloud keepalive)
and the client-DB GC ticker acquired each session's stateMu. That lock
is held across the entire session/client initialization and across
removeDaggerSession teardown (which has a 60s safeguard). A single
session stuck initializing or tearing down therefore stalled the
active-clients enumeration for ALL sessions, so the keepalive could age
past the cloud's 15s inactivity timeout and the consolidator would
suspend a live engine, crashing connected clients.

Hard-cut the session/client lock model so observer paths never touch a
per-session lifecycle lock:

- Session identity (sessionID, mainClientCallerID) is immutable, set at
  construction before the session is published into the registry.
- Session state is an atomic, read lock-free by observers.
- The per-session lock (renamed stateMu -> lifecycleMu) serializes only
  init and teardown; no observer path acquires it.
- Clients / activeClientIDs / clientFromIDs read atomic state + immutable
  identity (plus a brief clientMu for the client map), never lifecycleMu,
  so they can never stall on a session in init or teardown.
- Teardown stores state=removed first, leaves the session in the registry
  as a tombstone during cleanup, and pointer-conditionally deletes it only
  after lifecycleMu is released. This also fixes a latent same-id
  resurrection race in the old code, which deleted the session from the
  registry before releasing its session-scoped cache.
- getOrInitClient fast-path returns a retryable error for a removed
  tombstone without taking lifecycleMu; the new session locks lifecycleMu
  before publication (init-owner guarantee); clients are inserted only
  after full initialization; the client DB is opened outside clientMu.
- GracefulStop snapshots session pointers into a slice before iterating.

Adds -race regression tests for the active-clients stall, the same-id
fast-return on a removed tombstone, clientFromIDs state gating, and a
lifecycle/observer concurrency stress test; adapts the #13474
concurrent-map and init-window race tests to the new field types.

Signed-off-by: Erik Sipsma <[email protected]>
(cherry picked from commit 0e21927)
Signed-off-by: Matias Pan <[email protected]>

* fix(engine): take session teardown off the client shutdown path

The main client's /shutdown POST could exceed the CLI's shutdown budget
(default 10s) even though the run itself succeeded, failing CI jobs at
cleanup with:

    shutdown: do shutdown: Post "http://dagger/shutdown": context deadline exceeded

The mechanism: serveShutdown's beginClosing() cancels the main client's
session-attachables request, so the /shutdown POST is usually the main
client's last active connection. Its per-request cleanup then ran the
entire removeDaggerSession synchronously — in the request handler,
before the response is flushed. Teardown is unbounded: the dagql cache
release alone walks every session-owned result and runs their release
funcs sequentially (one production trace showed ~11.8k buildkit ref
releases consuming the whole budget on an engine with ~188k live cache
entries). The symptom disappears on a fresh cache and scales with
parallelism, matching user reports on 0.21.7.

Schedule teardown on a background reaper instead of running it in the
request's cleanup. The reaper re-makes the teardown decision under
lifecycleMu (state still initialized, main client still at zero active
connections), so a reconnect that lands between the last disconnect and
the reap abandons the reap, duplicate reaps no-op via the removed
state, and GracefulStop coordinates through lifecycleMu exactly as
before. The removed-tombstone machinery already keeps same-id
reconnects safe during background teardown (retryable error until the
tombstone drops).

The client-critical drain (workspace lock flush, service stop,
session-wide telemetry flush) still runs synchronously in serveShutdown
before the response; only the engine-internal reclamation moves off the
ACK path.

Adds -race regression tests: last-disconnect cleanup returns while
teardown is blocked mid-drain, same-id connects during background
teardown fail retryable and the tombstone survives until teardown
completes, a reconnect abandons a scheduled reap, and concurrent reaps
tear down exactly once.

Signed-off-by: Erik Sipsma <[email protected]>
(cherry picked from commit b395309)

* chore: add v0.21.9 release note for #13681

Signed-off-by: Erik Sipsma <[email protected]>

* test(engine): adapt teardown fixtures for v0.21

Signed-off-by: Erik Sipsma <[email protected]>

* test: use project deploy tokens for GitLab (#13849)

GitLab automatically revokes user PATs when it detects that they were leaked.
That is what these private repository tests used, and both tokens were
revoked. GitLab does not document the same automatic response for project
deploy tokens.

Replace the PATs with read-only deploy tokens scoped to the test project.
Deploy tokens use a username and token pair, so pass the matching username
through each Git authentication path.

These credentials are intentionally committed for end-to-end tests. They can
only read one disposable test repository, which contains no private code or
data, so exposing them does not grant access beyond the test fixture.

Keep both credentials in the shared VCS fixture so future rotations happen in
one place. The second token is still needed to test two different credentials
for the same repository.

Signed-off-by: Guillaume de Rouville <[email protected]>
(cherry picked from commit 66337ba)
Signed-off-by: Erik Sipsma <[email protected]>

---------

Signed-off-by: Erik Sipsma <[email protected]>
Signed-off-by: Matias Pan <[email protected]>
Signed-off-by: Guillaume de Rouville <[email protected]>
Co-authored-by: Guillaume de Rouville <[email protected]>
social4hyq pushed a commit to social4hyq/homebrew-core that referenced this pull request Sep 20, 2026
dagger 0.21.7

Created-by: HarmonybrewBot
Commit-by: HarmonybrewBot
Merged-by: HarmonybrewBot
Description: Created by `brew bump`

---

Created with `brew bump-formula-pr`.<details>
  <summary>release notes</summary>
  <pre>## v0.21.7 - 2026-06-17

### Changed

- Bump default CNI IP address pool size to reduce address exhaustion under high container concurrency. by @sipsma in dagger/dagger#13394
- Bump Dang to v2.1.1, moving JSON encoding behind the `JSON` namespace (with a deprecated top-level `toJSON`) and picking up constructor scope and nullable input fixes. by @vito in dagger/dagger#13482

### Fixed

- `.env` file handling so non-matching variables are kept as hidden expansion context instead of being dropped, allowing later variables to expand against them. by @marcosnils in dagger/dagger#13490
- Filesync so syncing a re-included file under a gitignored directory (e.g. `foo/` ignored with `!foo/bar.txt`) no longer fails with `no such file or directory`; ignored parent dirs are now created on demand for visible descendants. by @grouville in dagger/dagger#13439
- A concurrent map panic during GC by guarding session client map iteration, preventing fatal `concurrent map iteration and map write` crashes. by @sipsma in dagger/dagger#13474
- Go SDK codegen so SDK auth config is forwarded to typedef generation, allowing modules with private transitive Go dependencies to resolve during codegen. by @grouville in dagger/dagger#13457
- Core enum handling to preserve conventional enum member names. by @shykes in dagger/dagger#13435
- The Apple `container` driver to honor the privileged option by granting all capabilities (`--cap-add ALL`), so the engine can bind-mount `/etc/resolv.conf` on startup with `container` 1.0.0's reduced default capability set. by @eunomie in dagger/dagger#13460
- Dang directive argument resolution so `@cache` (and other directive) arguments resolve against the declaring type's file scope, fixing `FunctionCachePolicy not found in env` errors in multi-file modules. by @vito in dagger/dagger#13455
- The report-mode heartbeat so it waits for its goroutine to exit before final render, avoiding interleaved output. by @kpenfound in dagger/dagger#13425
- Nullable result unwrapping for SDK input so nullable values are passed correctly into SDK functions. by @shykes in dagger/dagger#13401

### What to do next?

- Read the [documentation](https://docs.dagger.io)
- Join our [Discord server](https://discord.gg/dagger-io)
- Follow us on [Twitter](https://twitter.com/dagger_io)

</pre>
  <p>View the full release notes at <a href="https://github.com/dagger/dagger/releases/tag/v0.21.7">https://github.com/dagger/dagger/releases/tag/v0.21.7</a>.</p>
</details>
<hr>

See merge request: Harmonybrew/homebrew-core!11763
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants