<?xml version="1.0" encoding="utf-8"?>
<feed xml:lang="en-us" xmlns="http://www.w3.org/2005/Atom"><title>Micah Stubbs's Weblog: debugging</title><link href="https://micahstubbs.ai/" rel="alternate"/><link href="https://micahstubbs.ai/tags/debugging.atom" rel="self"/><id>https://micahstubbs.ai/</id><updated>2026-01-25T22:35:31.034174+00:00</updated><author><name>Micah Stubbs</name></author><entry><title>Thread locks don't cross process boundaries</title><link href="https://simonwillison.net/2026/Jan/25/thread-locks-dont-cross-process-boundaries/#atom-tag" rel="alternate"/><published>2026-01-25T22:35:31.034174+00:00</published><updated>2026-01-25T22:35:31.034174+00:00</updated><id>https://simonwillison.net/2026/Jan/25/thread-locks-dont-cross-process-boundaries/#atom-tag</id><summary type="html">
    # Thread locks don't cross process boundaries

I've been building a voice-to-text daemon that transcribes speech and injects it into my terminal using [xdotool](https://github.com/jordansissel/xdotool). Today I hit a bug that took me way too long to diagnose. The symptoms were genuinely weird, which is why I'm writing this up.

The transcription was working perfectly. I could see the correct text in my logs. But what appeared on screen looked like this:

```
PAl efaosoet eard dt haa tf osohtoewrs  tthhaet csuhrorwesn tlhye
```

That was supposed to be "Please add a footer that shows the currently deployed..."

#### Finding the culprit

I used Jesse Vincent's `systematic-debugging` [skill](https://github.com/obra/superpowers/blob/main/skills/systematic-debugging/SKILL.md), which forced me to gather evidence before jumping to conclusions. The logs showed correct transcription. The code looked fine. My threading lock was in place.

Then I ran `ps aux | grep voice_input` and immediately saw the problem:

```
m  899547  python3 -m voice_input.daemon --claude
m  958207  python3 -m voice_input.daemon
```

Two daemon instances. I'd started one earlier and forgotten about it. Both were listening to the same microphone, both transcribing, both calling `xdotool type` at the same time.

#### Why the output was garbled

When two processes call `xdotool type` simultaneously, their keystrokes interleave character-by-character:

1. Daemon A sends `P`
2. Daemon B sends `l`
3. Daemon A sends `e`
4. Daemon B sends `e`
5. ...and so on

The result is alphabet soup. Both daemons were doing everything correctly in isolation. The bug only showed up because they were racing each other.

#### Why my threading lock didn't help

I had a lock in my injector class:

```python
class TextInjector:
    def __init__(self):
        self._injection_lock = threading.Lock()

    def inject(self, text):
        with self._injection_lock:
            subprocess.run(["xdotool", "type", text])
```

The problem: `threading.Lock()` only coordinates threads within a single Python process. It does nothing to prevent two separate processes from colliding.

This seems obvious once you think about it. But when you're staring at code that "has a lock" and wondering why there's still a race condition, it's easy to forget that locks don't cross process boundaries.

#### The fix: PID file locking

The standard Unix solution for daemon singletons is [`fcntl.flock()`](https://docs.python.org/3/library/fcntl.html#fcntl.flock):

```python
import fcntl
import os
from pathlib import Path

PID_FILE = Path("/tmp/voice-input-daemon.pid")

def _acquire_singleton_lock() -&gt; int:
    fd = os.open(str(PID_FILE), os.O_RDWR | os.O_CREAT, 0o644)

    try:
        fcntl.flock(fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
    except BlockingIOError:
        existing_pid = PID_FILE.read_text().strip()
        raise SingletonDaemonError(
            f"Another daemon is already running (PID {existing_pid}).\n"
            f"Stop it with: kill {existing_pid}"
        )

    os.write(fd, f"{os.getpid()}\n".encode())
    return fd  # Must keep fd open to maintain lock
```

A few things worth noting:

- `LOCK_EX` requests an exclusive lock. Only one process can hold it.
- `LOCK_NB` makes it non-blocking, so we fail immediately if someone else has the lock.
- You have to keep the file descriptor open. Close it and the lock releases.
- The kernel automatically releases the lock when your process exits, even if it crashes.

Now if I accidentally start a second daemon:

```
$ python -m voice_input.daemon
Error: Another daemon is already running (PID 899547).
Stop it with: kill 899547
```

#### What I learned

I keep coming back to the distinction between thread-level and process-level coordination. Any time you're building something that controls a system-wide resource (audio hardware, keyboard injection, a GPU) you need to think about what happens when multiple instances run simultaneously.

`ps aux | grep &lt;program&gt;` should probably be higher in my debugging checklist for daemons. It would have saved me an hour today.

I'm also a believer in helpful error messages. Including the existing PID means you can immediately run `kill 899547` instead of hunting around to figure out which process to stop.

The fix took five minutes once I understood the problem. Finding the problem took considerably longer.

    
        &lt;p&gt;Tags: &lt;a href="https://micahstubbs.net/tags/debugging"&gt;debugging&lt;/a&gt;, &lt;a href="https://micahstubbs.net/tags/linux"&gt;linux&lt;/a&gt;, &lt;a href="https://micahstubbs.net/tags/python"&gt;python&lt;/a&gt;, &lt;a href="https://micahstubbs.net/tags/concurrency"&gt;concurrency&lt;/a&gt;, &lt;a href="https://micahstubbs.net/tags/voice-input"&gt;voice-input&lt;/a&gt;&lt;/p&gt;
    

</summary><category term="debugging"/><category term="linux"/><category term="python"/><category term="concurrency"/><category term="voice-input"/></entry><entry><title>Systematic Debugging the Overnight OOM</title><link href="https://simonwillison.net/2026/Jan/24/oom-debugging-systematic-approach/#atom-tag" rel="alternate"/><published>2026-01-24T10:16:49+00:00</published><updated>2026-01-24T10:16:49+00:00</updated><id>https://simonwillison.net/2026/Jan/24/oom-debugging-systematic-approach/#atom-tag</id><summary type="html">
    #### Tracking down the OOM event
I woke up this morning to find my GNOME session had crashed overnight. Terminal sessions gone, browsers closed, had to log back in. The `journalctl` output told me why: an Out of Memory event that killed 48 processes at `00:56:33`.
My gut reaction was to blame Chrome or some runaway Node process, but this time I decided to actually look into it.
#### Systematic debugging with Claude
I asked [Claude](https://code.claude.com/docs/en/overview) to investigate using my `sd` short alias for [Jesse Vincent](https://metasocial.com/@jesse)'s excellent `systematic-debugging` [skill](https://github.com/obra/superpowers/blob/main/skills/systematic-debugging/SKILL.md), a four-phase debugging framework that goes:

1. Root Cause Investigation

2. Pattern Analysis

3. Hypothesis Testing

4. Implementation

That order matters. No guessing allowed.
```
please scan all claude code transcripts and the relevant system logs and develop three hypothesis, using the sd skill, as to what caused the OOM in the last ~6 hours or so
```
#### What got killed
Here's what `journalctl` showed:

14 orphaned `bd` processes stood out.
`bd` is the golang implementation of [beads](https://github.com/yegge/beads), a git-backed issue tracker I use with Claude Code. It spawns processes for triage, graph computations, and IPC. When Claude subagents invoke it, apparently these child processes weren't getting cleaned up.
The system had been running for 13+ days. 14 zombie beads processes built up over that time.

| Process | Count |
|---------|-------|
| `bd` | 14 |
| `zsh` | 11 |
| `http-server` | 5 |
| `python` | 4 |
| `claude` | 2 |
| `zoom` | 1 |

#### Three hypotheses

**Beads process accumulation** Each `bd` process holds memory for issue caching, graph operations (PageRank, betweenness), and IPC channels. 14 orphans over 13 days of uptime. Most likely cause.

**Claude transcript growth** Found transcript files at 491MB and 348MB. One session had 64 subagent files. Long-running sessions with large contexts might not free memory properly.

**http-server leaks** 5 orphaned `http-server` processes. Claude spawns these for HTML previews. When sessions crash, they persist.
#### The pattern underneath
All three point to the same thing: process lifecycle management failure.
When a parent process exits, children should get `SIGHUP` and terminate. But if they're detached or `nohup`'d, they become orphans with `PPID=1`. Without explicit cleanup, they stick around. Memory builds up. Eventually the OOM killer steps in.
The system has 62GB RAM and 80GB swap. Usage was at 42GB, not dangerous by itself. But multiple processes trying to allocate at once can still trigger the OOM killer.
#### Switching to beads_rust
The investigation led to a related change: migrating from `bd` to `br` ([beads_rust](https://github.com/Dicklesworthstone/beads_rust)) from [@doodlestein](https://x.com/doodlestein).
```
please replace bd with br ... in ~/.claude, all CLAUDE.mds, all claude skills, and everywhere else that `bd` is mentioned
```
The differences in `br`:

- Never auto-commits to git
- No background daemon processes
- You run `br sync --flush-only` explicitly when you want to sync

The golang `bd` had automatic background syncing and daemons. The Rust version makes everything explicit. Nothing runs unless you tell it to.

- `beads_rust`: You want a stable, minimal issue tracker that stays out of your way.
- `beads`: You want advanced features like Linear/Jira sync, RPC daemon, and automatic hooks.

Here's a [full comparison](https://github.com/Dicklesworthstone/beads_rust?tab=readme-ov-file#br-vs-original-beads-go):

| | `br` | `beads` |
|--------|------|---------|
| Language | Rust | Go |
| Lines of code | ~20,000 | ~276,000 |
| Git operations | Never (explicit) | Auto-commit, hooks |
| Storage | [SQLite](https://sqlite.org/) + [JSONL](https://jsonlines.org/) | [SQLite](https://sqlite.org/)/[Dolt](https://github.com/dolthub/dolt) |
| Background daemon | No | Yes |
| Hook installation | Manual | Automatic |
| Binary size | ~5-8 MB | ~30+ MB |
| Complexity | Focused | Feature-rich |

#### What I took away
The systematic debugging skill stopped me from doing what I normally would have done: blame Chrome, kill some processes, call it a day. Instead I got actual evidence pointing to 14 zombie beads processes that had built up over two weeks.
The fix wasn't just killing processes. It was figuring out why they accumulated and switching to a tool that handles process lifecycle better.
Full investigation notes: [oom-investigation-2026-01-24.md](https://gist.github.com/micahstubbs/2c885a9eb7596aaa051d809cbd1fcc21)


    
        &lt;p&gt;Tags: &lt;a href="https://micahstubbs.net/tags/claude-code"&gt;claude-code&lt;/a&gt;, &lt;a href="https://micahstubbs.net/tags/debugging"&gt;debugging&lt;/a&gt;, &lt;a href="https://micahstubbs.net/tags/oom"&gt;oom&lt;/a&gt;, &lt;a href="https://micahstubbs.net/tags/systematic-debugging"&gt;systematic-debugging&lt;/a&gt;, &lt;a href="https://micahstubbs.net/tags/linux"&gt;linux&lt;/a&gt;, &lt;a href="https://micahstubbs.net/tags/superpowers"&gt;superpowers&lt;/a&gt;, &lt;a href="https://micahstubbs.net/tags/beads"&gt;beads&lt;/a&gt;, &lt;a href="https://micahstubbs.net/tags/skills"&gt;skills&lt;/a&gt;, &lt;a href="https://micahstubbs.net/tags/rust"&gt;rust&lt;/a&gt;, &lt;a href="https://micahstubbs.net/tags/prompts"&gt;prompts&lt;/a&gt;&lt;/p&gt;
    

</summary><category term="claude-code"/><category term="debugging"/><category term="oom"/><category term="systematic-debugging"/><category term="linux"/><category term="superpowers"/><category term="beads"/><category term="skills"/><category term="rust"/><category term="prompts"/></entry></feed>