A shared build host ran four kinds of work at once: two production MySQL databases, a CI runner, and the build jobs of a coding agent. Each agent job got its own dev server on the box, tunnelled back so the agent could take screenshots of what it built. One afternoon the database started dying every minute. It took a while to see that the database was the victim, not the cause.

Key facts

  • 36 dev servers from finished agent jobs were still running, with about 8 GB of RAM between them.
  • The kernel's OOM killer picked the database: it was killed and restarted 421 times in six hours.
  • The CI runner was killed too. Its service exits 0 on OOM, so systemd did not restart it.
  • A mobile over-the-air release sat in the queue the whole time.
  • Earlier the same day the disk had filled (34.6 GB of build cache plus 30 days of binlogs) and the database was down for five hours.

The symptom: the database kept dying

The alerts all said the same thing: the database is down. Then it was up. Then it was down again. The service manager restarted it each time, it came back, served a few queries, and went away again within a minute.

At the same time a mobile release that should have gone out over the air sat queued. Both were the same problem.

The wrong first suspects

When a database keeps dying, people look at the database. The first guess was its own settings: buffer sizes too large for the box, a bad query, a setting changed by the morning's work. None of that had changed.

The second guess was the disk, and it was a fair guess. Earlier that day the same box had filled its disk. A docker build cache had grown to 34.6 GB, and the database kept 30 days of binlogs. With no space left the database stopped, and it was down for five hours. That was fixed by pruning the build cache and setting binlog_expire_logs_seconds to 3 days. So when the database fell over again in the afternoon, it was easy to think the disk was back. It was not. There was plenty of space this time.

Nobody looked at the dev servers first. They were small, they belonged to jobs that had already finished, and the symptom pointed somewhere else.

What three commands showed

The answer came from looking at memory instead of at the database.

  • free -m showed almost no free memory and swap in use.
  • ps --sort=-rss lists processes by resident memory, largest first. The database was near the top, as it always is. Right under it was a long run of dev server processes, one after another.
  • pgrep -fc <dev server> counts them. It returned 36.

The kernel log filled in the rest. Each time memory ran out, the OOM killer chose the database, because it was the biggest single process. The service manager restarted it, it grew back to its normal size, and the kernel killed it again. The dev servers, each one small, were never chosen. The CI runner was killed on one of those passes, and that is why the release sat queued.

Why agent-started processes leak

A person who starts a dev server usually stops it. An agent works differently. The job starts a dev server in an early step so it can take screenshots later. The job finishes. The tab or session that ran it closes. The dev server keeps running, because nothing in the last step of the job stops what the first step started.

At that point the process has no owner. No job is waiting on it, no person remembers it, and no timer will end it. One such server is a few hundred megabytes and nobody notices. After a few days of normal work there were 36 of them, and together they held about 8 GB.

Recovery

The fix on the day was blunt: pkill -f <build dir>, which kills every process whose command line contains the build directory. It worked, and it also killed the ssh shell that ran it, because that shell's own command line held the same path. After that the CI runner was started again by hand, the queued release went out, and the cache and binlog changes from the morning stayed in place.

A cleanup you can run

A safer version matches on each process's working directory, not on its command line, and only stops what is old and belongs to a finished job. The paths and the .done marker are examples; use whatever your jobs really write.

BUILD=/srv/build
MAX_AGE=$((6 * 3600))   # six hours
for d in /proc/[0-9]*; do
  pid=${d#/proc/}
  dir=$(readlink "$d/cwd" 2>/dev/null) || continue
  case "$dir" in "$BUILD"/*) ;; *) continue ;; esac
  job=${dir#"$BUILD"/}; job=${job%%/*}
  age=$(ps -o etimes= -p "$pid" | tr -d ' ')
  if [ "${age:-0}" -gt "$MAX_AGE" ] && [ -f "$BUILD/$job/.done" ]; then
    kill "$pid"
  fi
done

Matching on the working directory means the shell that runs the cleanup is not caught just because a path appears in its arguments. That was the mistake in the recovery above.

Four rules

  • Every process an agent starts gets an owner and a stop condition, written in the same step that starts it. The stop condition can be the end of the job, a time limit, or a systemd unit with a lifetime such as RuntimeMaxSec. If the start and the stop are in different steps, one day the second step will not run.
  • Production databases do not share a box with unbounded build work. If they must, protect the database: give it a cgroup memory floor (MemoryMin) or a lower oom_score_adj than the build jobs, so the kernel picks a build job first.
  • A service that exits 0 on OOM needs Restart=always, not Restart=on-failure. To systemd an exit code of 0 is a clean stop, so on-failure does nothing. The runner stayed down for that reason alone.
  • A weekly line that counts long-running processes per directory catches the next leak before the kernel does. A count of 36 is easy to read in a report.

Common questions

Why did the kernel kill the database and not the dev servers?

The OOM killer picks the process that frees the most memory, so it scores mostly by size. The database was the biggest single process on the box. Each dev server was small, and 36 small ones never looked like the problem to the kernel.

Is a bigger box the fix?

No. A leak with no stop condition fills any size of box. More memory only buys days before the same night happens again.