Skip to content

Commit 4618ea9

Browse files
better locking
1 parent 1de44c8 commit 4618ea9

1 file changed

Lines changed: 13 additions & 4 deletions

File tree

src/subscription_thread_dispatcher.cpp

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -668,16 +668,25 @@ std::thread SubscriptionThreadDispatcher::startDataReaderLocked(DataFrameCallbac
668668
stream = subscribe_result.value();
669669
LK_LOG_INFO("Data reader thread: subscribed to \"{}\" track=\"{}\"", identity, track_name);
670670

671+
bool cancelled = false;
671672
{
672673
const std::scoped_lock<std::mutex> guard(reader->sub_mutex);
673674
// A replacement or teardown may have cancelled this reader while the
674-
// subscribe was in flight. Close the fresh stream and bail so we do not
675-
// leave a second live subscription behind.
675+
// subscribe was in flight. Close the fresh stream so we do not leave a
676+
// second live subscription behind.
676677
if (reader->cancelled.load()) {
678+
cancelled = true;
677679
stream->close();
678-
return;
680+
} else {
681+
reader->stream = stream;
679682
}
680-
reader->stream = stream;
683+
}
684+
if (cancelled) {
685+
// Mirror the normal-exit cleanup below. Done outside sub_mutex to keep the
686+
// lock_ -> sub_mutex order and avoid inversion; a no-op unless this reader
687+
// still owns its slot.
688+
eraseDataReaderIfCurrent(id, reader);
689+
return;
681690
}
682691

683692
DataTrackFrame frame;

0 commit comments

Comments
 (0)