Relanding: Fixing crash that can occur if signal is modified while firing.

The crash occurs if a slot causes the very next slot in iteration order
to be disconnected.

Relanding after fixing a race condition that this CL revealed. Previously
the race resulted in an invalidated iterator, but now it will result in the
iterator being modified, so TSan catches it.

BUG=webrtc:7527

Review-Url: https://codereview.webrtc.org/2846593005
Cr-Original-Commit-Position: refs/heads/master@{#17943}
Committed: 961c2adf1e
Review-Url: https://codereview.webrtc.org/2846593005
Cr-Commit-Position: refs/heads/master@{#17965}
This commit is contained in:
deadbeef
2017-05-01 22:24:20 -07:00
committed by Commit bot
parent 0153ca5a61
commit fc1af01557
4 changed files with 93 additions and 22 deletions

View File

@ -20,7 +20,6 @@ AsyncInvoker::AsyncInvoker() : invocation_complete_(false, false) {}
AsyncInvoker::~AsyncInvoker() {
destroying_ = true;
SignalInvokerDestroyed();
// Messages for this need to be cleared *before* our destructor is complete.
MessageQueueManager::Clear(this);
// And we need to wait for any invocations that are still in progress on
@ -126,8 +125,9 @@ NotifyingAsyncClosureBase::NotifyingAsyncClosureBase(
calling_thread_(calling_thread) {
calling_thread->SignalQueueDestroyed.connect(
this, &NotifyingAsyncClosureBase::CancelCallback);
invoker->SignalInvokerDestroyed.connect(
this, &NotifyingAsyncClosureBase::CancelCallback);
// Note: We don't need to listen for the invoker being destroyed, because it
// will wait for this closure to be destroyed (and pending_invocations_ to go
// to 0) before its destructor completes.
}
NotifyingAsyncClosureBase::~NotifyingAsyncClosureBase() {

View File

@ -144,9 +144,6 @@ class AsyncInvoker : public MessageHandler {
// behavior is desired, call Flush() before destroying this object.
void Flush(Thread* thread, uint32_t id = MQID_ANY);
// Signaled when this object is destructed.
sigslot::signal0<> SignalInvokerDestroyed;
private:
void OnMessage(Message* msg) override;
void DoInvoke(const Location& posted_from,

View File

@ -394,7 +394,8 @@ namespace sigslot {
protected:
typedef std::list< _opaque_connection > connections_list;
_signal_base() : _signal_base_interface(&_signal_base::do_slot_disconnect, &_signal_base::do_slot_duplicate)
_signal_base() : _signal_base_interface(&_signal_base::do_slot_disconnect, &_signal_base::do_slot_duplicate),
m_current_iterator(m_connected_slots.end())
{
}
@ -407,7 +408,8 @@ namespace sigslot {
_signal_base& operator= (_signal_base const& that);
public:
_signal_base(const _signal_base& o) : _signal_base_interface(&_signal_base::do_slot_disconnect, &_signal_base::do_slot_duplicate) {
_signal_base(const _signal_base& o) : _signal_base_interface(&_signal_base::do_slot_disconnect, &_signal_base::do_slot_duplicate),
m_current_iterator(m_connected_slots.end()) {
lock_block<mt_policy> lock(this);
for (const auto& connection : o.m_connected_slots)
{
@ -460,7 +462,14 @@ namespace sigslot {
{
if(it->getdest() == pclass)
{
m_connected_slots.erase(it);
// If we're currently using this iterator,
// don't erase it and invalidate it yet; set a
// flag to do so afterwards.
if (m_current_iterator == it) {
m_erase_current_iterator = true;
} else {
m_connected_slots.erase(it);
}
pclass->signal_disconnect(static_cast< _signal_base_interface* >(this));
return;
}
@ -484,8 +493,15 @@ namespace sigslot {
if(it->getdest() == pslot)
{
self->m_connected_slots.erase(it);
}
// If we're currently using this iterator,
// don't erase it and invalidate it yet; set a
// flag to do so afterwards.
if (self->m_current_iterator == it) {
self->m_erase_current_iterator = true;
} else {
self->m_connected_slots.erase(it);
}
}
it = itNext;
}
@ -511,7 +527,12 @@ namespace sigslot {
protected:
connections_list m_connected_slots;
};
// Used to handle a slot being disconnected while a signal is
// firing (iterating m_connected_slots).
connections_list::iterator m_current_iterator;
bool m_erase_current_iterator = false;
};
template<class mt_policy = SIGSLOT_DEFAULT_MT_POLICY>
class has_slots : public has_slots_interface, public mt_policy
@ -605,16 +626,22 @@ namespace sigslot {
void emit(Args... args)
{
lock_block<mt_policy> lock(this);
typename connections_list::const_iterator it = this->m_connected_slots.begin();
typename connections_list::const_iterator itEnd = this->m_connected_slots.end();
while(it != itEnd)
{
_opaque_connection const& conn = *it;
++it;
conn.emit<Args...>(args...);
}
this->m_current_iterator =
this->m_connected_slots.begin();
while (this->m_current_iterator !=
this->m_connected_slots.end()) {
_opaque_connection const& conn =
*this->m_current_iterator;
conn.emit<Args...>(args...);
if (this->m_erase_current_iterator) {
this->m_current_iterator =
this->m_connected_slots.erase(
this->m_current_iterator);
this->m_erase_current_iterator = false;
} else {
++(this->m_current_iterator);
}
}
}
void operator()(Args... args)

View File

@ -272,3 +272,50 @@ TEST(SigslotTest, CopyConnectedSlot) {
signal();
EXPECT_EQ(1, copied_receiver.signal_count());
}
// Just used for the test below.
class Disconnector : public sigslot::has_slots<> {
public:
Disconnector(SigslotReceiver<>* receiver1, SigslotReceiver<>* receiver2)
: receiver1_(receiver1), receiver2_(receiver2) {}
void Connect(sigslot::signal<>* signal) {
signal_ = signal;
signal->connect(this, &Disconnector::Disconnect);
}
private:
void Disconnect() {
receiver1_->Disconnect();
receiver2_->Disconnect();
signal_->disconnect(this);
}
sigslot::signal<>* signal_;
SigslotReceiver<>* receiver1_;
SigslotReceiver<>* receiver2_;
};
// Test that things work as expected if a signal is disconnected from a slot
// while it's firing.
TEST(SigslotTest, DisconnectFromSignalWhileFiring) {
sigslot::signal<> signal;
SigslotReceiver<> receiver1;
SigslotReceiver<> receiver2;
SigslotReceiver<> receiver3;
Disconnector disconnector(&receiver1, &receiver2);
// From this ordering, receiver1 should receive the signal, then the
// disconnector will be invoked, causing receiver2 to be disconnected before
// it receives the signal. And receiver2 should also receive the signal,
// since it was never disconnected.
receiver1.Connect(&signal);
disconnector.Connect(&signal);
receiver2.Connect(&signal);
receiver3.Connect(&signal);
signal();
EXPECT_EQ(1, receiver1.signal_count());
EXPECT_EQ(0, receiver2.signal_count());
EXPECT_EQ(1, receiver3.signal_count());
}