How We Stopped PDF Crashes From Taking Down Our Python Workers

本文为原文前 6,000 字符的节选翻译,完整内容请查看原文。

How We Stopped PDF Crashes From Taking Down Our Python Workers

TL;DR A crash inside native code (the compiled C or C++ libraries that Python calls into) kills the whole Python process, so no try/except, finally block or signal-based timeout in that process can save it. Running each PDF conversion in its own short-lived child process turned a crash that killed the worker into one failed document, with one automatic retry that skips text recognition. The isolation has a price: every document now starts a fresh process, and with every worker now staying up under heavy CPU load, slow conversions ran past our time limit until we raised it.

简而言之,原生代码(Python 所调用的已编译 C 或 C++ 库)内部的崩溃会直接杀死整个 Python 进程,因此该进程内的任何 try/except、finally 代码块或基于信号的超时机制都无法挽救它。通过在各自独立的短生命周期子进程中运行每个 PDF 转换任务,我们将导致工作进程崩溃的问题转化为单个文档处理失败,并配合一次跳过文本识别的自动重试机制。这种隔离是有代价的:每个文档现在都会启动一个新进程,且由于所有工作进程在繁重的 CPU 负载下持续运行,缓慢的转换任务超出了我们的时间限制,直到我们调高了限时。

Our PDF workers kept dying a few minutes after we started them. There was no error and no stack trace. We restarted them, and they died again. If you run background workers, you’ve probably seen a version of this. You start a process, it picks up a few jobs, and a while later it’s simply gone from the process list. The last log line is an ordinary progress message. You search for the exception that killed it, and there isn’t one.

我们的 PDF 工作进程在启动几分钟后就会不断死亡。没有任何错误提示,也没有堆栈跟踪。我们重启它们,它们又再次死亡。如果你运行过后台工作进程,你可能见过类似的情况。你启动一个进程,它处理了几个任务,过了一会儿它就从进程列表中消失了。最后一条日志只是一条普通的进度消息。你搜索导致它终止的异常,却什么也找不到。

Multibagg AI is an AI-powered stock research platform for Indian investors. Every company document that reaches us has to become searchable text before our AI can answer questions from it, so a stalled converter means answers that miss the latest documents. Here’s how a segfault (a hard crash in compiled code) in an OCR library took our workers down, the three theories we got wrong first, and the small change that lets one bad document fail without taking the worker with it.

Multibagg AI 是一个为印度投资者提供的 AI 驱动型股票研究平台。每一份到达我们这里的公司文档都必须转化为可搜索的文本,我们的 AI 才能回答相关问题,因此转换器停滞意味着无法根据最新文档提供答案。以下是 OCR 库中的段错误(编译代码中的严重崩溃)如何导致我们的工作进程瘫痪,我们最初误判的三个理论,以及那个能让单个坏文档失败而不拖垮工作进程的小改动。

Our workers died, and nothing recorded why. The setup is simple. A queue holds documents waiting for conversion. A Python worker takes one message, converts the PDF to markdown with Docling (an open-source document converter), stores the result and moves on. If a page is an image rather than text, Docling runs OCR (optical character recognition) to read it. A sweep job runs twice a day and re-queues any document that has no “attempted” mark yet, so nothing gets forgotten.

我们的工作进程死了,但没有任何记录说明原因。设置很简单:一个队列存放等待转换的文档。一个 Python 工作进程获取一条消息,使用 Docling(一个开源文档转换器)将 PDF 转换为 Markdown,存储结果并继续处理。如果页面是图像而非文本,Docling 会运行 OCR(光学字符识别)来读取它。一个扫描任务每天运行两次,将任何尚未标记为“已尝试”的文档重新入队,这样就不会遗漏任何内容。

The old worker had three safety nets, and on paper they covered every failure: A time limit. Before converting, the code set a SIGALRM alarm for 600 seconds, and the alarm handler raised a TimeoutError. A fallback. A teammate had added a retry without OCR for errors that looked like OCR problems. Bookkeeping in a finally block. Whatever happened, the handler marked the document as attempted, and a failed document went to a failed-documents queue. A reclaim job, also a teammate’s work, retried those up to three times and then set the document aside with an alert.

旧的工作进程有三道安全网,理论上它们覆盖了所有故障:一是时间限制。在转换前,代码设置了一个 600 秒的 SIGALRM 警报,警报处理程序会引发 TimeoutError。二是回退机制。一位同事为看起来像 OCR 问题的错误添加了不使用 OCR 的重试逻辑。三是 finally 代码块中的记录工作。无论发生什么,处理程序都会将文档标记为“已尝试”,并将失败的文档发送到失败文档队列。还有一个回收任务(也是同事的工作)会对这些文档重试最多三次,然后将其搁置并发出警报。

Every one of those nets assumes the Python process is still alive when something goes wrong. That assumption was the bug. The message was already deleted, so when the process died mid-conversion, nothing sent it to the failed queue. The finally block never ran, so the document never got its “attempted” mark. At the next sweep it went straight back into the queue and killed whichever worker picked it up. Nothing restarted the workers either. We started them by hand.

这些安全网中的每一个都假设在出错时 Python 进程仍然存活。这个假设就是漏洞所在。消息已经被删除,因此当进程在转换过程中死亡时,没有任何东西将其发送到失败队列。finally 代码块从未执行,因此文档从未获得“已尝试”标记。在下一次扫描时,它直接回到了队列中,并杀死了任何拾取它的工作进程。也没有任何机制重启这些工作进程,我们只能手动启动它们。

A crashing PDF never got marked as attempted, so the sweep kept feeding it to a fresh worker. Three reasonable theories, all wrong. I worked through the investigation with an AI coding assistant. It read the code and logs and proposed causes, and I checked each one against what the server actually showed. Three theories sounded right and didn’t survive.

导致崩溃的 PDF 从未被标记为“已尝试”,因此扫描任务不断将其喂给新的工作进程。三个合理的理论,全错了。我与一位 AI 编程助手一起进行了调查。它阅读了代码和日志并提出了原因,我将每一个原因与服务器的实际表现进行了核对。有三个理论听起来很正确,但都站不住脚。

Theory: Out of memory. Why it sounded right: A process that vanishes with no traceback looks like the kernel’s out-of-memory killer. What ended it: Plenty of free memory, and no out-of-memory kills in the kernel log. Theory: One poison PDF. Why it sounded right: A single bad file re-queued forever would explain the repeat deaths. What ended it: Different PDFs crashed different workers, and conversions succeeded in between. Theory: A numpy binary mismatch. Why it sounded right: Nothing was pinned, so a reinstall had moved several native libraries to new major versions. What ended it: That kind of mismatch fails loudly at import, and these workers ran cleanly for minutes first.

理论:内存不足。听起来合理的原因:一个没有堆栈跟踪就消失的进程看起来像是内核的内存不足杀手(OOM Killer)。终结该理论的事实:有充足的可用内存,且内核日志中没有内存不足杀死的记录。理论:存在一个“毒”PDF。听起来合理的原因:单个坏文件被无限期重新入队可以解释重复的死亡。终结该理论的事实:不同的 PDF 导致不同的工作进程崩溃,且在崩溃之间转换任务是成功的。理论:Numpy 二进制不匹配。听起来合理的原因:没有锁定版本,因此重新安装将几个原生库移动到了新的主版本。终结该理论的事实:那种不匹配在导入时就会剧烈报错,而这些工作进程最初能正常运行几分钟。

The kernel log settled it: a segfault in the Python process. A segfault (segmentation fault) means native code touched memory it had no right to, and the operating system ended the process on the spot. The crash always came during the OCR step. The second finding was quieter. onnxruntime, the inference runtime that the RapidOCR engine uses by default, wasn’t installed on the server. When Docling picks an OCR engine automatically and can’t import onnxruntime, it moves down its list and can end up running RapidOCR on PyTorch instead. Nothing warned us. Every crash we saw happened inside that fallback path.

内核日志给出了定论:Python 进程中出现了段错误(segfault)。段错误意味着原生代码触碰了它无权访问的内存,操作系统当场终止了该进程。崩溃总是发生在 OCR 步骤中。第二个发现则更为隐蔽。onnxruntime(RapidOCR 引擎默认使用的推理运行时)并未安装在服务器上。当 Docling 自动选择 OCR 引擎且无法导入 onnxruntime 时,它会顺着列表向下查找,最终可能在 PyTorch 上运行 RapidOCR。没有任何警告提示我们。我们看到的每一次崩溃都发生在那个回退路径中。

I want to be careful here. The crashes lined up with the fallback, but we never reproduced one with a core dump, so “the torch backend crashed” is a correlation, not a proven root cause. What it did teach me: a missing optional dependency doesn’t always fail. Sometimes it quietly swaps in a different code path, and now you’re running software you never tested. Can you catch a segfault in Python? No. A segfault isn’t a Python exception. It’s a signal from the operating system, and its default action ends the process. No except block sees it and no finally block runs.

我想在这里谨慎一点。崩溃确实与回退路径相吻合,但我们从未通过核心转储(core dump)复现过,因此“Torch 后端崩溃”是一种相关性,而非已证实的根本原因。它教会我的是:缺失的可选依赖项并不总是会导致失败。有时它会悄悄切换到不同的代码路径,而你现在运行的是从未测试过的软件。你能在 Python 中捕获段错误吗?不能。段错误不是 Python 异常。它是来自操作系统的信号,其默认操作是终止进程。没有任何 except 代码块能看到它,也没有任何 finally 代码块会运行。