Skip to content

Commit e96df68

Browse files
goffrieConvex, Inc.
authored andcommitted
Fix race condition on log_subscription_queue_length_delta (#40948)
During shutdown a race can occur where we decrement before the increment comes through, causing an underflow. GitOrigin-RevId: 3fd17cd8200506d628a55616bd990e8af9271113
1 parent 28975e0 commit e96df68

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

crates/database/src/subscription.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,15 @@ impl SubscriptionsClient {
101101
};
102102
let (subscription, sender) = Subscription::new(&token);
103103
let request = SubscriptionRequest::Subscribe { token, sender };
104-
self.sender.try_send(request).map_err(|e| match e {
105-
TrySendError::Full(..) => metrics::subscriptions_worker_full_error().into(),
106-
TrySendError::Closed(..) => metrics::shutdown_error(),
107-
})?;
104+
// Increment the counter first to avoid underflow
108105
metrics::log_subscription_queue_length_delta(1);
106+
if let Err(e) = self.sender.try_send(request) {
107+
metrics::log_subscription_queue_length_delta(-1);
108+
return Err(match e {
109+
TrySendError::Full(..) => metrics::subscriptions_worker_full_error().into(),
110+
TrySendError::Closed(..) => metrics::shutdown_error(),
111+
});
112+
}
109113
Ok(subscription)
110114
}
111115

0 commit comments

Comments
 (0)