|
1 | 1 | use jack_sys as j; |
2 | 2 | use std::fmt; |
3 | 3 | use std::fmt::Debug; |
4 | | -use std::mem; |
5 | 4 | use std::sync::atomic::AtomicBool; |
6 | 5 |
|
7 | 6 | use super::callbacks::clear_callbacks; |
@@ -49,33 +48,47 @@ where |
49 | 48 | /// methods specified by the `NotificationHandler` and `ProcessHandler` objects. |
50 | 49 | /// |
51 | 50 | /// On failure, either `Err(Error::CallbackRegistrationError)` or |
52 | | - /// `Err(Error::ClientActivationError)` is returned. |
| 51 | + /// `Err(Error::ClientActivationError)` is returned. The client is closed and both handlers |
| 52 | + /// are dropped. |
53 | 53 | /// |
54 | 54 | /// `notification_handler` and `process_handler` are consumed, but they are returned when |
55 | 55 | /// `Client::deactivate` is called. |
56 | 56 | pub fn new(client: Client, notification_handler: N, process_handler: P) -> Result<Self, Error> { |
| 57 | + Self::new_with_activation( |
| 58 | + client, |
| 59 | + notification_handler, |
| 60 | + process_handler, |
| 61 | + |client| unsafe { j::jack_activate(client.raw()) }, |
| 62 | + ) |
| 63 | + } |
| 64 | + |
| 65 | + // Inject activation so tests can exercise failures independently of the JACK implementation. |
| 66 | + fn new_with_activation( |
| 67 | + client: Client, |
| 68 | + notification_handler: N, |
| 69 | + process_handler: P, |
| 70 | + activate: impl FnOnce(&Client) -> libc::c_int, |
| 71 | + ) -> Result<Self, Error> { |
| 72 | + let mut callback_context = Box::new(CallbackContext { |
| 73 | + client, |
| 74 | + notification: notification_handler, |
| 75 | + process: process_handler, |
| 76 | + is_valid_for_callback: AtomicBool::new(true), |
| 77 | + has_panic: AtomicBool::new(false), |
| 78 | + callback_lock: std::sync::Mutex::new(()), |
| 79 | + }); |
| 80 | + // Declare the guard after the context so it is released before error cleanup: |
| 81 | + // Client::drop also acquires this mutex. |
57 | 82 | let _m = CREATE_OR_DESTROY_CLIENT_MUTEX.lock().ok(); |
58 | 83 | unsafe { |
59 | | - let mut callback_context = Box::new(CallbackContext { |
60 | | - client, |
61 | | - notification: notification_handler, |
62 | | - process: process_handler, |
63 | | - is_valid_for_callback: AtomicBool::new(true), |
64 | | - has_panic: AtomicBool::new(false), |
65 | | - callback_lock: std::sync::Mutex::new(()), |
66 | | - }); |
67 | 84 | CallbackContext::register_callbacks(&mut callback_context)?; |
68 | | - let res = j::jack_activate(callback_context.client.raw()); |
69 | | - match res { |
70 | | - 0 => Ok(AsyncClient { |
71 | | - callback: Some(callback_context), |
72 | | - }), |
73 | | - _ => { |
74 | | - mem::forget(callback_context); |
75 | | - Err(Error::ClientActivationError) |
76 | | - } |
77 | | - } |
78 | 85 | } |
| 86 | + if activate(&callback_context.client) != 0 { |
| 87 | + return Err(Error::ClientActivationError); |
| 88 | + } |
| 89 | + Ok(AsyncClient { |
| 90 | + callback: Some(callback_context), |
| 91 | + }) |
79 | 92 | } |
80 | 93 | } |
81 | 94 |
|
@@ -151,3 +164,81 @@ impl<N, P> AsRef<Client> for AsyncClient<N, P> { |
151 | 164 | self.as_client() |
152 | 165 | } |
153 | 166 | } |
| 167 | + |
| 168 | +#[cfg(test)] |
| 169 | +mod tests { |
| 170 | + use super::*; |
| 171 | + use crate::{AudioIn, ClientOptions, Control, Port, ProcessScope, Unowned}; |
| 172 | + use std::sync::{ |
| 173 | + atomic::{AtomicUsize, Ordering}, |
| 174 | + mpsc::{sync_channel, SyncSender}, |
| 175 | + Arc, |
| 176 | + }; |
| 177 | + |
| 178 | + struct Handler { |
| 179 | + drops: Arc<AtomicUsize>, |
| 180 | + port: Port<Unowned>, |
| 181 | + processed: Option<SyncSender<()>>, |
| 182 | + } |
| 183 | + |
| 184 | + impl NotificationHandler for Handler {} |
| 185 | + |
| 186 | + impl ProcessHandler for Handler { |
| 187 | + fn process(&mut self, _: &Client, _: &ProcessScope) -> Control { |
| 188 | + assert_eq!(self.drops.load(Ordering::SeqCst), 0); |
| 189 | + if let Some(sender) = &self.processed { |
| 190 | + sender.try_send(()).ok(); |
| 191 | + } |
| 192 | + Control::Continue |
| 193 | + } |
| 194 | + } |
| 195 | + |
| 196 | + impl Drop for Handler { |
| 197 | + fn drop(&mut self) { |
| 198 | + // The client must be closed before either handler is destroyed. |
| 199 | + assert_eq!(self.port.name(), Err(Error::ClientIsNoLongerAlive)); |
| 200 | + self.drops.fetch_add(1, Ordering::SeqCst); |
| 201 | + } |
| 202 | + } |
| 203 | + |
| 204 | + fn check_activation_failure(start_processing: bool) { |
| 205 | + let (client, _) = Client::new("", ClientOptions::default()).unwrap(); |
| 206 | + let port = client.register_port("input", AudioIn::default()).unwrap(); |
| 207 | + let notification_drops = Arc::new(AtomicUsize::new(0)); |
| 208 | + let process_drops = Arc::new(AtomicUsize::new(0)); |
| 209 | + let (send, recv) = sync_channel(1); |
| 210 | + let notification = Handler { |
| 211 | + drops: notification_drops.clone(), |
| 212 | + port: port.clone_unowned(), |
| 213 | + processed: None, |
| 214 | + }; |
| 215 | + let process = Handler { |
| 216 | + drops: process_drops.clone(), |
| 217 | + port: port.clone_unowned(), |
| 218 | + processed: Some(send), |
| 219 | + }; |
| 220 | + let result = AsyncClient::new_with_activation(client, notification, process, |client| { |
| 221 | + if start_processing { |
| 222 | + // Model a failure after JACK has started using the registered callback pointers. |
| 223 | + assert_eq!(unsafe { j::jack_activate(client.raw()) }, 0); |
| 224 | + recv.recv_timeout(std::time::Duration::from_secs(2)) |
| 225 | + .unwrap(); |
| 226 | + } |
| 227 | + -1 |
| 228 | + }); |
| 229 | + assert_eq!(result.err(), Some(Error::ClientActivationError)); |
| 230 | + assert_eq!(notification_drops.load(Ordering::SeqCst), 1); |
| 231 | + assert_eq!(process_drops.load(Ordering::SeqCst), 1); |
| 232 | + assert_eq!(port.name(), Err(Error::ClientIsNoLongerAlive)); |
| 233 | + } |
| 234 | + |
| 235 | + #[test] |
| 236 | + fn activation_failure_closes_client_and_drops_handlers() { |
| 237 | + check_activation_failure(false); |
| 238 | + } |
| 239 | + |
| 240 | + #[test] |
| 241 | + fn activation_failure_after_start_closes_client_and_drops_handlers() { |
| 242 | + check_activation_failure(true); |
| 243 | + } |
| 244 | +} |
0 commit comments