mirror of
https://github.com/neosubhamoy/pytubepp-helper.git
synced 2026-02-04 11:22:22 +05:30
(fixed): occasional port binding errors on server restart
This commit is contained in:
@@ -34,6 +34,25 @@ struct WebSocketState {
|
|||||||
config: Config,
|
config: Config,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn is_port_available(port: u16) -> bool {
|
||||||
|
match TcpListener::bind(format!("127.0.0.1:{}", port)).await {
|
||||||
|
Ok(_) => true,
|
||||||
|
Err(_) => false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn wait_for_port_availability(port: u16, max_attempts: u32) -> Result<(), String> {
|
||||||
|
let mut attempts = 0;
|
||||||
|
while attempts < max_attempts {
|
||||||
|
if is_port_available(port).await {
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
sleep(Duration::from_millis(500)).await;
|
||||||
|
attempts += 1;
|
||||||
|
}
|
||||||
|
Err(format!("Port {} did not become available after {} attempts", port, max_attempts))
|
||||||
|
}
|
||||||
|
|
||||||
async fn start_websocket_server(app_handle: tauri::AppHandle, port: u16) -> Result<(), String> {
|
async fn start_websocket_server(app_handle: tauri::AppHandle, port: u16) -> Result<(), String> {
|
||||||
let addr = format!("127.0.0.1:{}", port);
|
let addr = format!("127.0.0.1:{}", port);
|
||||||
|
|
||||||
@@ -43,15 +62,22 @@ async fn start_websocket_server(app_handle: tauri::AppHandle, port: u16) -> Resu
|
|||||||
let mut state = state.lock().await;
|
let mut state = state.lock().await;
|
||||||
if let Some(old_abort) = state.server_abort.take() {
|
if let Some(old_abort) = state.server_abort.take() {
|
||||||
let _ = old_abort.send(());
|
let _ = old_abort.send(());
|
||||||
// Give it a moment to shut down
|
// Wait for the port to become available
|
||||||
sleep(Duration::from_millis(200)).await;
|
wait_for_port_availability(port, 6).await?; // Try for 3 seconds (6 attempts * 500ms)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Now try to bind to the port
|
// Now try to bind to the port
|
||||||
let listener = TcpListener::bind(&addr)
|
let listener = match TcpListener::bind(&addr).await {
|
||||||
.await
|
Ok(l) => l,
|
||||||
.map_err(|e| format!("Failed to bind to port {}: {}", port, e))?;
|
Err(_e) => {
|
||||||
|
// One final attempt to wait and retry
|
||||||
|
sleep(Duration::from_secs(1)).await;
|
||||||
|
TcpListener::bind(&addr)
|
||||||
|
.await
|
||||||
|
.map_err(|e| format!("Failed to bind to port {}: {}", port, e))?
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
let (abort_sender, mut abort_receiver) = tokio::sync::oneshot::channel();
|
let (abort_sender, mut abort_receiver) = tokio::sync::oneshot::channel();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user