From 883d00198892c982330de302fb0276e01f87c3cf Mon Sep 17 00:00:00 2001 From: Cyrix126 Date: Fri, 11 Oct 2024 19:15:33 +0200 Subject: [PATCH] fix: more deadlocks --- src/helper/tests.rs | 16 ++++++++++++++-- src/helper/xrig/xmrig.rs | 17 ++++++++++------- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/src/helper/tests.rs b/src/helper/tests.rs index 2a2ae81..f3e7c33 100644 --- a/src/helper/tests.rs +++ b/src/helper/tests.rs @@ -329,12 +329,24 @@ Uptime = 0h 2m 4s ))); process.lock().unwrap().state = ProcessState::Alive; - PubXmrigApi::update_from_output(&public, &output_parse, &output_pub, elapsed, &process); + PubXmrigApi::update_from_output( + &mut public.lock().unwrap(), + &output_parse, + &output_pub, + elapsed, + &mut process.lock().unwrap(), + ); println!("{:#?}", process); assert!(process.lock().unwrap().state == ProcessState::NotMining); let output_parse = Arc::new(Mutex::new(String::from("[2022-02-12 12:49:30.311] net new job from 192.168.2.1:3333 diff 402K algo rx/0 height 2241142 (11 tx)"))); - PubXmrigApi::update_from_output(&public, &output_parse, &output_pub, elapsed, &process); + PubXmrigApi::update_from_output( + &mut public.lock().unwrap(), + &output_parse, + &output_pub, + elapsed, + &mut process.lock().unwrap(), + ); assert!(process.lock().unwrap().state == ProcessState::Alive); } diff --git a/src/helper/xrig/xmrig.rs b/src/helper/xrig/xmrig.rs index a079c5d..d718230 100644 --- a/src/helper/xrig/xmrig.rs +++ b/src/helper/xrig/xmrig.rs @@ -532,13 +532,17 @@ impl Helper { } // Always update from output debug!("XMRig Watchdog | Starting [update_from_output()]"); + let mut process_lock = process.lock().unwrap(); + let mut pub_api_lock = pub_api.lock().unwrap(); PubXmrigApi::update_from_output( - &pub_api, + &mut pub_api_lock, &output_pub, &output_parse, start.elapsed(), - &process, + &mut process_lock, ); + drop(pub_api_lock); + drop(process_lock); // Send an HTTP API request debug!("XMRig Watchdog | Attempting HTTP API request..."); match PrivXmrigApi::request_xmrig_api(&client, &api_uri, token).await { @@ -743,17 +747,16 @@ impl PubXmrigApi { // This combines the buffer from the PTY thread [output_pub] // with the actual [PubApiXmrig] output field. pub fn update_from_output( - public: &Arc>, + public: &mut Self, output_parse: &Arc>, output_pub: &Arc>, elapsed: std::time::Duration, - process: &Arc>, + process: &mut Process, ) { // 1. Take the process's current output buffer and combine it with Pub (if not empty) let mut output_pub = output_pub.lock().unwrap(); { - let mut public = public.lock().unwrap(); if !output_pub.is_empty() { public.output.push_str(&std::mem::take(&mut *output_pub)); } @@ -764,9 +767,9 @@ impl PubXmrigApi { // 2. Check for "new job"/"no active...". let mut output_parse = output_parse.lock().unwrap(); if XMRIG_REGEX.new_job.is_match(&output_parse) { - process.lock().unwrap().state = ProcessState::Alive; + process.state = ProcessState::Alive; } else if XMRIG_REGEX.not_mining.is_match(&output_parse) { - process.lock().unwrap().state = ProcessState::NotMining; + process.state = ProcessState::NotMining; } // 3. Throw away [output_parse]