rust - How do I ensure that a spawned Child process is killed if my app panics? -


i'm writing small test starts daemon process , tests e.g:

let server = command::new("target/debug/server").spawn();  // tests  server.kill(); 

the typical way fail test panic. unfortunately means kill() never gets invoked , repeated runs of test suite fail, because port taken old process still running.

is there trap function can use ensure child gets killed?

you can put possibly-panicking code closure , give closure catch_panic. catch_panic acts same way scoped or spawned thread on joining. returns result either ok(closureretval) or err(box<any>) if closure panicked.

let res = std::thread::catch_panic(|| {     panic!("blub: {}", 35); }); if let err(err) = res {     let msg: string = *err.downcast().unwrap();     println!("{}", msg); } 

playpen


Comments

Popular posts from this blog

Java 3D LWJGL collision -

spring - SubProtocolWebSocketHandler - No handlers -

methods - python can't use function in submodule -