I Put a Container Exec Behind Every SQL Statement, and Our Tests Took Down a Shared Podman Socket

This is about a mistake I made in some test code. The code looked tidy and passed review. Run across a fleet of shared machines, it overloaded the container runtime that other people’s sessions needed to start.

What I built

We have shell table tests for a database bootstrap process. They start several PostgreSQL containers plus one container that stands in for the pod the bootstrap job runs in. All the setup goes through a shared fixture with a few small helper functions:

  • run SQL as the superuser
  • run SQL as a given role with its password
  • write a file (or a secret) into a container
  • run the script under test inside the pod

The tests also wait for each database to become ready before they start.

Every one of those helpers was a single podman exec, and so was each readiness probe. The readiness wait ran pg_isready through an exec every half second, per server, up to 120 times each.

The first version of the test had one container and about six exec sites, and it was fine. Later I rewrote it to cover a database split. The rewrite went to five servers and several times more assertions, most of them inside loops: over service roles, connection pools, schemas, servers, and a table of “should reach / should be refused” cases run twice. I moved every runtime call behind the helpers and never changed what the helpers did. A single pass of the main test came to several hundred execs. The companion test was around a hundred.

Why that mattered

I treated podman exec as if it were a cheap local fork. It wasn’t. The tests run inside a devcontainer, where podman is a remote client of the host’s podman API socket. Each exec is several HTTP round trips to one socket-activated service on the host: create the exec, start it with an attached stream, then inspect it for the exit code. Every workspace on the machine shares that service.

These tests also run as part of the standard gate that every development session runs before it can finish. The gate runs up to eight tests in parallel, and it’s capped at eight on purpose, “to bound the load on a shared machine”. That cap applies to one gate run in one workspace, though, and our machines each hold well over a dozen workspaces. A sibling repository had a copy of the same fixture design and ran it in its own gates too. Nobody had ever added up the total.

What happened

On one machine the container exec rate reached well over a thousand every five minutes and stayed high for more than an hour. The host’s podman service kept crashing and restarting, many times a minute, and each restart left stray helper processes behind. The load average went into the high thirties.

A separate orchestration service on the same machines checks container state with a short deadline before it starts a session. Those checks began timing out repeatedly. Session starts returned 502s, and users on two machines got sessions with no prompt and no token. Leaked test containers from an unrelated cleanup bug added to the load.

What I got wrong

I picked a mechanism for each call and never priced a whole run. The helpers were the one place every runtime call went through, which made them the obvious place to ask how often a line runs per pass and how many workspaces multiply that. I wrote “one API round trip per assertion” into a test that was about to grow to hundreds of assertions, and I never noticed I had done it.

I didn’t look at the unit cost when I multiplied the test. Six execs was tolerable. The rewrite was the moment to check what each call cost, because I was multiplying the call count by five servers and several times the assertions, and I skipped that check.

I polled the shared socket instead of polling inside the container. A readiness loop whose body is a runtime call polls the host daemon. Five servers at 120 probes each is up to 600 execs before the test has asserted anything.

I ignored the reasoning the gate already spelled out. The gate limits its own parallelism because the machine is shared. A test inside that gate that makes hundreds of calls to the shared runtime defeats the limit, and the reason for the limit applies to the test just as much.

What I should have done

The containers already have psql, and the pod already has the scripts and environment. Either of these shapes brings a run down to a handful of execs:

  1. One long-running session per server. Open one psql per server, once, as a coprocess inside a single exec -i. Send it statements over stdin and read the answers back. Role-specific checks use SET ROLE or a \connect with the role’s credentials instead of a new exec.
  2. Run the whole test inside the container. Copy the test into the pod and run it there once. The hundreds of psql calls become local forks inside the container, and the host socket sees one exec. The pod already exists to be the job’s environment.

Either way, the readiness wait becomes one exec per server running something like timeout 60 sh -c 'until pg_isready -q; do sleep 0.5; done' inside the container, and it reports once.

What I’m changing

  • Counting runtime calls in review. For any test that talks to a container runtime, I’ll read the helpers and the loops that call them, count calls per pass, and multiply by the number of workspaces a machine holds. A few dozen per run is worth raising. Several hundred means the test is designed wrong.
  • Treating an exec inside a helper as a warning sign. A function named for what it does, like “run as superuser” or “write secret”, that hides a runtime round trip is just waiting to be called from a loop.
  • An automated check. First, a grep that lists every podman exec or docker exec site in tests and scripts, along with the loops around it. Second, a run of each such test under a stub runtime that counts calls and fails the test if it goes over a stated budget. Our other shell tests already run under stubs, so this fits the pattern we have.
  • Remembering that inside a devcontainer, the runtime is remote. podman and docker there are clients of a daemon that every workspace on the host shares. Every exec costs a network round trip on a shared resource.

The broader lesson

We had already learned two related lessons. One was that every external call should go through a single interface so the routing is decided in one place. The other was that any bound sized for one unit has to be checked against the whole fleet. My test followed the first: it had the single interface. It failed the second, because I never asked what the cost behind that interface added up to across every workspace.

Having a single interface for these calls doesn’t help much unless you also look at what each call costs. That interface is the right place to weigh the cost, so that’s where I’ll check it from now on.

Advanced search: filter by model or tag