Skip to content

Panicking in the drop function of a struct that is stored in a thread local causes a stack overflow #24313

Closed
@carllerche

Description

@carllerche

When thread locals are dropped, the thread is not valid anymore so accessing thread::current() will panic, however, begin_unwind calls thread::current() which will cause an infinite loop and a stack overflow.

The follow example has been tested to crash with an out of memory error on OS X:

use std::thread;

struct Handle {
    i: i32,
}

impl Drop for Handle {
    fn drop(&mut self) {
        panic!("BOOM");
    }
}

thread_local!(static HANDLE: Handle = new_handle());

fn new_handle() -> Handle {
    Handle { i: 0 }
}

pub fn main() {
    let _ = thread::scoped(|| {
        HANDLE.with(|h| {
            println!("HANDLE: {}", h.i);
        });
    });
}

cc @alexcrichton

Metadata

Metadata

Assignees

No one assigned

    Labels

    I-crashIssue: The compiler crashes (SIGSEGV, SIGABRT, etc). Use I-ICE instead when the compiler panics.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions