-
Notifications
You must be signed in to change notification settings - Fork 0
Optimization #30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Optimization #30
Changes from all commits
9e35178
1d95ad0
18789c4
95591fd
7dc5415
7a34f18
297f3a3
32182fd
7af8109
aef68a0
1d0e0fb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,14 @@ | ||
| use std::sync::Arc; | ||
| use std::sync::atomic::AtomicBool; | ||
|
|
||
| #[derive(Debug, Default)] | ||
| pub struct Flags { | ||
| pub is_hidden: bool, | ||
| pub is_scanning: Arc<AtomicBool>, | ||
| pub show_delete_confirmation: bool, | ||
| pub show_help: bool, | ||
| pub show_password_popup: bool, | ||
| pub show_saved: bool, | ||
| pub show_ssid_popup: bool, | ||
| pub show_status_popup: bool, | ||
| pub show_delete_confirmation: bool, | ||
| pub show_saved: bool, | ||
| pub show_help: bool, | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,14 @@ | ||
| use crate::WifiNetwork; | ||
| use crate::utils::saved_connection::saved_connections; | ||
| use std::process::Command; | ||
| use std::sync::{Arc, Mutex}; | ||
| use std::sync::atomic::{AtomicBool, Ordering}; | ||
| use std::sync::{Arc, RwLock}; | ||
| use std::thread; | ||
|
|
||
| pub fn scan_networks(wifi_list: Arc<Mutex<Vec<WifiNetwork>>>) { | ||
| pub fn scan_networks(wifi_list: Arc<RwLock<Vec<WifiNetwork>>>, is_scanning: Arc<AtomicBool>) { | ||
| is_scanning.store(true, Ordering::SeqCst); | ||
|
Comment on lines
+8
to
+9
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Critical: Scanning flag can be stuck at The 🔎 Recommended fix using a scope guard pattern pub fn scan_networks(wifi_list: Arc<RwLock<Vec<WifiNetwork>>>, is_scanning: Arc<AtomicBool>) {
- is_scanning.store(true, Ordering::SeqCst);
- // // nmcli -t -f IN-USE,SSID,SECURITY device wifi list
thread::spawn(move || {
+ is_scanning.store(true, Ordering::SeqCst);
+ // Ensure the flag is reset when the thread exits (success, error, or panic)
+ let _guard = scopeguard::guard((), |_| {
+ is_scanning.store(false, Ordering::SeqCst);
+ });
+
let output = Command::new("nmcli")
.args(["-t", "-f", "IN-USE,SSID,SECURITY", "device", "wifi", "list"])
.output()
- .expect("Failed to execute nmcli command");
+ .ok()?;
if !output.status.success() {
- return;
+ return None;
}
let stdout = String::from_utf8_lossy(&output.stdout);
let mut networks: Vec<WifiNetwork> = Vec::new();
let saved_networks = saved_connections();
for line in stdout.lines() {
let mut parts = line.splitn(3, ':');
let in_use = parts.next() == Some("*");
let ssid = parts.next().unwrap_or("").to_string();
let security = parts
.next()
.map(|s| {
if s.is_empty() {
"Unsecured".to_string()
} else {
s.to_string()
}
})
.unwrap_or("?".to_string());
let is_saved = saved_networks.contains(&ssid);
if !ssid.is_empty() {
networks.push(WifiNetwork {
is_saved,
in_use,
ssid,
security,
})
}
}
networks.push(WifiNetwork {
is_saved: false,
in_use: false,
ssid: "Connect to Hidden network".to_string(),
security: "?".to_string(),
});
- let mut wifi_list_lock = wifi_list.write().expect("WifiNetworks lock poisoned");
+ let mut wifi_list_lock = wifi_list.write().ok()?;
*wifi_list_lock = networks;
- is_scanning.store(false, Ordering::SeqCst);
+ Some(())
});
}Note: This requires adding the
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you create issue related to that I will fix it later
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: Length of output: 51 @santoshxshrestha, I've created issue #31 to track this bug. The issue describes the problem where the 🧬 Code Graph Analysis Results[src/utils/saved_connection.rs] lines 3-28 [src/utils/scan.rs] (function: scan_networks)
|
||
| // // nmcli -t -f IN-USE,SSID,SECURITY device wifi list | ||
| thread::spawn(move || { | ||
| let mut wifi_list_lock = wifi_list.lock().unwrap(); | ||
| let output = Command::new("nmcli") | ||
| .args(["-t", "-f", "IN-USE,SSID,SECURITY", "device", "wifi", "list"]) | ||
| .output() | ||
|
|
@@ -55,6 +56,9 @@ pub fn scan_networks(wifi_list: Arc<Mutex<Vec<WifiNetwork>>>) { | |
| ssid: "Connect to Hidden network".to_string(), | ||
| security: "?".to_string(), | ||
| }); | ||
|
|
||
| let mut wifi_list_lock = wifi_list.write().expect("WifiNetworks lock poisoned"); | ||
|
vimlinuz marked this conversation as resolved.
|
||
| *wifi_list_lock = networks; | ||
| is_scanning.store(false, Ordering::SeqCst); | ||
| }); | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.