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 spawn
ed thread on join
ing. 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); }
Comments
Post a Comment