3
0
Fork 0
mirror of https://github.com/Z3Prover/z3 synced 2025-09-03 08:38:06 +00:00

fix #7603: race condition in Ctrl-C handling (#7755)

* fix #7603: race condition in Ctrl-C handling

* fix race in cancel_eh

* fix build
This commit is contained in:
Nuno Lopes 2025-08-06 22:27:28 +01:00 committed by GitHub
parent 7a8ba4b474
commit b1ab695eb6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 49 additions and 44 deletions

View file

@ -18,6 +18,8 @@ Revision History:
--*/
#pragma once
#include <atomic>
#include <mutex>
#include "util/event_handler.h"
/**
@ -25,22 +27,29 @@ Revision History:
*/
template<typename T>
class cancel_eh : public event_handler {
bool m_canceled = false;
std::mutex m_mutex;
std::atomic<bool> m_canceled = false;
bool m_auto_cancel = false;
T & m_obj;
public:
cancel_eh(T & o): m_obj(o) {}
~cancel_eh() override { if (m_canceled) m_obj.dec_cancel(); if (m_auto_cancel) m_obj.auto_cancel(); }
// Note that this method doesn't race with the destructor since
// potential callers like scoped_ctrl_c/scoped_timer are destroyed
// before the cancel_eh destructor is invoked.
// Thus, the only races are with itself and with the getters.
void operator()(event_handler_caller_t caller_id) override {
std::lock_guard lock(m_mutex);
if (!m_canceled) {
m_caller_id = caller_id;
m_obj.inc_cancel();
m_canceled = true;
m_obj.inc_cancel();
}
}
bool canceled() const { return m_canceled; }
void reset() { m_canceled = false; }
T& t() { return m_obj; }
void set_auto_cancel() { m_auto_cancel = true; }
};