Skip to content

Commit 75027cf

Browse files
committed
Add an extra safety net
1 parent d001d99 commit 75027cf

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

src/feed/impl/TrackableSignal.hpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,13 @@ class TrackableSignal {
7373

7474
// This class can't hold the trackable's shared_ptr, because disconnect should be able to be called in the
7575
// the trackable's destructor. However, the trackable can not be destroyed when the slot is being called
76-
// either. `track_foreign` is racey because the control block can be released from some thread without a mutex
77-
// protecting it while another one is invoking the slot. Therefore we are storing a weak_ptr of the trackable
78-
// and explicitly checking it in the slot invocation instead. The invocation is guaranteed to happen under an
79-
// internal mutex so it's safe.
76+
// either. `track_foreign` is racey when one shared_ptr is tracked by multiple signals. Therefore we are storing
77+
// a weak_ptr of the trackable and using weak_ptr::lock() to atomically check existence and acquire a shared_ptr
78+
// during slot invocation. This guarantees to keep the trackable alive for the duration of the slot call and
79+
// avoids potential race conditions.
8080
connections->emplace(
8181
trackable.get(), signal_.connect([slot, weakTrackable = std::weak_ptr(trackable)](Args&&... args) {
82-
if (not weakTrackable.expired())
82+
if (auto lifeExtender = weakTrackable.lock(); lifeExtender)
8383
std::invoke(slot, std::forward<Args...>(args)...);
8484
})
8585
);

0 commit comments

Comments
 (0)