Two Bitbucket Pipelines runners, one ID, and a git clone error for a directory that wasn’t there
Fabian Blechschmidt
A pipeline run failed with this:
+ git clone --branch='develop' --depth 50 https://x-token-auth:$REPOSITORY_OAUTH_ACCESS_TOKEN@bitbucket.org/$BITBUCKET_REPO_FULL_NAME.git $BUILD_DIR
fatal: destination path '/home/winkelwagen/atlassian-bitbucket-pipelines-runner-6/bin/../temp/d7951174-444d-5dca-840b-7ab86ad13ed9/build' already exists and is not an empty directory.
Fine, I thought, some leftover build directory, I’ll just rm -rf it and re-run. Except the directory wasn’t there. I checked. Twice. Git was very sure a non-empty directory existed at that exact path a few seconds ago, and the filesystem was equally sure it had never heard of it. That contradiction is the whole story.
The setup: a shell runner kept alive with screen
This particular runner isn’t Bitbucket’s Docker-based runner — it’s a shell runner (runs-on: [self.hosted, linux.shell]), running directly on the host with no container around each step. We use these on a couple of pipelines specifically because a Docker-in-Docker step on this same host doesn’t share the host’s image cache — the fix for that was moving the step to a shell runner so it hits the real host daemon directly. Good tradeoff for a private repo, no external contributors.
A shell runner isn’t a systemd service here, it’s a long-running foreground process kept alive the cheap way: a screen session plus a small starter script that (re)launches it. Simple, has worked for ages, right up until it didn’t.
What the starter script actually killed
The starter script’s job, before launching a fresh runner, is to make sure no old one is still running. It killed the screen session. It did not kill the runner process itself. The runner had been started inside that screen session, backgrounded — so once screen died, the process didn’t die with it, it just got reparented and kept right on running, invisible to screen -ls. Ask „is there a screen session for this runner?“ and the honest answer was no. Ask „is the runner still running?“ and the honest answer was also yes. The script only ever asked the first question.
So the script, seeing no session, did exactly what it was told: started a second runner process from the same install directory, atlassian-bitbucket-pipelines-runner-6 — same registration, same runner UUID, same OAuth identity. Two live OS processes, both presenting themselves to Bitbucket as the exact same runner.
The part that surprised me: Bitbucket didn’t care
I expected Atlassian’s backend to reject the second connection, or at least flag it — „this runner ID is already connected elsewhere.“ It doesn’t seem to. Both processes stayed up, both were apparently eligible to be handed work, with nothing in the Bitbucket UI hinting that „one“ runner was actually two. If there’s a dedup check on their end, I couldn’t find any sign of it firing here.
And because it’s a shell runner — no container, no per-run filesystem isolation — both processes share the literal same temp/ directory on the same disk, since they were both launched from the same install path. Normally that’s harmless: one runner, one job at a time, one temp dir in use. With two processes answering to the same identity, that stops being true.
Why the directory „existed“ and then didn’t
This is the part I had to sit with for a minute. My best working theory, and I’ll say plainly it’s a theory, not something I have Atlassian’s own logs to confirm: whatever handed this job to „the runner“ ended up handing it — or letting it be claimed — by both processes close enough in time that they raced for the same temp/<uuid>/build path. One process got there first and started cloning; the second’s git clone hit a directory that already existed and bailed out, exactly as git is supposed to. Classic check-then-act race, just not one I expected to find in „there’s only one runner, surely.“
By the time I actually looked at the filesystem — maybe ten, twenty seconds later — the first process had already finished its step and its own cleanup had removed the temp directory. So git, a moment earlier, was completely correct that the path existed. I was also completely correct that it didn’t. We were just looking at two different moments, on a host secretly running two copies of the same worker.
The actual fix
Nothing clever, which is a little embarrassing given how long it took to notice: stop treating „no screen session“ as „no runner.“ The starter script needs to track the runner’s own PID (or grep for its actual process, not the terminal multiplexer wrapped around it) and kill that before starting a new one. screen was never the thing that needed to die — it was just where I was looking.
TL;DR
A starter script killed a screen session but not the self-hosted Bitbucket shell runner running inside it, so a second runner process got started under the same runner ID. Bitbucket’s backend didn’t seem to notice or reject the duplicate connection, and since it’s a shell runner with no container isolation, both processes shared the same host temp directory. When both raced to claim a build in that shared path, one `git clone` failed with „directory already exists“ — and by the time I went to check, the sibling process’s cleanup had already removed it, making a very real race condition look like a filesystem lie. Fix: kill the runner process itself, not the terminal session wrapping it.
If you’re running self-hosted shell runners kept alive with screen/tmux plus a home-grown starter script, worth checking right now whether your „kill old runner“ step actually kills the runner. Drop me an email if Atlassian’s ever confirmed to you whether runner-ID uniqueness is enforced anywhere on their end — I couldn’t find anything saying it is.
Based on a real production incident on this shop’s self-hosted Bitbucket Pipelines infrastructure (runner install path atlassian-bitbucket-pipelines-runner-6). The clone error and path shown are the actual output, unedited. The „two processes raced for the same job“ explanation is the working theory reached by reasoning through the observed symptoms (duplicate process discovered on the host, no dedup rejection visible from Bitbucket, shared temp path being the only way the collision makes sense for a shell runner) — not something confirmed against Atlassian’s own server-side logs or documentation, since neither is available to us.