Skip to content

Commit 8dc79f3

Browse files
authored
Return error instead of panicking on missing JACK (#177)
Before this trying to initialize a client when JACK was not available would result in an unrecoverable panic because of an unwrap on `jack_sys::library()`. Fixes #176
1 parent 8f4e3ab commit 8dc79f3

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

src/client/client_impl.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,23 @@ unsafe impl Sync for Client {}
4141

4242
impl Client {
4343
/// Opens a JACK client with the given name and options. If the client is successfully opened,
44-
/// then `Ok(client)` is returned. If there is a failure, then `Err(Error::ClientError(status))`
45-
/// will be returned.
44+
/// then `Ok(client)` is returned. If the JACK server returned an error, then
45+
/// `Err(Error::ClientError(status))` will be returned. And if the `dynamic_loading` feature is
46+
/// enabled and the JACK library could not be loaded, an `Err(Error::LibraryError(message))` is
47+
/// returned.
4648
///
4749
/// Although the client may be successful in opening, there still may be some errors minor
4850
/// errors when attempting to opening. To access these, check the returned `ClientStatus`.
4951
pub fn new(client_name: &str, options: ClientOptions) -> Result<(Self, ClientStatus), Error> {
5052
let _m = CREATE_OR_DESTROY_CLIENT_MUTEX.lock().unwrap();
53+
54+
// All of the jack_sys functions below assume the client library is loaded and will panic if
55+
// it is not
56+
#[cfg(feature = "dynamic_loading")]
57+
if let Err(err) = jack_sys::library() {
58+
return Err(Error::LibraryError(err.to_string()));
59+
}
60+
5161
unsafe {
5262
jack_sys::jack_set_error_function(Some(error_handler));
5363
jack_sys::jack_set_info_function(Some(info_handler));

src/jack_enums.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use crate::ClientStatus;
33
/// An error that can occur in JACK.
44
#[derive(Clone, Debug, Eq, PartialEq)]
55
pub enum Error {
6+
LibraryError(String),
67
CallbackDeregistrationError,
78
CallbackRegistrationError,
89
ClientActivationError,

0 commit comments

Comments
 (0)