slawlor / ractor

Rust actor framework
MIT License
1.3k stars 66 forks source link

SpawnErr when spawning named actor will permanently pollute that name #240

Closed Escapingbug closed 1 month ago

Escapingbug commented 1 month ago

Describe the bug

When in actor pre_start, if any error is thrown, causing a SpawnErr and stopping that actor being spawned successfully, the name of that actor cannot be used after that. That name is permanently being occupied by the actor that is not spawned.

To Reproduce

Testing code:

use ractor::{Actor, ActorProcessingErr, ActorRef};

struct Test;

#[ractor::async_trait]
impl Actor for Test {
    type Msg = ();
    type State = ();
    type Arguments = ();

    async fn pre_start(
        &self,
        _: ActorRef<Self::Msg>,
        _: ()
    ) -> Result<(), ActorProcessingErr> {
        Err(Box::new(std::io::Error::last_os_error()))
    }
}

#[tokio::main]
async fn main() {
    println!("Hello, world!");
    let a = Actor::spawn(Some("test".to_owned()), Test, ()).await
        .inspect_err(|e| println!("first error {e}"));
    drop(a);

    let _ = Actor::spawn(Some("test".to_owned()), Test, ()).await
        .inspect_err(|e| println!("second error {e}"));
}

Outputs:

Hello, world!
first error Actor panicked during startup '操作系统找不到已输入的环境选项。 (os error 203)'
second error Actor 'test' is already registered in the actor registry

Expected behavior

When spawn fails, the name of the actor should be usable when spawn the actor again. So the second error of the testing code should still be the same as the first one instead of saying that the name is being used.

Additional context Add any other context about the problem here (e.g. Rust version, other crates imported, etc)