Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Perf[BMQIO]: remove unnecessary function copy #483

Merged
merged 2 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 21 additions & 34 deletions src/groups/bmq/bmqio/bmqio_ntcchannel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,6 @@ NtcRead::~NtcRead()
BSLS_ASSERT_OPT(d_numNeeded == 0);
BSLS_ASSERT_OPT(d_complete);
BSLS_ASSERT_OPT(!d_timer_sp);
BSLS_ASSERT_OPT(!d_callback);
}

// MANIPULATORS
Expand All @@ -427,17 +426,6 @@ void NtcRead::setTimer(const bsl::shared_ptr<ntci::Timer>& timer)
d_timer_sp = timer;
}

void NtcRead::setComplete()
{
if (d_timer_sp) {
d_timer_sp->close();
d_timer_sp.reset();
}

d_numNeeded = 0;
d_complete = true;
}

void NtcRead::clear()
{
if (d_timer_sp) {
Expand All @@ -447,8 +435,6 @@ void NtcRead::clear()

d_numNeeded = 0;
d_complete = true;

d_callback = bmqio::Channel::ReadCallback();
}

// ACCESSORS
Expand Down Expand Up @@ -634,14 +620,12 @@ void NtcChannel::processReadTimeout(

d_readQueue.remove(read);

bool isComplete = read->isComplete();
read->setComplete();

if (d_state == e_STATE_CLOSED) {
read->clear();
return;
}

if (isComplete) {
if (read->isComplete()) {
return;
}

Expand All @@ -665,14 +649,12 @@ void NtcChannel::processReadCancelled(

d_readQueue.remove(read);

bool isComplete = read->isComplete();
read->setComplete();

if (d_state == e_STATE_CLOSED) {
read->clear();
return;
}

if (isComplete) {
if (read->isComplete()) {
return;
}

Expand Down Expand Up @@ -711,7 +693,6 @@ void NtcChannel::processReadQueueLowWatermark(
bsl::shared_ptr<bmqio::NtcRead> read = d_readQueue.front();

if (read->numNeeded() == 0 || read->isComplete()) {
read->setComplete();
read->clear();
d_readQueue.pop();
continue;
Expand Down Expand Up @@ -758,11 +739,19 @@ void NtcChannel::processReadQueueLowWatermark(

int numNeeded = 0;
{
bmqio::Channel::ReadCallback readCallback = read->callback();
const bmqio::Channel::ReadCallback& readCallback =
read->callback();

bslmt::UnLockGuard<bslmt::Mutex> unlock(&d_mutex);
readCallback(bmqio::Status(), &numNeeded, &d_readCache);
Copy link
Collaborator Author

@678098 678098 Oct 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

    bslmt::UnLockGuard<bslmt::Mutex> unlock(&d_mutex);
    // !!!!!!!!! `close` might be called from another thread
    readCallback(bmqio::Status(), &numNeeded, &d_readCache);

Here, we have a non-trivial thread race. In the past, we could have emptied the d_callback of NtcRead between these 2 lines of code. We make a copy of d_callback before unlocking a mutex, so we don't access the empty callback. However, it means that we can send another callback from this copy after we executed close.

My PR doesn't fix this race, so we still can send something over this callback. Instead, I make sure not to clear it from another thread, so this reference is always alive. And I also added a post-check for isComplete() to return early.

}
if (read->isComplete()) {
// It's possible that we encountered canceled or timeout event
// when we unlocked `d_mutex`, so the `read` pointer that we
// hold now might be pointing to NtcRead already removed from
// `d_readQueue`. There is nothing we can do.
continue;
}

BMQIO_NTCCHANNEL_LOG_READ_CACHE_DRAINED(this,
d_streamSocket_sp,
Expand All @@ -772,7 +761,6 @@ void NtcChannel::processReadQueueLowWatermark(
BMQIO_NTCCHANNEL_LOG_READ_COMPLETE(this,
d_streamSocket_sp,
read);
read->setComplete();
read->clear();
d_readQueue.remove(read);
continue;
Expand Down Expand Up @@ -802,10 +790,7 @@ void NtcChannel::processReadQueueLowWatermark(
bsl::shared_ptr<bmqio::NtcRead> read;
d_readQueue.pop(&read);

bool isComplete = read->isComplete();
read->setComplete();

if (!isComplete) {
if (!read->isComplete()) {
bmqio::Channel::ReadCallback readCallback = read->callback();
read->clear();

Expand Down Expand Up @@ -880,10 +865,7 @@ void NtcChannel::processShutdownReceive(
bsl::shared_ptr<bmqio::NtcRead> read;
d_readQueue.pop(&read);

bool isComplete = read->isComplete();
read->setComplete();

if (!isComplete) {
if (!read->isComplete()) {
bmqio::Channel::ReadCallback readCallback = read->callback();
read->clear();

Expand Down Expand Up @@ -1307,6 +1289,8 @@ void NtcChannel::cancelRead()

void NtcChannel::close(const Status& status)
{
// Executed from *ANY* thread

bslmt::LockGuard<bslmt::Mutex> lock(&d_mutex);

bsl::shared_ptr<NtcChannel> self = this->shared_from_this();
Expand All @@ -1319,7 +1303,10 @@ void NtcChannel::close(const Status& status)
bsl::shared_ptr<bmqio::NtcRead> read;
d_readQueue.pop(&read);

read->setComplete();
// This code assumes thread-safety of `ntci::Timer::close` because we
// are not in IO thread.
// `read->d_callback` can still be executed concurrently from IO
// thread.
read->clear();
}

Expand Down
3 changes: 0 additions & 3 deletions src/groups/bmq/bmqio/bmqio_ntcchannel.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,6 @@ class NtcRead {
/// Set the timer to the specified `timer`.
void setTimer(const bsl::shared_ptr<ntci::Timer>& timer);

/// Set the operation as completed.
void setComplete();

/// Set the operation as completed and clear all resources.
void clear();

Expand Down
Loading