refactor(script-rs): idiomatic Rust, better error handling, doc comments, SIGINT cleanup#24
Conversation
Agent-Logs-Url: https://github.com/rayworks/DroidCast/sessions/6c11ad0f-0e9e-4e6f-a916-8f5baafca9c1 Co-authored-by: rayworks <1329281+rayworks@users.noreply.github.com>
| /// main thread is not interrupted. | ||
| fn setup_signal_handler() -> Result<(), Box<dyn Error>> { | ||
| let mut signals = Signals::new(&[SIGINT])?; | ||
| let mut signals = Signals::new([SIGINT])?; |
There was a problem hiding this comment.
The main thread should be interrupted at this time. So can you call unforward_connection method when receiving the SIGINT signal ?
@copilot
There was a problem hiding this comment.
Done in f0b9cb1. setup_signal_handler now accepts port: String and serial: Option<String>, moves them into the handler thread, and calls unforward_connection on SIGINT. The call is placed in main right after forward_connection, so the handler only runs cleanup for a forward that was actually established.
Agent-Logs-Url: https://github.com/rayworks/DroidCast/sessions/437b5153-d611-42e7-b542-285cd2b51ae5 Co-authored-by: rayworks <1329281+rayworks@users.noreply.github.com>
|
|
||
| // unforward | ||
| unforward_connection(&port); | ||
| unforward_connection(port, serial); |
There was a problem hiding this comment.
This redundant call should be removed since it will be called in the signal handler.
There was a problem hiding this comment.
Removed in 629c813. Cleanup is now exclusively the signal handler's responsibility.
| let mut signals = Signals::new([SIGINT])?; | ||
|
|
||
| thread::spawn(move || { | ||
| for sig in signals.forever() { |
There was a problem hiding this comment.
Once received the SIGINT, the thread should return, with a JoinHandle waiting for the thread exit.
There was a problem hiding this comment.
Done in 629c813. setup_signal_handler now returns (thread::JoinHandle<()>, Handle). The thread uses signals.forever().next() — it processes one SIGINT and then returns. In main, after startup_service_and_wait, signals_close.close() is called (unblocks the thread on a normal exit) and signal_handle.join() waits for it to finish in both the SIGINT and normal-exit paths.
…INT with JoinHandle Agent-Logs-Url: https://github.com/rayworks/DroidCast/sessions/486e392b-0cac-4f4e-b726-26b846266b47 Co-authored-by: rayworks <1329281+rayworks@users.noreply.github.com>
The
script-rsCLI had several code-quality issues:&Stringparams, empty-string sentinels, repeatedenv::args()reads per call, bareunwrap()on I/O, andString::from("x") + &yconcatenation.src/main.rsmainasOption<&str>and threaded through all helpers;serial_checked_commandnow acceptsserial: Option<&str>instead of re-readingenv::args()on every invocationlocate_apk_path→Option<String>— replaces the empty-string sentinel with a properNonereturn&String→&strin all function signaturesopen_browser(port, serial)— accepts parameters instead of re-reading argsformat!everywhere — replacesString::from("x") + &yand the redundantprintln!("{}", format!(…))split_once(':')— replaces the fragile.split(":").last()expectmessages on every I/O.unwrap()///doc comments added to every top-level functionuse webbrowser;bare importsetup_signal_handleracceptsport: Stringandserial: Option<String>, moves them into a dedicated background thread, and invokesunforward_connectionon Ctrl-C. The handler is registered inmainafterforward_connection(to avoid spurious cleanup on early exits) and is the sole placeunforward_connectionis called. The thread processes exactly one SIGINT viasignals.forever().next()and then returns.setup_signal_handlerreturns(thread::JoinHandle<()>, Handle)— afterstartup_service_and_wait,maincallssignals_close.close()to unblock the thread on a normal exit and thensignal_handle.join()to wait for cleanup in both the SIGINT and clean-exit paths.Cargo.tomldescriptionandrepositoryfields; dropped the vacuousauthors = []Original prompt
This pull request was created from Copilot chat.