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

fix: deadlock when replacing logging stream sink #227

Merged
merged 4 commits into from
Dec 5, 2024
Merged
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
16 changes: 8 additions & 8 deletions applogic/src/api/mobile_logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,14 @@ pub struct SendToDartLogger {

impl SendToDartLogger {
pub fn set_stream_sink(stream_sink: StreamSink<LogEntry>) {
let mut guard = SEND_TO_DART_LOGGER_STREAM_SINK.write().unwrap();
let overriding = guard.is_some();

*guard = Some(stream_sink);

drop(guard);

if overriding {
let prev_stream_sink = {
let mut guard = SEND_TO_DART_LOGGER_STREAM_SINK.write().expect("poisoned");
// Note: The previous stream sink MUST NOT be dropped before the `guard` is released.
// On drop, it will log a message, and therefore lock the sink for reading. Because the
// `RwLock` is not reentrant, this will deadlock.
guard.replace(stream_sink)
};
if prev_stream_sink.is_some() {
warn!(
"SendToDartLogger::set_stream_sink but already exist a sink, thus overriding. \
(This may or may not be a problem. It will happen normally if hot-reload Flutter app.)"
Expand Down
Loading