# Rust: thread 'main' has overflowed its stack — fatal runtime error: stack overflow

**Code:** `thread 'main' has overflowed its stack (SIGABRT)` · **Category:** Rust · **Seen on:** Rust, Linux

Recursion (or huge stack allocations) exhausted the thread's stack.

## Meaning

Add a base case or convert recursion to iteration, avoid large arrays on the stack (`[0u8; 10_000_000]` → `Vec` or `Box`), and for deep legitimate recursion spawn a thread with a bigger stack (`thread::Builder::new().stack_size(64*1024*1024)`). Debug builds use more stack than release.

## Common causes

- Infinite or very deep recursion
- Large stack arrays/structs
- Recursive Drop/Debug on deep linked lists/trees
- Async recursion without boxing (compile error instead)
- Default 8 MB (2 MB for spawned threads) limit

## Quick fix

1. Fix the base case or iterate with an explicit stack
2. Move big buffers to the heap (`vec![0; n]`/`Box`)
3. Run in a thread with a larger stack

## Detailed fix by platform

### Rust

1. `std::thread::Builder::new().stack_size(64 << 20).spawn(run)?.join().unwrap();`

## How to diagnose

1. **Recursion** — Depth/base case?
2. **Stack** — Large arrays?
3. **Thread** — Stack size?

---
Source: [WhatIsMyError](https://whatismyerror.com/errors/rust/panic-stack-overflow-thread-main)
