How a Podman Bug Almost Made Me Roast a Perfectly Good Hash Table
Published: November 27, 2026
How a Podman Bug Almost Made Me Trash a Perfectly Good Hash Table
I was ready to write a takedown of libcuckoo. The benchmarks were damning — negative scaling on isolated CPUs, 3 threads delivering 62% of single-thread throughput, the whole "concurrent hash table" premise falling apart under proper measurement conditions. I had the perf counters, the root cause analysis, the comparison against absl::flat_hash_map. The narrative was clean: academic paper promises concurrency, real hardware disagrees.
Then I ran the same benchmark outside the container and got 2.55x scaling at 3 threads. Perfect linear scaling confirmed by perf stat showing identical IPC and cache-miss rates across thread counts.
The bug wasn't in libcuckoo. It was in how pthread_setaffinity_np interacts with podman's --cpuset-cpus flag. And it's the kind of bug that would survive code review, pass CI, and silently corrupt every benchmark result without any error message.
The Setup That Bit Us
The test machine is an AMD Ryzen 9 3900 with CPUs 6-11 isolated via isolcpus. The benchmark needed to run inside a podman container because the host lacked abseil-cpp headers. So:
sudo podman run --rm \
--cpuset-cpus 9-11 \
-v /tmp/bench:/bench:z \
challenge-sandbox \
/bench/bench_compare --base-cpu 0 --num-cpus 3
The benchmark creates 3 threads and pins each one:
// Thread t pins itself to core base_cpu + t
cpu_set_t cpuset;
CPU_ZERO(&cpuset);
CPU_SET(g_base_cpu + t, &cpuset);
pthread_setaffinity_np(
pthread_self(), sizeof(cpuset), &cpuset);
With --base-cpu 0, thread 0 pins to CPU 0, thread 1 to CPU 1, thread 2 to CPU 2. Seemed logical — the container has 3 CPUs, so use 0, 1, 2.
The Bug
Podman's --cpuset-cpus 9-11 does not remap CPU numbers. Inside the container, the kernel still sees CPUs 9, 10, and 11. You can verify:
$ cat /proc/self/status | grep Cpus_allowed_list
Cpus_allowed_list: 9-11
$ taskset -p -c 0 $$
taskset: failed to set pid 1's affinity: Invalid argument
$ taskset -p -c 9 $$
pid 1's new affinity list: 9
CPU 0 doesn't exist in the container's cpuset. The pthread_setaffinity_np call to pin to CPU 0 returns EINVAL. Same for CPU 1 and CPU 2. Our code never checked the return value.
So all three threads ran with the container's default affinity: allowed on CPUs 9-11, pinned to none of them. The kernel scheduler decided where each thread ran, migrated them freely, and nproc inside the container reported 1 (due to cgroup CPU quota). Three threads timesharing what the kernel treated as a single CPU's worth of compute.
The "negative scaling" was three threads fighting over one effective CPU. Not spinlock contention. Not cache-line bouncing. Just Linux scheduling on a misconfigured cpuset.
The Fix
Pass the real host CPU numbers:
/bench/bench_compare --base-cpu 9 --num-cpus 3
Or better, check the return value:
int rc = pthread_setaffinity_np(
pthread_self(), sizeof(cpuset), &cpuset);
if (rc != 0) {
fprintf(stderr, "FATAL: can't pin thread %d "
"to CPU %d: %s\n", t, cpu, strerror(rc));
abort();
}
If we'd had that check, the benchmark would have crashed immediately instead of silently producing garbage data.
The Real Numbers
Once we ran bare-metal on the same isolated CPUs with correct pinning, everything changed.
Single-threaded (pinned to CPU 9):
| Container | Insert (Mop/s) | Lookup (Mop/s) |
|---|---|---|
std::unordered_map |
6.53 | 13.32 |
libcuckoo::cuckoohash_map |
4.12 | 10.77 |
Libcuckoo is slower single-threaded — 0.81x of unordered_map on lookups — due to the per-operation spinlock overhead even without contention. An uncontended test_and_set plus clear costs 15-25 ns of pipeline disruption. Not great, but not the 2-3x gap the container results suggested.
Lookup latency distribution (pinned to CPU 9):
| Container | P50 (ns) | P99 (ns) | P99.9 (ns) | Max (ns) |
|---|---|---|---|---|
std::unordered_map |
210 | 671 | 862 | 1,412 |
libcuckoo |
150 | 441 | 501 | 18,575 |
Libcuckoo has 29% better P50 and 34% better P99 than unordered_map. The bounded two-bucket probing gives it a much tighter distribution than chaining. The max latency is worse (18μs vs 1.4μs), likely from a rare cuckoo displacement or lazy rehash hitting the read path.
Multi-threaded scaling (80% read, 20% write, each thread pinned to its own isolated core):
| Container | 1T (Mop/s) | 2T | 3T | 3T Scaling |
|---|---|---|---|---|
| libcuckoo | 5.29 | 10.26 | 14.15 | 2.55x |
unordered_map+rwlock |
5.70 | 1.43 | 1.40 | 0.25x |
Libcuckoo scales to 2.55x at 3 threads. That's 85% of linear. The striped spinlocks work — 65,536 lock stripes across 500K buckets means the probability of two threads hitting the same lock with random keys is about 1/65,536 per operation. Low enough that real contention is rare.
Meanwhile unordered_map behind a shared_mutex collapses to 0.25x under the 20% write load. A single writer blocks all readers, and with 20% writes, writer starvation or reader blocking dominates.
We confirmed this with perf stat on a diagnostic benchmark. 1 thread vs 3 threads on 100% reads:
| Metric | 1T | 3T |
|---|---|---|
| Throughput | 14.0 Mop/s | 41.8 Mop/s (2.99x) |
| IPC | 0.95 | 0.96 |
| L1 miss rate | 4.23% | 4.13% |
| Cache-miss rate | 51.83% | 51.88% |
| Speculative locks | 25.3M | 25.8M |
Nearly identical counters. Same IPC, same miss rates, same lock counts. The 3-thread run uses the same total resources in 1/3 the wall time. No cache-line bouncing, no coherence traffic, no false sharing. The striped spinlocks are doing their job.
The Podman Pitfall
This is worth dwelling on because it's a trap that's easy to fall into and hard to detect.
Podman (and Docker) --cpuset-cpus does not create a virtual CPU namespace. Unlike PID and network namespaces, CPU numbering is passthrough. If the host has CPUs 0-17 and you restrict the container to CPUs 9-11, the process inside the container sees CPUs 9-11 in its affinity mask. CPU 0 still "exists" in the kernel's numbering but is forbidden by the cgroup.
What happens when you call sched_setaffinity with a CPU not in the cgroup's cpuset.cpus:
- The syscall returns EINVAL
- The thread's affinity remains unchanged (the cgroup default)
- No signal, no abort, no log message — unless you check the return value
Most benchmark harnesses don't check. Google Benchmark doesn't. Our code didn't. The libcuckoo universal_benchmark doesn't. If your CI pipeline runs benchmarks in containers on isolated CPUs, and the benchmark does CPU pinning, the pinning is probably broken.
The reliable pattern for containerized benchmarks on isolated CPUs:
cpu_set_t allowed;
sched_getaffinity(0, sizeof(allowed), &allowed);
int cpus[CPU_SETSIZE], ncpus = 0;
for (int i = 0; i < CPU_SETSIZE; ++i)
if (CPU_ISSET(i, &allowed))
cpus[ncpus++] = i;
// Now pin thread t to cpus[t % ncpus]
Read the allowed set from the kernel, then index into it. Never assume CPU numbering starts at 0.
What About absl::flat_hash_map?
We only had abseil inside the container, so our 3-way comparison numbers (including flat_hash_map) came from the bugged container runs. Those single-threaded numbers are still valid — all threads ran on the same core, so single-threaded results are just "pinned to one core in the cpuset," which is correct. The container flat_hash_map lookup number of 31.86 Mop/s vs libcuckoo's 4.96 Mop/s is genuine — but the libcuckoo number is depressed because the container's nproc reporting 1 may affect the library's internal sizing heuristics.
The multi-threaded comparison between flat_hash_map+rwlock and libcuckoo from the container is invalid and should be discarded. We'd need to install abseil on the host or fix the container pinning to get correct numbers.
The Code Issues Are Still Real
Independent of the performance story, the code audit findings from clang-tidy and -Wall -Wextra -Wpedantic stand:
106 compiler warnings. The member initialization order mismatch fires 90 times. The spinlock was changed from the correct memory_order_acquire to memory_order_acq_rel in a 2016 Christmas commit with flawed reasoning. CopyableAtomic doesn't handle self-assignment. aligned_storage is deprecated in C++23. The benchmark starts timing before creating threads. All of these are real PRs worth submitting.
The Lesson
The article I almost wrote — "libcuckoo shows negative scaling on isolated CPUs" — was wrong. Spectacularly wrong. The data was real, the analysis was detailed, the root cause explanation was plausible, and every word of it was garbage because pthread_setaffinity_np(pthread_self(), sizeof(cpuset), &cpuset) returned EINVAL and we didn't check.
A benchmark that doesn't verify its own preconditions isn't a benchmark. It's a random number generator with a nice table formatter. We measured scheduling overhead and called it spinlock contention. We observed context-switch delays and attributed them to cache-line bouncing. We had a complete theory — MESI protocol, false sharing, lock-stripe collisions — that explained data which had nothing to do with what we thought we were measuring.
The fix was two lines of code: check the return value, abort if it fails. In the hierarchy of bugs, "didn't check the return value of a syscall" is the first thing you learn and the last thing you actually do.
Libcuckoo scales fine. The spinlocks work. The paper's concurrency design holds up on modern hardware. The only thing broken was our benchmark setup — and our certainty that we'd gotten it right.
Review quiz
Five questions about this article. Get four right and it counts as read. Review quizzes come with a membership.
Log in See membership
Comments
No comments yet. Be the first to share your thoughts.