← Back to PureKit

Multithreaded fork() & os.popen() Corrupting glibc into Python SIGSEGV

#Python#Core Dump#glibc#Concurrency
Multithreaded fork() & os.popen() Corrupting glibc into Python SIGSEGV

The Symptom

A multithreaded Python management daemon began crashing intermittently with native SIGSEGV faults during burst scheduling. Because it died inside native C code, Python never printed a traceback—only a core dump remained.

Disassembling the Core Dump

Inspecting the core dump in GDB and disassembling around the faulting instruction pointed directly into glibc’s internal open-stream linked list (_IO_list_all).

Cross-referencing the thread stacks revealed the exact concurrency trap:

  1. Multiple worker threads were invoking os.popen() concurrently.
  2. Under the hood, os.popen() calls C popen() / fork() while sibling threads are actively allocating and mutating glibc’s internal FILE* stream list.
  3. In POSIX multithreaded processes, fork() duplicates only the calling thread; if another thread held a lock or was mid-update on glibc’s stream structures, the child process inherited a corrupted linked list and immediately segfaulted.

The Fix

  • Replaced os.popen() with Python’s thread-safe subprocess APIs (using posix_spawn / safe exec paths without mutating C stdio stream lists across threads).
  • Added explicit concurrency caps to batch command scheduling to prevent burst thread storms.

Takeaway

os.popen() is a legacy hazard inside multithreaded Python daemons. When native crashes happen with no Python traceback, GDB disassembly on the core dump beats weeks of log guessing.

Enjoyed this note or seen a similar bug?

Feel free to drop a line—always glad to exchange ideas with fellow engineers.

Say hi · [email protected]