Async/.await object outlives after drop

When I wrote async code like this:

let mut urls = handled_urls.lock().unwrap();
if !urls.contains(&child_url) {
    println!("hadl_urls.size() is {}", hadl_urls.len());
    urls.insert(child_url.clone());
    drop(urls);
    tokio::spawn(handle_url(child_url, handled_urls.clone())).await;
}

Compiler thinks that urls outlives the tokio::spawn(handle_url(child_url, handled_urls.clone())).await; ...
But as we can see, there is happens drop of this object before call of tokio::spawn ...

Is it an issue in compiler or maybe I do not understand something ?

The tokio::spawn function requires the provided future to be 'static, which means it can't contain references to outside of itself.

One way this could fail: if the function calling tokio::spawn is in one half of a select! and it finishes, the task waiting for the spawn is aborted without aborting the spawned task, invalidating the url.

But how urls object relate to it ?
I understand if an issue was 'caused by child_url, but I drop urls a line above:

drop(urls);
tokio::spawn(handle_url(child_url, handled_urls.clone())).await;

Well it was probably caused by the child_url?

No, it is separate Url object created from the string ...

Yes, but if the handle_url takes it as a &str, then it's just a borrow from the separate url object.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.