Fix deadlock on deleting event. Cannot race for the lock, joining will do the job

This commit is contained in:
Isaac Connor 2022-01-12 23:11:19 -05:00
parent a9f7b257ea
commit f764cfbc32
2 changed files with 18 additions and 6 deletions

View File

@ -134,14 +134,19 @@ Event::Event(
); );
id = zmDbDoInsert(sql); id = zmDbDoInsert(sql);
thread_ = std::thread(&Event::Run, this); thread_ = std::thread(&Event::Run, this);
} }
Event::~Event() { Event::~Event() {
Debug(1, "Deleting event, calling stop");
Stop(); Stop();
if (thread_.joinable()) thread_.join(); if (thread_.joinable()) {
// Should be. Issuing the stop and then getting the lock
Debug(1, "Joinable");
thread_.join();
} else {
Debug(1, "Not Joinable");
}
/* Close the video file */ /* Close the video file */
// We close the videowriter first, because if we finish the event, we might try to view the file, but we aren't done writing it yet. // We close the videowriter first, because if we finish the event, we might try to view the file, but we aren't done writing it yet.
@ -670,19 +675,23 @@ void Event::Run() {
if (storage != monitor->getStorage()) if (storage != monitor->getStorage())
delete storage; delete storage;
std::unique_lock<std::mutex> lck(packet_queue_mutex); std::unique_lock<std::mutex> lck(packet_queue_mutex);
// The idea is to process the queue no matter what so that all packets get processed. // The idea is to process the queue no matter what so that all packets get processed.
// We only break if the queue is empty // We only break if the queue is empty
while (true) { while (true) {
if (!packet_queue.empty()) { if (!packet_queue.empty()) {
Debug(1, "adding packet");
this->AddPacket_(packet_queue.front()); this->AddPacket_(packet_queue.front());
packet_queue.pop(); packet_queue.pop();
} else { } else {
if (terminate_ or zm_terminate) if (terminate_ or zm_terminate) {
Debug(1, "terminating");
break; break;
}
Debug(1, "waiting");
packet_queue_condition.wait(lck); packet_queue_condition.wait(lck);
Debug(1, "wakeing");
} }
} }
} }

View File

@ -147,7 +147,10 @@ class Event {
int score = 0, int score = 0,
Image *alarm_image = nullptr); Image *alarm_image = nullptr);
void Stop() { terminate_ = true; } void Stop() {
terminate_ = true;
packet_queue_condition.notify_all();
}
bool Stopped() const { return terminate_; } bool Stopped() const { return terminate_; }
private: private: