Skip to content

refactor(BA-5862): Retry once on stale aiodocker connections after dockerd restart#11235

Open
rapsealk wants to merge 3 commits into
perf/11218-docker-client-poolfrom
perf/11233-aiodocker-stale-retry
Open

refactor(BA-5862): Retry once on stale aiodocker connections after dockerd restart#11235
rapsealk wants to merge 3 commits into
perf/11218-docker-client-poolfrom
perf/11233-aiodocker-stale-retry

Conversation

@rapsealk
Copy link
Copy Markdown
Member

@rapsealk rapsealk commented Apr 22, 2026

Closes #11233 (BA-5862)
Refs #11216

Summary

  • Add _retry_on_stale_connection helper that catches (ServerDisconnectedError, ClientOSError) once, logs a warning, and retries.
  • Explicitly excludes ServerTimeoutError — even though it inherits from ClientConnectionError, it represents response-too-slow, not a stale socket. Legitimate long-running images.push / images.pull timeouts now propagate normally.
  • Apply to retry-safe per-op aiodocker calls on DockerAgent (image / container / network inspect / list / delete / create / start / stop — 26 sites across 13 methods).
  • Explicitly do NOT wrap: monitor_docker_events (own reconnect loop), DockerStatsStreamer (bounded backoff), __ainit__ bootstrap probes (boot-failures should be loud), streaming log readers, commit and other non-idempotent verbs.

Why

After systemctl restart docker, the shared aiohttp session's pooled keepalive sockets are stale. aiohttp reconnects on the next call, but the first one-shot failure surfaces as spurious user-facing errors (purge_images, check_image, etc.). See #11233.

Stacked on

#11226 (perf/11218-docker-client-pool). Rebase to main once #11226 merges.

Test plan

  • pants test tests/component/agent/docker:: passes — 5 tests: retry-once-then-succeeds, propagates persistent failure, non-matching errors not retried, ServerTimeoutError NOT retried (regression guard), container.create retry path via resolve_image_distro.
  • docker restart; run bai admin image list from the CLI; verify it succeeds on the first call post-restart (would fail pre-this-PR).
  • Run a deliberately-slow images.pull past its timeout; verify it propagates without silent retry.

…erd restart

The shared aiodocker client (introduced in #11226) pools keepalive sockets
in its aiohttp.ClientSession. After a dockerd restart, the first post-
restart call can pick a stale socket and fail with ClientConnectionError
or ServerDisconnectedError; aiohttp reconnects on the next call. A thin
once-retry wrapper absorbs that one-shot failure so user-visible
operations (check_image, scan_images, purge_images, etc.) don't spuriously
fail after dockerd bounces.

- Wraps retry-safe aiodocker calls via _retry_on_stale_connection.
- Leaves monitor_docker_events (own reconnect loop) and the
  DockerStatsStreamer (bounded backoff) untouched.
- Non-idempotent / retry-risky calls (commit, etc.) are explicitly not
  wrapped.

Closes #11233
Refs #11216
Refs #11226

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@rapsealk rapsealk added this to the 26.5 milestone Apr 22, 2026
@github-actions github-actions Bot added size:L 100~500 LoC comp:agent Related to Agent component labels Apr 22, 2026
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
The previous catch tuple (ClientConnectionError, ServerDisconnectedError)
subsumed ServerTimeoutError via ClientConnectionError. A long-running
images.push / images.pull that blew its timeout would be silently retried
and emit a misleading "stale aiodocker connection" warning.

Narrow to (ServerDisconnectedError, ClientOSError), which covers the
real pooled-socket-dead cases (dockerd restart / keepalive reset)
without catching legitimate timeouts. ClientConnectorError (dockerd
down) and ClientSSLError also correctly fall through.

Add a regression test asserting ServerTimeoutError is NOT retried,
and a test exercising container.create's retry path.

Refs #11233
Refs #11235

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@rapsealk rapsealk requested review from a team and achimnol April 22, 2026 07:54
@rapsealk rapsealk changed the title refactor(agent): Retry once on stale aiodocker connections after dockerd restart refactor(BA-5862): Retry once on stale aiodocker connections after dockerd restart Apr 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp:agent Related to Agent component size:L 100~500 LoC

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Retry once on stale-connection errors from shared aiodocker client after dockerd restart

1 participant