-
Notifications
You must be signed in to change notification settings - Fork 422
Stop BackgroundProcessor's thread on drop #1007
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -43,7 +43,7 @@ pub struct BackgroundProcessor { | |
| stop_thread: Arc<AtomicBool>, | ||
| /// May be used to retrieve and handle the error if `BackgroundProcessor`'s thread | ||
| /// exits due to an error while persisting. | ||
| pub thread_handle: JoinHandle<Result<(), std::io::Error>>, | ||
| pub thread_handle: Option<JoinHandle<Result<(), std::io::Error>>>, | ||
| } | ||
|
|
||
| #[cfg(not(test))] | ||
|
|
@@ -158,13 +158,27 @@ impl BackgroundProcessor { | |
| } | ||
| } | ||
| }); | ||
| Self { stop_thread: stop_thread_clone, thread_handle: handle } | ||
| Self { stop_thread: stop_thread_clone, thread_handle: Some(handle) } | ||
| } | ||
|
|
||
| /// Stop `BackgroundProcessor`'s thread. | ||
| pub fn stop(self) -> Result<(), std::io::Error> { | ||
| pub fn stop(mut self) -> Result<(), std::io::Error> { | ||
| assert!(self.thread_handle.is_some()); | ||
valentinewallace marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| self.stop_and_join_thread() | ||
| } | ||
|
|
||
| fn stop_and_join_thread(&mut self) -> Result<(), std::io::Error> { | ||
| self.stop_thread.store(true, Ordering::Release); | ||
| self.thread_handle.join().unwrap() | ||
| match self.thread_handle.take() { | ||
| Some(handle) => handle.join().unwrap(), | ||
|
Contributor
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. It seems that if the background thread panics,
Contributor
Author
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. Added docs about panicking to
Collaborator
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. It does feel a bit strange that we have a function that returns a
Contributor
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. Hmm, so the behavior of
Contributor
Author
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.
It will return an
My intention was to mirror the same behavior as I'm not necessarily opposed to making it return the wrapped result, but I think we'd want |
||
| None => Ok(()), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl Drop for BackgroundProcessor { | ||
| fn drop(&mut self) { | ||
| self.stop_and_join_thread().unwrap(); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -416,7 +430,13 @@ mod tests { | |
| let persister = |_: &_| Err(std::io::Error::new(std::io::ErrorKind::Other, "test")); | ||
| let event_handler = |_| {}; | ||
| let bg_processor = BackgroundProcessor::start(persister, event_handler, nodes[0].chain_monitor.clone(), nodes[0].node.clone(), nodes[0].peer_manager.clone(), nodes[0].logger.clone()); | ||
| let _ = bg_processor.thread_handle.join().unwrap().expect_err("Errored persisting manager: test"); | ||
| match bg_processor.stop() { | ||
| Ok(_) => panic!("Expected error persisting manager"), | ||
| Err(e) => { | ||
| assert_eq!(e.kind(), std::io::ErrorKind::Other); | ||
| assert_eq!(e.get_ref().unwrap().to_string(), "test"); | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
|
|
||
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.
Maybe we should make this non-pub now? Users can always use
stoporstop_and_join_thread.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.
Yeah, that would be a good idea. This was
pubto allow waiting on any errors (e.g. from persisting). We'd have to expose ajoinmethod to allow for that behavior still, which also hides theOptionimplementation detail.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.
Wait, how does that differ from
stop?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.
Oh, duh.
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.
stopwill stop the thread (if it hasn't already because of an error) and allows you to examine any errors that may have occurred.joinwill wait for any errors to occur. The latter is only useful if your persisters can give an error. I updated the documentation to spell out these cases in terms ofjoinandstop.