From 8b769e035b978deea95d105441f01b65ac15f302 Mon Sep 17 00:00:00 2001 From: Nick Steel Date: Mon, 7 Oct 2024 06:54:16 +0100 Subject: [PATCH] core: audio key response timeout after 1.5s (#1360) --- core/src/audio_key.rs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/core/src/audio_key.rs b/core/src/audio_key.rs index 74be4258..0ffa8383 100644 --- a/core/src/audio_key.rs +++ b/core/src/audio_key.rs @@ -1,4 +1,4 @@ -use std::{collections::HashMap, io::Write}; +use std::{collections::HashMap, io::Write, time::Duration}; use byteorder::{BigEndian, ByteOrder, WriteBytesExt}; use bytes::Bytes; @@ -20,6 +20,8 @@ pub enum AudioKeyError { Packet(u8), #[error("sequence {0} not pending")] Sequence(u32), + #[error("audio key response timeout")] + Timeout, } impl From for Error { @@ -29,6 +31,7 @@ impl From for Error { AudioKeyError::Channel => Error::aborted(err), AudioKeyError::Sequence(_) => Error::aborted(err), AudioKeyError::Packet(_) => Error::unimplemented(err), + AudioKeyError::Timeout => Error::aborted(err), } } } @@ -89,7 +92,14 @@ impl AudioKeyManager { }); self.send_key_request(seq, track, file)?; - rx.await? + const KEY_RESPONSE_TIMEOUT: Duration = Duration::from_millis(1500); + match tokio::time::timeout(KEY_RESPONSE_TIMEOUT, rx).await { + Err(_) => { + error!("Audio key response timeout"); + Err(AudioKeyError::Timeout.into()) + } + Ok(k) => k?, + } } fn send_key_request(&self, seq: u32, track: SpotifyId, file: FileId) -> Result<(), Error> {