Skip to content

Commit

Permalink
Use an atomic bool flag since Soliton pointed out the last method sti…
Browse files Browse the repository at this point in the history
…ll had a race condition
  • Loading branch information
Vultraz committed Aug 1, 2019
1 parent 923e9aa commit 13b1758
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
17 changes: 7 additions & 10 deletions src/gui/dialogs/network_transmission.cpp
Expand Up @@ -42,10 +42,11 @@ network_transmission::pump_monitor::pump_monitor(connection_data*& connection)
, window_()
, completed_(0)
, total_(0)
, stop_(false)
, poller_(std::async(std::launch::async, [this]() {
while(true) {
// connection_ will be null if we reset the ptr in the main thread before canceling the dialog
if(!connection_) {
if(stop_) {
return false;
}

Expand Down Expand Up @@ -123,18 +124,14 @@ void network_transmission::post_show(window& /*window*/)
pump_monitor_.window_.reset();

if(get_retval() == retval::CANCEL) {
// The worker thread holds a reference to connection_ and checks whether it's null before polling.
// The actual object to which connection_ is pointing will be destroyed by cancel(), so we're
// setting connection_ to null here before that so the worker thread will exit next run.
connection_data* const connection_ptr_copy = connection_;
connection_ = nullptr;

// Wait for the current polling loop to conclude before exiting so we don't invalidate the pointer
// mid-loop. The thread should return false if connection_ is null.
// We need to wait for the current polling loop to conclude before exiting so we don't invalidate
// the pointer mid-loop, so signal that here. The thread should return false if connection_ is null.
pump_monitor_.stop_ = true;

pump_monitor_.poller_.wait();
assert(pump_monitor_.poller_.get() == false);

connection_ptr_copy->cancel();
connection_->cancel();
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/gui/dialogs/network_transmission.hpp
Expand Up @@ -66,6 +66,8 @@ class network_transmission : public modal_dialog

std::atomic_size_t completed_, total_;

std::atomic_bool stop_;

std::future<bool> poller_;
} pump_monitor_;

Expand Down

0 comments on commit 13b1758

Please sign in to comment.