This repository has been archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Runtime diagnostics for leaked messages in unbounded channels (part 2) #13020
Merged
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,6 +37,7 @@ mod inner { | |
mod inner { | ||
// tracing implementation | ||
use crate::metrics::UNBOUNDED_CHANNELS_COUNTER; | ||
use backtrace::Backtrace; | ||
use futures::{ | ||
channel::mpsc::{ | ||
self, SendError, TryRecvError, TrySendError, UnboundedReceiver, UnboundedSender, | ||
|
@@ -47,11 +48,10 @@ mod inner { | |
}; | ||
use log::error; | ||
use std::{ | ||
backtrace::{Backtrace, BacktraceStatus}, | ||
pin::Pin, | ||
sync::{ | ||
atomic::{AtomicBool, AtomicI64, Ordering}, | ||
Arc, | ||
Arc, Mutex, | ||
}, | ||
}; | ||
|
||
|
@@ -67,7 +67,7 @@ mod inner { | |
queue_size: Arc<AtomicI64>, | ||
queue_size_warning: i64, | ||
warning_fired: Arc<AtomicBool>, | ||
creation_backtrace: Arc<Backtrace>, | ||
creation_backtrace: Arc<Mutex<Backtrace>>, | ||
} | ||
|
||
// Strangely, deriving `Clone` requires that `T` is also `Clone`. | ||
|
@@ -108,7 +108,7 @@ mod inner { | |
queue_size: queue_size.clone(), | ||
queue_size_warning, | ||
warning_fired: Arc::new(AtomicBool::new(false)), | ||
creation_backtrace: Arc::new(Backtrace::capture()), | ||
creation_backtrace: Arc::new(Mutex::new(Backtrace::new_unresolved())), | ||
}; | ||
let receiver = TracingUnboundedReceiver { inner: r, name, queue_size }; | ||
(sender, receiver) | ||
|
@@ -149,23 +149,20 @@ mod inner { | |
|
||
let queue_size = self.queue_size.fetch_add(1, Ordering::Relaxed); | ||
if queue_size == self.queue_size_warning && | ||
!self.warning_fired.load(Ordering::Relaxed) | ||
self.warning_fired | ||
.compare_exchange(false, true, Ordering::Relaxed, Ordering::Relaxed) | ||
.is_ok() | ||
{ | ||
// `warning_fired` and `queue_size` are not synchronized, so it's possible | ||
// that the warning is fired few times before the `warning_fired` is seen | ||
// by all threads. This seems better than introducing a mutex guarding them. | ||
self.warning_fired.store(true, Ordering::Relaxed); | ||
match self.creation_backtrace.status() { | ||
BacktraceStatus::Captured => error!( | ||
"The number of unprocessed messages in channel `{}` reached {}.\n\ | ||
The channel was created at:\n{}", | ||
self.name, self.queue_size_warning, self.creation_backtrace, | ||
), | ||
_ => error!( | ||
"The number of unprocessed messages in channel `{}` reached {}.", | ||
self.name, self.queue_size_warning, | ||
), | ||
} | ||
let mut bt = self.creation_backtrace.lock().expect("another thread panicked."); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not really sure about the mutex here. I mean I get why, but we could also just use Arc and then clone the backtrace here into some mutable value to resolve it.
But I don't know if not using a mutex is wort the clone 🤷 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. It's a rare one-time event anyway, not a big deal. |
||
bt.resolve(); | ||
error!( | ||
"The number of unprocessed messages in channel `{}` reached {}.\n\ | ||
The channel was created at:\n{:?}", | ||
self.name, self.queue_size_warning, bt, | ||
); | ||
} | ||
|
||
s | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we not use the channel from utils here?
The metrics field is always None or?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sender
is not accessible on its own, but only viaOutChannels
.metrics
is inserted when a newSender
is added withOutChannels::push()
:substrate/client/network/src/service/out_events.rs
Lines 176 to 180 in 80082f1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've updated the comment to indicate that
metrics
will be initialized.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Metrics in
out_events.rs
andmpsc.rs
seem different, so I don't know how to reuseutils
version of the channel.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay ty. At some point we should probably merge both implementations. The metrics are probably not that different.