Skip to content

Commit

Permalink
Ensure we don't modify watches while poll is running
Browse files Browse the repository at this point in the history
  • Loading branch information
TrentHouliston committed Oct 10, 2023
1 parent 11fa194 commit e774840
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions src/extension/IOController_Posix.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,9 @@ namespace extension {
std::system_category(),
"There was an error while writing to the notification pipe");
}

// Locking here will ensure we won't return until poll is not running
std::lock_guard<std::mutex> lock(poll_mutex);
}

public:
Expand Down Expand Up @@ -282,7 +285,6 @@ namespace extension {
return t.reaction->id == event.id;
});

// If we found it then clear the waiting events
if (task != tasks.end()) {
// If the events we were processing included close remove it from the list
if ((task->processing_events & IO::CLOSE) != 0) {
Expand Down Expand Up @@ -348,10 +350,14 @@ namespace extension {
}

// Wait for an event to happen on one of our file descriptors
if (::poll(watches.data(), nfds_t(watches.size()), -1) < 0) {
throw std::system_error(network_errno,
std::system_category(),
"There was an IO error while attempting to poll the file descriptors");
/* mutex scope */ {
std::lock_guard<std::mutex> lock(poll_mutex);
if (::poll(watches.data(), nfds_t(watches.size()), -1) < 0) {
throw std::system_error(
network_errno,
std::system_category(),
"There was an IO error while attempting to poll the file descriptors");
}
}

// Collect the events that happened into the tasks list
Expand All @@ -366,6 +372,9 @@ namespace extension {
/// @brief The send file descriptor for our notification pipe
fd_t notify_send{-1};

/// @brief The mutex to wait on when bumping to ensure poll has returned
std::mutex poll_mutex;

/// @brief Whether or not we are shutting down
std::atomic<bool> shutdown{false};
/// @brief The mutex that protects the tasks list
Expand Down

0 comments on commit e774840

Please sign in to comment.