You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Processes may terminate in a number of ways, eg SIGTERM, SIGKILL, machine down. Currently, Madsim does not simulate processes termination very well.
Madsim provides kill function to terminate a host, but the semantic of the kill function is unclear.
See this code:
structFoo(String);implFoo{fnnew(name:&str) -> Foo{println!("foo::new in {}", name);Foo(name.to_owned())}}implDropforFoo{fndrop(&mutself){println!("foo::drop in {}!", self.0);}}#[madsim::main]asyncfnmain(){let handle = madsim::Handle::current().create_host("127.0.0.1:10086").unwrap();
handle.spawn(asyncmove{let _foo = Foo::new("madsim");loop{
madsim::time::sleep(std::time::Duration::from_secs(1)).await;}}).detach();
madsim::time::sleep(std::time::Duration::from_secs(1)).await;
madsim::Handle::current().kill("127.0.0.1:1".parse().unwrap());loop{
madsim::time::sleep(std::time::Duration::from_secs(1)).await;}}
Run this code with features sim enabled, we will find that foo::drop is called. This means that the kill function behaves more like SIGTERM than SIGKILL, and this function cannot simulate some unexpected events (machine failure, out of memory, etc.). We have no way to know that when all async tasks on that host will be dropped, so it's still hard to determine a suitable time to restart host.
If we replace sleep in spawned task with futures::future::pending::<()>().await. We will see that the drop function is not called, which looks like SIGKILL (but not same as SIGKILL).
It seems difficult to simulate SIGKILL or machine failure. If the drop function is not called, there may be resource leaks (memory, file locks, etc.)
Another problem is that drop function won't be called automatically after process termination. See this code:
structFoo(String);implFoo{fnnew(name:&str) -> Foo{println!("foo::new in {}", name);Foo(name.to_owned())}}implDropforFoo{fndrop(&mutself){println!("foo::drop in {}!", self.0);}}#[madsim::main]asyncfnmain(){let handle = madsim::Handle::current().create_host("127.0.0.1:10086").unwrap();
handle.spawn(asyncmove{let _foo = Foo::new("madsim");loop{
madsim::time::sleep(std::time::Duration::from_secs(1)).await;}}).detach();
madsim::time::sleep(std::time::Duration::from_secs(1)).await;}
Foo::drop won't be called run this code with feature sim or std. This makes it difficult for applications that use madsim to exit gracefully. It seems would be better to provide an async fn as an entry function when the host is created, like spawn_blocking in tokio. When this entry function exits, the host is considered finished. And provide a joinhandle to wait for the host to finish.
The text was updated successfully, but these errors were encountered:
Processes may terminate in a number of ways, eg SIGTERM, SIGKILL, machine down. Currently, Madsim does not simulate processes termination very well.
Madsim provides
kill
function to terminate a host, but the semantic of the kill function is unclear.See this code:
Run this code with features
sim
enabled, we will find thatfoo::drop
is called. This means that the kill function behaves more likeSIGTERM
thanSIGKILL
, and this function cannot simulate some unexpected events (machine failure, out of memory, etc.). We have no way to know that when all async tasks on that host will be dropped, so it's still hard to determine a suitable time to restart host.If we replace
sleep
in spawned task withfutures::future::pending::<()>().await
. We will see that the drop function is not called, which looks like SIGKILL (but not same as SIGKILL).It seems difficult to simulate SIGKILL or machine failure. If the drop function is not called, there may be resource leaks (memory, file locks, etc.)
Another problem is that
drop
function won't be called automatically after process termination. See this code:Foo::drop
won't be called run this code with featuresim
orstd
. This makes it difficult for applications that use madsim to exit gracefully. It seems would be better to provide an async fn as an entry function when the host is created, likespawn_blocking
in tokio. When this entry function exits, the host is considered finished. And provide a joinhandle to wait for the host to finish.The text was updated successfully, but these errors were encountered: