Diggsey / act-zero

Apache License 2.0
122 stars 7 forks source link

Waiting for drop to complete #6

Closed mkj closed 4 years ago

mkj commented 4 years ago

Firstly many thanks for act-zero - it's very nice to work with. I've converted a program to it from Riker.

I'm trying to run some cleanup in my actor's drop(), but I can't figure a way to wait for it to complete. .termination()returns immediately. I guess the channel is closed before the inner actor is dropped? I guess maybe the AddrInner could drop its Actor value before closing channels, but I'm not sure where. Any ideas would be appreciated.

// Copy of using_async_std example
use act_zero::runtimes::async_std::spawn_actor;
use act_zero::*;

struct HelloWorldActor;

impl Actor for HelloWorldActor {}

impl HelloWorldActor {
    async fn say_hello(&mut self) {
        println!("Hello, world!");
    }
}

impl Drop for HelloWorldActor {
    fn drop(&mut self) {
        println!("actor drop starts, waiting a sec");
        std::thread::sleep(std::time::Duration::from_secs(1));
        println!("actor drop done");
    }
}

#[async_std::main]
async fn main() -> Result<(), ActorError> {
    let addr = spawn_actor(HelloWorldActor);
    call!(addr.say_hello()).await?;
    let ended = addr.termination();
    std::mem::drop(addr);
    ended.await;
    println!("The End");
    Ok(())
% cargo run --example using_async_std --features=async-std
Hello, world!
actor drop starts, waiting a sec
The End
%