Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Beta: Actor not automatically dropped when last strong Address is dropped #25

Closed
seijikun opened this issue Nov 15, 2020 · 2 comments
Closed
Assignees
Labels
bug Something isn't working

Comments

@seijikun
Copy link

seijikun commented Nov 15, 2020

In the current beta 0.5.0-beta.5, an actor is not automatically dropped when its last Address is dropped.
This behavior can be caused by the following code:

use xtra::prelude::*;
use xtra::spawn::AsyncStd;

struct Printer {
    times: usize,
}

impl Printer {
    fn new() -> Self {
        Printer { times: 0 }
    }
}

#[async_trait::async_trait]
impl Actor for Printer {
    async fn stopped(&mut self) {
        println!("Printer is stopped");
    }
}

struct Print(String);
impl Message for Print {
    type Result = ();
}

#[async_trait::async_trait]
impl Handler<Print> for Printer {
    async fn handle(&mut self, print: Print, _ctx: &mut Context<Self>) {
        self.times += 1;
        println!("Printing {}. Printed {} times so far.", print.0, self.times);
    }
}

#[async_std::main]
async fn main() {
    {
        let addr = Printer::new().create(None).spawn(&mut AsyncStd);
        addr.send(Print("hello".to_string()))
            .await
            .expect("Printer should not be dropped");
    }
    println!("Printer should be dropped here");
}

With this code, Printer is actually never dropped, even until the application stops.

This is probably because of refcount.rs line 74, where the strong refcount is checked against ==2.
It's only 1 in the example, though.

@Restioson
Copy link
Owner

Restioson commented Nov 16, 2020

Good catch! Looks like I missed this when I changed context to having a weak refcount rather than a strong one. I'll try release another beta today.

@Restioson Restioson added the bug Something isn't working label Nov 16, 2020
@Restioson Restioson self-assigned this Nov 16, 2020
@Restioson
Copy link
Owner

Restioson commented Jan 4, 2021

This has been fixed for a while, I just forgot to close the issue 😅 Fixed in 423ba56, and a test has been added to prevent regressions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants