Skip to content

Crashes in a multithreaded program on Ctrl-C #7603

@mikulas-patocka

Description

@mikulas-patocka

The object scoped_ctrl_c in src/util/scoped_ctrl_c.cpp is not thread safe. It uses a global variable g_obj without any locking. It installs a signal handler with the "signal" function, but the handler is per-process, not per-thread, so different threads step over each other's handlers. Note also that in a multithreaded program, it is unpredictable which thread receives the signal, so the signal handler may be racing with the compute thread that installed the handler.

I created a program that spawns 16 threads, each thread repeatedly makes a context, fills the context with simple assertions, calls "(check-sat)" and destroys the context. It works reliably, but when Ctrl-C is pressed, I get occasional crashes. The crashes happen inside the "on_ctrl_c" function.

I locked the calls to "Z3_mk_context" and "Z3_del_context" (so that they are not performed in parallel). But I did not lock the calls to "Z3_eval_smtlib2_string" because they are done on different contexts. Note that calling "Z3_eval_smtlib2_string" from only one thread wouldn't fix this bug entirely, because the signal handler may be run in a different thread and may race with scoped_ctrl_c destructor.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions