Fixed bug with event notifier being notified with a bad eventfd

This commit is contained in:
Nav
2021-05-31 00:01:42 +01:00
parent 602328d9d1
commit 635f908a45
3 changed files with 27 additions and 11 deletions

View File

@@ -32,7 +32,6 @@ void DebugServer::startup() {
this->eventManager.registerListener(this->eventListener);
this->interruptEventNotifier = std::make_shared<EventNotifier>();
this->interruptEventNotifier->init();
this->eventListener->setInterruptEventNotifier(this->interruptEventNotifier);
// Register event handlers
@@ -52,8 +51,8 @@ void DebugServer::shutdown() {
this->setThreadState(ThreadState::SHUTDOWN_INITIATED);
Logger::info("Shutting down DebugServer");
this->close();
this->interruptEventNotifier->close();
this->setThreadStateAndEmitEvent(ThreadState::STOPPED);
this->eventManager.deregisterListener(this->eventListener->getId());
}
void DebugServer::onShutdownDebugServerEvent(EventPointer<Events::ShutdownDebugServer> event) {

View File

@@ -31,7 +31,7 @@ void EventListener::registerEvent(GenericEventPointer event) {
eventQueueByType[eventName].push(event);
this->eventQueueByEventTypeCV.notify_all();
if (this->interruptEventNotifier != nullptr) {
if (this->interruptEventNotifier != nullptr && this->interruptEventNotifier->isInitialised()) {
this->interruptEventNotifier->notify();
}
}

View File

@@ -4,6 +4,7 @@
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <atomic>
#include "src/Exceptions/Exception.hpp"
@@ -26,9 +27,7 @@ namespace Bloom
{
private:
int fileDescriptor = -1;
public:
EventNotifier() = default;
std::atomic<bool> initialised = false;
void init() {
this->fileDescriptor = ::eventfd(0, EFD_NONBLOCK);
@@ -37,12 +36,34 @@ namespace Bloom
throw Exceptions::Exception("Failed to create new eventfd object - error number: "
+ std::to_string(errno));
}
this->initialised = true;
}
void close() {
::close(this->fileDescriptor);
this->initialised = false;
}
public:
EventNotifier() {
this->init();
};
~EventNotifier() {
if (this->initialised) {
this->close();
}
}
int getFileDescriptor() {
return this->fileDescriptor;
}
bool isInitialised() {
return this->initialised;
}
void notify() {
if (::eventfd_write(this->fileDescriptor, 1) < 0) {
throw Exceptions::Exception("Failed to increment eventfd counter - error number: "
@@ -57,9 +78,5 @@ namespace Bloom
"error number: " + std::to_string(errno));
}
}
void close() {
::close(this->fileDescriptor);
}
};
}