From 11428f65e74f684d3e1fd7d20d5a1111f0685021 Mon Sep 17 00:00:00 2001 From: Herbert Wolverson Date: Mon, 23 Jan 2023 21:59:02 +0000 Subject: [PATCH] Fix "lqosd is running" error when it clearly isn't. * Fix the Python code to actually call `is_lqosd_alive()` instead of just checking that it exists (`is_lqosd_alive`). * Fix the os.exit command syntax. * Cleanup the blocking Tokio/lqosd request handler to pass better messages. * Catch the "file not found" and replace it with a nicer message. --- src/LibreQoS.py | 4 +- src/rust/lqos_bus/src/bus/client.rs | 56 ++++++++++++++++++---------- src/rust/lqos_python/src/blocking.rs | 3 +- 3 files changed, 40 insertions(+), 23 deletions(-) diff --git a/src/LibreQoS.py b/src/LibreQoS.py index bd9c2281..4f807baa 100755 --- a/src/LibreQoS.py +++ b/src/LibreQoS.py @@ -1250,11 +1250,11 @@ def refreshShapersUpdateOnly(): print("refreshShapersUpdateOnly completed on " + datetime.now().strftime("%d/%m/%Y %H:%M:%S")) if __name__ == '__main__': - if is_lqosd_alive: + if is_lqosd_alive(): print("lqosd is running") else: print("ERROR: lqosd is not running. Aborting") - os.exit() + os._exit(-1) parser = argparse.ArgumentParser() parser.add_argument( diff --git a/src/rust/lqos_bus/src/bus/client.rs b/src/rust/lqos_bus/src/bus/client.rs index 3ea5f6c5..017bbead 100644 --- a/src/rust/lqos_bus/src/bus/client.rs +++ b/src/rust/lqos_bus/src/bus/client.rs @@ -1,26 +1,44 @@ -use tokio::{net::UnixStream, io::{AsyncWriteExt, AsyncReadExt}}; -use crate::{BUS_SOCKET_PATH, BusSession, BusRequest, encode_request, decode_response, BusResponse}; -use anyhow::Result; use super::PREALLOCATE_CLIENT_BUFFER_BYTES; +use crate::{ + decode_response, encode_request, BusRequest, BusResponse, BusSession, + BUS_SOCKET_PATH, +}; +use anyhow::{Error, Result}; +use tokio::{ + io::{AsyncReadExt, AsyncWriteExt}, + net::UnixStream, +}; /// Convenient wrapper for accessing the bus -/// +/// /// ## Arguments -/// +/// /// * `requests` a vector of `BusRequest` requests to make. -/// +/// /// **Returns** Either an error, or a vector of `BusResponse` replies -pub async fn bus_request(requests: Vec) -> Result> { - let mut stream = UnixStream::connect(BUS_SOCKET_PATH).await.unwrap(); - let test = BusSession { - persist: false, - requests, - }; - let msg = encode_request(&test)?; - stream.write(&msg).await?; - let mut buf = Vec::with_capacity(PREALLOCATE_CLIENT_BUFFER_BYTES); - let _ = stream.read_to_end(&mut buf).await.unwrap(); - let reply = decode_response(&buf)?; +pub async fn bus_request( + requests: Vec, +) -> Result> { + let stream = UnixStream::connect(BUS_SOCKET_PATH).await; + match &stream { + Err(e) => match e.kind() { + std::io::ErrorKind::NotFound => { + return Err(Error::msg(format!( + "{} not found. Check permissions and that lqosd is running.", + BUS_SOCKET_PATH + ))) + } + _ => {} + }, + _ => {} + } + let mut stream = stream.unwrap(); + let test = BusSession { persist: false, requests }; + let msg = encode_request(&test)?; + stream.write(&msg).await?; + let mut buf = Vec::with_capacity(PREALLOCATE_CLIENT_BUFFER_BYTES); + let _ = stream.read_to_end(&mut buf).await?; + let reply = decode_response(&buf)?; - Ok(reply.responses) -} \ No newline at end of file + Ok(reply.responses) +} diff --git a/src/rust/lqos_python/src/blocking.rs b/src/rust/lqos_python/src/blocking.rs index 05a9acbb..5ddeba1b 100644 --- a/src/rust/lqos_python/src/blocking.rs +++ b/src/rust/lqos_python/src/blocking.rs @@ -7,8 +7,7 @@ pub fn run_query(requests: Vec) -> Result> { let mut replies = Vec::with_capacity(8); tokio::runtime::Builder::new_current_thread() .enable_all() - .build() - .unwrap() + .build()? .block_on(async { replies.extend_from_slice(&bus_request(requests).await?); Ok(replies)