Skip to content

Commit e441d84

Browse files
Adam GibsonAdam Gibson
Adam Gibson
authored and
Adam Gibson
committed
Prevent runtime crash on exit
This spawns the verifier RPC call in a thread to remove the crash message when the window is quit out from. Also tidied up some debug statements and comments.
1 parent b384346 commit e441d84

File tree

3 files changed

+16
-28
lines changed

3 files changed

+16
-28
lines changed

src/bin/gui.rs

+16-28
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ impl ProcessGuard {
2929
}
3030

3131
// kills subprocess (here autct) on shutdown.
32-
// while this can cause substantial delays, we otherwise
33-
// would need to sync a previously running process
34-
// with current config, which is a bunch more mess.
32+
// While it would be more convenient to allow
33+
// attaching to a pre-existing server, to prevent
34+
// startup delays, this is simpler for now, and
35+
// avoids potential confusion/more code logic.
3536
impl Drop for ProcessGuard {
3637
fn drop(&mut self) {
37-
println!("drop is getting called now");
3838
// TODO is it even possible to
3939
// handle the case where kill fails to kill?
4040
let _ = self.child.kill();
@@ -46,7 +46,6 @@ struct Wrapper {
4646
wrapped_app: Arc<RwLock<AutctVerifierApp>>,
4747
}
4848

49-
5049
struct AutctVerifierApp {
5150
autctcfg: Option<AutctConfig>,
5251
sigmessage: Option<String>,
@@ -68,7 +67,6 @@ struct AutctVerifierApp {
6867
verif_result: Arc<Mutex<Option<String>>>,
6968
// this remembers the last RPC response we saw:
7069
last_verif_result: Option<String>,
71-
verif_runtime: Arc<Runtime>,
7270
server_state: Arc<Mutex<ServerState>>,
7371
autct_process_guard: Option<ProcessGuard>,
7472
}
@@ -85,11 +83,11 @@ enum ServerState {
8583

8684
#[tokio::main]
8785
async fn main() -> eframe::Result {
88-
// make a read-only copy of the app for the
89-
// monitoring thread:
9086
let mut myapp = AutctVerifierApp::default();
9187
myapp.try_load_autctcfg();
9288
let myapplock = Arc::new(RwLock::new(myapp));
89+
// make a copy of the app lock for the
90+
// monitoring thread:
9391
let shared_myapplock = Arc::clone(&myapplock);
9492

9593
thread::spawn(move || {
@@ -99,28 +97,19 @@ async fn main() -> eframe::Result {
9997
let lockres = shared_myapplock.try_read();
10098
if !lockres.is_err() {
10199
let app_for_reading = lockres.unwrap();
102-
// println-s here for now to sanity check the monitoring echo RPC
103-
// calls are behaving as expected:
104100
if !app_for_reading.autctcfg.is_none() {
105-
//println!("autctcfg was not none in the monitoring thread");
106101
let rt = Runtime::new().unwrap();
107102
let res = rt.block_on(
108103
request_echo(&app_for_reading.autctcfg.as_ref().unwrap()));
109-
//println!("Request echo method returned");
110104
match res {
111-
Ok(_) => {println!("Echo successful");
105+
Ok(_) => {println!("Echo successful; server is up.");
112106
let mut state = app_for_reading
113107
.server_state.lock().unwrap();
114108
*state = ServerState::Ready;
115109
break;},
116-
Err(_) => {
117-
//println!("Echo call returned an error");
118-
}
110+
Err(_) => {}
119111
} // end match
120112
} // end autctcfg none check
121-
else {
122-
println!("in monitoring thread, autctcfg was none");
123-
}
124113
} // end check for lock
125114
// Sleep for 1 second before the next check
126115
thread::sleep(Duration::from_secs(1));
@@ -165,7 +154,6 @@ impl AutctVerifierApp {
165154
|| CustomError("Failed to load signature message".to_string()))?);
166155
}
167156
cfg.bc_network = Some(self.network.clone());
168-
println!("Finishing check cfg data ok");
169157
Ok(cfg)
170158
}
171159

@@ -184,8 +172,11 @@ impl AutctVerifierApp {
184172
self.is_verif_loading = true;
185173
let result = Arc::clone(&self.verif_result);
186174
let cfgclone = self.autctcfg.clone().unwrap();
187-
self.verif_runtime.spawn(async move {
188-
let res = AutctVerifierApp::verify(cfgclone).await;
175+
// logic here is as for `request_echo` above:
176+
thread::spawn(move || {
177+
let rt = Runtime::new().unwrap();
178+
let res = rt.block_on(
179+
AutctVerifierApp::verify(cfgclone));
189180
*result.lock().unwrap() = Some(res);
190181
});
191182
}
@@ -227,7 +218,6 @@ impl AutctVerifierApp {
227218
dot_pos: &Pos2, dot_radius: f32) {
228219
// Determine the color of the dot based on the verification
229220
// server(called in the background)'s state:
230-
//println!("Got into update dot");
231221
let process_state = *self.server_state.lock().unwrap();
232222
let dot_color = match process_state {
233223
ServerState::NotStarted => Color32::BLUE,
@@ -249,7 +239,6 @@ impl AutctVerifierApp {
249239

250240
// Handle click on the circle
251241
if response.clicked() && process_state == ServerState::NotStarted {
252-
println!("Circle clicked! Starting the process...");
253242
self.start_server();
254243
}
255244
});
@@ -270,7 +259,6 @@ impl Default for AutctVerifierApp {
270259
is_verif_loading: false,
271260
verif_result: Arc::new(Mutex::new(None)),
272261
last_verif_result: None,
273-
verif_runtime: Arc::new(Runtime::new().unwrap()),
274262
server_state: Arc::new(Mutex::new(ServerState::NotStarted)),
275263
autct_process_guard: None,
276264
}
@@ -280,7 +268,7 @@ impl Default for AutctVerifierApp {
280268
impl eframe::App for Wrapper {
281269
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
282270
// Point of discussion: I think dark mode will work
283-
// a little better than light:
271+
// a little better than light for most users:
284272
//ctx.set_visuals(egui::Visuals::light());
285273

286274
// GUI does not respond to updates unless
@@ -306,7 +294,8 @@ impl eframe::App for Wrapper {
306294
.min_height(available_height * 0.25)
307295
.show(ctx, |ui| {
308296
ui.set_enabled(!(
309-
state.show_verif_modal || state.show_conn_modal || state.show_verifres_modal));
297+
state.show_verif_modal || state.show_conn_modal ||
298+
state.show_verifres_modal));
310299
if ui.add_sized([200.0, 60.0],
311300
egui::Button::new(RichText::new(
312301
"VERIFY").size(30.0).strong())).clicked() {
@@ -396,7 +385,6 @@ impl eframe::App for Wrapper {
396385
});
397386
egui::CentralPanel::default().show(ctx, |ui| {
398387
egui::ScrollArea::vertical().show(ui, |ui| {
399-
//println!("Starting the central panel paint");
400388
ui.set_enabled(!(state.show_verif_modal || state.show_conn_modal || state.show_verifres_modal));
401389
ui.label(RichText::new(
402390
"Choose keyset (.pks) file:").size(28.0).strong());
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)