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 crash when using RecordingStream::set_thread_local on macOS #3929

Merged
merged 8 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

67 changes: 47 additions & 20 deletions crates/re_sdk/src/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,49 @@ impl std::fmt::Display for RecordingScope {

// ---

/// Required to work-around <https://github.com/rerun-io/rerun/issues/2889>
#[derive(Default)]
struct ThreadLocalRecording {
stream: Option<RecordingStream>,
}

impl ThreadLocalRecording {
fn replace(&mut self, stream: Option<RecordingStream>) -> Option<RecordingStream> {
std::mem::replace(&mut self.stream, stream)
}

fn get(&self) -> Option<RecordingStream> {
self.stream.clone()
}
}

#[cfg(target_os = "macos")]
impl Drop for ThreadLocalRecording {
fn drop(&mut self) {
if let Some(stream) = self.stream.take() {
// Work-around for https://github.com/rerun-io/rerun/issues/2889
// Calling drop on `self.stream` will panic the calling thread.
// But we want to make sure we don't loose the data in the stream.
// So how?
re_log::warn!("Using thread-local RecordingStream on macOS can result in data loss because of https://github.com/rerun-io/rerun/issues/2889");

// Give the batchet and sink threads a chance to process the data.
emilk marked this conversation as resolved.
Show resolved Hide resolved
std::thread::sleep(std::time::Duration::from_millis(500));
Copy link
Member

Choose a reason for hiding this comment

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

Tbh not sure if we already talked about this, but there's no way easy way right now to know whether the threads are still busy, right? Would be nice to be able to tell them to join instead 🤔
Adding 500ms to our shutdown time is pretty awful and error prone.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, it's not great, but we would need to check if all the queues are empty and the threads non-busy all the way down, and it is not trivial

Copy link
Member Author

Choose a reason for hiding this comment

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

I'll open a new issue (and close the old one) as a future TODO

Copy link
Member Author

Choose a reason for hiding this comment

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


#[allow(clippy::mem_forget)] // Intentionally not calling `drop`
std::mem::forget(stream);
}
}
}

static GLOBAL_DATA_RECORDING: OnceCell<RwLock<Option<RecordingStream>>> = OnceCell::new();
thread_local! {
static LOCAL_DATA_RECORDING: RefCell<Option<RecordingStream>> = RefCell::new(None);
static LOCAL_DATA_RECORDING: RefCell<ThreadLocalRecording> = Default::default();
}

static GLOBAL_BLUEPRINT_RECORDING: OnceCell<RwLock<Option<RecordingStream>>> = OnceCell::new();
thread_local! {
static LOCAL_BLUEPRINT_RECORDING: RefCell<Option<RecordingStream>> = RefCell::new(None);
static LOCAL_BLUEPRINT_RECORDING: RefCell<ThreadLocalRecording> = Default::default();
}

/// Check whether we are the child of a fork.
Expand Down Expand Up @@ -187,17 +222,15 @@ impl RecordingStream {
.get_or_init(Default::default)
.read()
.clone(),
RecordingScope::ThreadLocal => {
LOCAL_DATA_RECORDING.with(|rec| rec.borrow().clone())
}
RecordingScope::ThreadLocal => LOCAL_DATA_RECORDING.with(|rec| rec.borrow().get()),
},
StoreKind::Blueprint => match scope {
RecordingScope::Global => GLOBAL_BLUEPRINT_RECORDING
.get_or_init(Default::default)
.read()
.clone(),
RecordingScope::ThreadLocal => {
LOCAL_BLUEPRINT_RECORDING.with(|rec| rec.borrow().clone())
LOCAL_BLUEPRINT_RECORDING.with(|rec| rec.borrow().get())
}
},
}
Expand All @@ -214,10 +247,9 @@ impl RecordingStream {
&mut *GLOBAL_DATA_RECORDING.get_or_init(Default::default).write(),
rec,
),
RecordingScope::ThreadLocal => LOCAL_DATA_RECORDING.with(|cell| {
let mut cell = cell.borrow_mut();
std::mem::replace(&mut *cell, rec)
}),
RecordingScope::ThreadLocal => {
LOCAL_DATA_RECORDING.with(|cell| cell.borrow_mut().replace(rec))
}
},
StoreKind::Blueprint => match scope {
RecordingScope::Global => std::mem::replace(
Expand All @@ -226,10 +258,9 @@ impl RecordingStream {
.write(),
rec,
),
RecordingScope::ThreadLocal => LOCAL_BLUEPRINT_RECORDING.with(|cell| {
let mut cell = cell.borrow_mut();
std::mem::replace(&mut *cell, rec)
}),
RecordingScope::ThreadLocal => {
LOCAL_BLUEPRINT_RECORDING.with(|cell| cell.borrow_mut().replace(rec))
}
},
}
}
Expand All @@ -244,9 +275,7 @@ impl RecordingStream {
}
}
RecordingScope::ThreadLocal => LOCAL_DATA_RECORDING.with(|cell| {
if let Some(cell) = cell.take() {
std::mem::forget(cell);
}
std::mem::forget(cell.take());
}),
},
StoreKind::Blueprint => match scope {
Expand All @@ -256,9 +285,7 @@ impl RecordingStream {
}
}
RecordingScope::ThreadLocal => LOCAL_BLUEPRINT_RECORDING.with(|cell| {
if let Some(cell) = cell.take() {
std::mem::forget(cell);
}
std::mem::forget(cell.take());
}),
},
}
Expand Down
16 changes: 16 additions & 0 deletions crates/re_sdk/src/recording_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1620,4 +1620,20 @@ mod tests {
// That's all.
assert!(msgs.pop().is_none());
}

#[test]
fn test_set_thread_local() {
// Regression-test for https://github.com/rerun-io/rerun/issues/2889
std::thread::Builder::new()
.name("test_thead".to_owned())
.spawn(|| {
let stream = RecordingStreamBuilder::new("rerun_example_test")
.buffered()
.unwrap();
RecordingStream::set_thread_local(StoreKind::Recording, Some(stream));
})
.unwrap()
.join()
.unwrap();
}
}
Loading