error linux

Linux Exit Code 139 - Segmentation Fault (SIGSEGV)

Understanding Linux exit code 139 - the process was terminated due to a segmentation fault, caused by invalid memory access.

What It Means

Exit code 139 (128 + 11) indicates that the process was terminated by SIGSEGV (signal 11), commonly known as a segmentation fault or “segfault.” This occurs when a program tries to access memory that it is not allowed to access, such as dereferencing a null pointer or accessing memory outside allocated bounds.

Common Causes

  • Dereferencing a null or dangling pointer (C/C++)
  • Buffer overflow — writing beyond allocated memory
  • Stack overflow from deep recursion
  • Using freed memory (use-after-free)
  • Incompatible shared library versions
  • Corrupted binary or library files
  • Hardware issues (faulty RAM)
  • Native addon crashes in Node.js or Python

How to Fix

Get a core dump for debugging

# Enable core dumps
ulimit -c unlimited

# Run the program
./my_program

# Analyze the core dump with gdb
gdb ./my_program core
(gdb) bt  # Print backtrace

# Or use lldb
lldb ./my_program -c core
(lldb) bt

Debug C/C++ segfaults

# Compile with debug symbols
gcc -g -O0 -fsanitize=address program.c -o program

# Run with AddressSanitizer
./program
# Will print detailed info about the memory error

# Use Valgrind
valgrind --tool=memcheck --leak-check=full ./program

Fix common C/C++ causes

// Null pointer dereference
char *ptr = NULL;
*ptr = 'x';  // SIGSEGV!

// Fix: check for NULL
if (ptr != NULL) {
    *ptr = 'x';
}

// Buffer overflow
char buf[10];
strcpy(buf, "this string is way too long for the buffer");  // SIGSEGV!

// Fix: use bounded copy
strncpy(buf, "short", sizeof(buf) - 1);
buf[sizeof(buf) - 1] = '\0';

Fix in Node.js (native addons)

# Rebuild native addons
npm rebuild

# Clear node_modules and reinstall
rm -rf node_modules
npm install

# Check for incompatible Node.js version
node -v
npm ls | grep native  # List native modules

Fix in Python (C extensions)

# Segfaults in Python usually come from C extensions
# Update the problematic package
pip install --upgrade numpy  # or whatever package is crashing

# Run with faulthandler for better traces
python3 -X faulthandler your_script.py

# Or enable in code
import faulthandler
faulthandler.enable()

Check for hardware issues

# Test RAM with memtest86+
sudo memtest86+

# Check disk for errors
sudo fsck -n /dev/sda1

# Check shared libraries
ldd ./my_program  # Look for "not found" entries

Fix shared library issues

# Check for missing or incompatible libraries
ldd ./my_program

# Update library cache
sudo ldconfig

# Set library path if needed
export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH