DEV Community

AI agents on a production bug: two wrong leads, then 8 crashes out of 8

How "clean" became a plan

Before the outage, there was a plan, and that plan came out of a conversation. To follow it, you first need to know who I was talking to.

I did this work with Claude Code, Anthropic's tool where an AI runs commands by itself. An agent is an AI that works this way: it runs something, reads the result, then decides what comes next.

The main session, the one I talk to, can launch subagents. Each one gets a written brief, works on its own and sends back a report.

That day, two models shared the roles: Fable, the more expensive of the two, writes the plans and runs the hard analyses. Sonnet executes: it writes the code and the tests from the plan. The main session also ran on Sonnet.

I run a server shared by about thirty members. Each of them runs their own rtorrent there, a command-line BitTorrent client: each one is called an instance, one process per member.

The server runs about thirty instances, all on the same program, which I wanted to move from version 0.9.8 to 0.16.28. My instruction to the AI fit in one sentence: "as fast as possible while doing it clean".

"Clean" means nothing to a machine. Instead of rushing in, the AI first sent questions back: how many instances, who owns them, what must never be lost, and how long a member can stay offline. Those answers, it could not read on the server.

The instances belong to the members, not to me. What must never happen is a member losing their list of torrents, or rtorrent rechecking all their files, hours of disk work for nothing. A few minutes of downtime, on the other hand, nobody will notice.

It turned those answers into concrete rules:

  • one instance at a time, so a mistake only hits one member
  • a backup before each switch, the moment an instance moves to the new version
  • a way back ready in a single command
  • a check script after each switch

It added an idea I didn't have: try the way back once, cold, before needing it.

The split was clear. I knew what must not break, it knew how to check it.

About twenty instances went through this way, and for each one the check script said all was well. But it checked the instance right after it started. Neither of us had asked how long an instance had to stay up to count as a success.

The answer came on its own: one to three minutes after starting, some instances began to die, without a word.

On Thursday, November 19, I handled a production bug with AI agents, from the outage to the fix.

After an rtorrent upgrade, instances died one to three minutes after starting. They left no message, no crash file, nothing in the logs.

The bug is not the subject of this article, it is where it comes from. The subject is how to work with agents on a machine where other people keep their files: where the AI was wrong, where I was wrong, and what settled it in the end.

Two wrong leads, then one stubborn sentence

When a program dies without a message, there is nothing to read, so you guess the cause. The AI came up with two causes, and both were wrong. What teaches the most is how it handled them: the first with method, the second by acting straight on production.

First lead: my disk regulator, a home-made service that slows instances down when the disk can't keep up. The AI first checked whether it had stepped in when the crashes happened. It had applied nothing, so the AI dropped the lead. Nothing to object to so far.

Second lead: my login session, the group of programs the system ties to my connection to the server. The AI noticed a coincidence: the instances still alive all ran inside that session. It drew an explanation from it. When a login session closes, the system takes down the programs started from it. So the dead instances must have come from another session, closed in the meantime.

It wrote itself that this was "a serious hypothesis, not a certainty". Then it went on as if it were a certainty, without checking anything and without asking me:

  • it restarted the dead instances in a separate login session
  • it changed the launch script in production, the one that starts every member's instance

The restarted instances died again, and it was always the same ones, while the others stayed stable. None of these actions looked for the error message. A coincidence had just modified a server shared by about thirty people.

That is when I typed the sentence that changed everything: "There has to be an error message going somewhere, in the rtorrent logs, the system logs or elsewhere. It doesn't just stop without saying anything."

This sentence is a method, the one an experienced sysadmin applies without thinking. I typed it to put the AI back on that track. It comes in three steps:

  1. A program never dies in silence. It always leaves a trace: an error message, an exit code, a signal recorded by the system.
  2. If you see nothing, the way you are looking is broken. Before any theory, fix the observation.
  3. Hypotheses come after the message, not before. A coincidence like "the survivors all live in my login session" stays a lead until a trace confirms it.

The AI was doing the opposite: it started from coincidences to build an explanation.

Once redirected, it asked the right question: why can't we see anything? Every program has a channel set aside for its failure messages, the error output. But the instances ran under screen, a program that keeps a terminal open in the background, and screen shows that channel in a terminal nobody looks at. When the instance dies, that terminal closes with it, and the message is gone before anyone can read it.

To fix the observation, it restarted four instances under systemd, the program that starts and watches the server's services. With systemd-run, each one becomes a temporary service, and systemd records everything it says in its journal, error output included.

A little over a minute later, the journal showed the sentence that screen was hiding:

terminate called after throwing an instance of 'torrent::internal_error' what():PollInternal::modify(): epoll_ctl(DEL) error for event: name:curl_socket fd:69 : Bad file descriptor
rt-diag3-<member>.service: Main process exited, code=killed, status=6/ABRT

The message was finally there. It still had to be understood.

A bug outside my field

That message meant nothing to me. I build applications, I never go down to the kernel or the network libraries. The AI is what taught me: a subagent, whose work I describe further down, took the message apart, then Claude explained it to me step by step. Here is that explanation, as I ended up understanding it.

Three words are enough to start:

  • the kernel: the core of the system, which handles files and the network for every program
  • a descriptor: the number the kernel gives to each open file or connection, like the 69 in the message
  • epoll: a watch list kept by the kernel, where rtorrent registers its connections to be woken up as soon as one of them receives data

That leaves who opens those connections. rtorrent must regularly check in with a tracker, the server that puts in touch the people sharing the same file. That message, the announce, basically says "I share this file, who else does?". It goes over HTTP, and rtorrent does not speak HTTP itself: it hands that job to libcurl, a ready-made library.

So each announce opens a connection, which rtorrent registers in epoll for as long as it lives. But libcurl is the one that closes it. To keep its list exact, rtorrent therefore asks libcurl to warn it at every close. It hands libcurl its own close function (the CURLOPT_CLOSESOCKETFUNCTION option), which libcurl must call every time it closes a connection.

The trap goes off with a dead tracker. It does not even answer the first packet of a connection, the SYN, the equivalent of a "hello". Here is the chain, in order:

Six steps.

  1. rtorrent opens connection 69 through libcurl and registers it in epoll.
  2. The tracker does not answer the SYN.
  3. Sixty seconds later, libcurl closes the connection without calling rtorrent's function.
  4. The kernel removes it from epoll.
  5. libcurl then asks rtorrent to remove it.
  6. rtorrent asks the kernel to remove a number that is already closed, gets an error and stops.
  • rtorrent ① opens connection no. 69 through libcurl, registers it in epoll
  • tracker ② does not answer no reply to the SYN
  • libcurl ③ gives up after 60 s closes 69 without calling rtorrent
  • kernel ④ removes 69 from epoll the number is closed, rtorrent can't tell
  • libcurl ⑤ then asks for the removal "remove 69 from your list"
  • rtorrent ⑥ removes 69 from epoll: error number already closed, rtorrent stops

Diagram made for this article, from the analysis report.

Step ③ is the bug: libcurl closes a connection that never got established without telling rtorrent. rtorrent, for its part, does nothing wrong. It trusts what libcurl's documentation promises: to be warned of every close. And its code refuses any epoll error, because it would rather stop than carry on with a wrong list.

The crash comes 60 seconds after the announce, the time it takes libcurl to give up: that timeout is set in rtorrent's code. So an instance whose trackers all answer never crashes. The mystery from the start finally made sense: some instances held and others did not, depending on their trackers. The one I later used as a guinea pig had six dead ones.

With this mechanism in mind, I could finally follow the discussion. The AI did not hand me a solution. It put four options on the table, with what each one costs:

  • Go back to the old version, 0.9.8. Safe and immediate, but we give up the upgrade.
  • Tune the kernel so it gives up sooner on unanswered connections (tcp_syn_retries). No code to write, but nothing guarantees it is enough.
  • Rebuild libcurl with the line the curl authors have already fixed. It cures the cause, but it touches a library the whole system depends on.
  • Fix libtorrent, rtorrent's library, so it absorbs the silent close of step ③. The change stays inside rtorrent, and we remove it the day Debian, the server's system, ships a fixed libcurl.

That was its recommendation.

I did not need to become an epoll expert. I needed to understand enough to judge what each option touches, what it risks and how we roll it back. That much, the AI taught me in one conversation. I followed its recommendation, fixing libtorrent.

The choice was still mine, made knowingly, and on one condition: that the fix be proven before touching the members' instances. Two things remained to prove: that the explanation was right, and that the fix held.

Search the code, then prove it

That explanation and those options did not fall from the sky. They came out of a single request, typed as soon as I had the error message: "Go search the internet and the source code for what's going on, and analyse it with Fable."

I was not only after a fix. I wanted the ins and outs explained to me, then several solutions laid out, each with what it touches and what it implies, so I could decide knowing the full picture.

The session turned that request into a four-point brief for a Fable subagent: read the code, search what the curl and rtorrent authors know, write a fix, prove it on an isolated test bench. It worked in a separate folder, away from the members' instances.

Less than an hour later, its report said three things: what it had found, what it had proven, and what it had not proven.

What it found

The bug comes from libcurl, and it is known. The curl authors fixed it with one line in July 2023 (PR #11439, shipped in curl 8.2.0). But Debian 12 still ships a libcurl from before that fix, 7.88.1.

On the rtorrent side, an open issue reports our exact message, and nobody has found the cause.

The report also listed what it had not found:

  • no newer rtorrent version that solves it
  • no published fix
  • no configuration option

That list is worth as much as the cause, because it saves me from hunting for a magic option that does not exist.

What it proved

A convincing explanation was not enough: the AI had just given a wrong one, the login session. We had to see the bug happen, on a machine where a crash bothers nobody. That is what the test bench is for: a small copy of the problem, on a private network, far from the members' instances.

To trigger the bug fast, the bench magnifies its trigger. A fake tracker replaces the real ones, and two of its addresses never answer: they play the dead trackers. 300 dummy torrents call it every 15 seconds, which multiplies the announces to those silent addresses.

Each run on this bench answers a single question:

  • Run: original rtorrent. The question: Does the bug reproduce? Result: 8 crashes out of 8, with the production message
  • Run: fixed rtorrent (libtorrent option). The question: Does the fix hold? Result: 30 min 36 s with no error, 30,397 announces
  • Run: original rtorrent, libcurl fixed by one line. The question: Is the cause the right one? Result: 16 min without a crash
  • Run: original rtorrent, tuned kernel (tcp_syn_retries=3). The question: Is the tuning enough? Result: 3 crashes out of 3

The third row is the most important. It does not test the fix, it tests the cause: rtorrent unchanged, one single libcurl line changed, and the crash disappears. The fourth closes the door on kernel tuning, the option that needed no code.

The chosen fix is about a hundred lines. libtorrent records each connection it hands to libcurl. If libcurl closed it on the sly, libtorrent forgets it instead of crashing, and keeps its strict check for everything else.

What it did not prove

The report ended with its own limits. I copy them here, because they say how far it can be trusted:

  • no run with real trackers or real peers
  • one of the fix's safety checks never came into play during the runs
  • half an hour of uptime, not days

So the report drew the line: proven on a test bench, not yet in production. The next step had to reach production without betting the whole server.

A guinea pig before everyone else

The bench had proved the fix at small scale. Production was left, and there we kept the upgrade's rule: one instance at a time, with an immediate way back.

The mechanism is a simple list, the li

Read on DEV Community ↗ ← Back to News

Comments

No comments yet. Start the discussion.