-
-
Notifications
You must be signed in to change notification settings - Fork 76
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
Panic causes "thread panicked at 'internal error: entered unreachable code'" #43
Comments
Hi. Thank you for the detailed report! I totally agree; we should take one of the options that you proposed.
I think we can take this option by using the |
Unless I am missing something, using use futures::future::FutureExt;
use tokio;
use std::sync::Arc;
use std::any::Any;
use futures::TryStreamExt;
use tokio::sync::Mutex;
use futures::TryFutureExt;
#[tokio::main]
async fn main() {
let cache: Arc<moka::future::Cache<i32, i32>> = Arc::new(moka::future::Cache::new(16));
let semaphore = Arc::new(tokio::sync::Semaphore::new(0));
{
let cache_ref = cache.clone();
let semaphore_ref = semaphore.clone();
tokio::task::spawn(async move {
cache_ref.get_or_try_insert_with::<_, tokio::sync::Mutex<Box<(dyn Any + std::marker::Send + 'static)>>>(1, async move {
semaphore_ref.add_permits(1);
tokio::time::sleep(tokio::time::Duration::from_millis(1000));
panic!("Panic during get_or_try_insert_with");
10
}.catch_unwind().map_err(|pn| Mutex::new(pn))).await;
});
}
semaphore.acquire().await;
cache.get_or_try_insert_with::<_, ()>(1, async move {
println!("Async block in second get_or_try_insert_with called");
Ok(5)
}).await;
} And here's the compiler error:
However, it should be possible to detect the panic on the other thread without directly catching it by handling the case currently marked as unreachable in |
Thanks for checking. I opened a pull request #46, which uses moka/src/future/value_initializer.rs Lines 110 to 122 in af44363
I verified that your example compiles and now works as expected: Lines 1345 to 1370 in af44363
It should be safe to use
I would avoid to remove the waiter from the other threads because it will get messy if two or more threads are trying to do so at the same time (removing the old waiter and inserting its own one). As for stack overflows, I used |
* updates - fix titan preset token - update web-server image - add gcloud config * reorganize rust packages * git subrepo clone https://github.com/thuo-io/openapi-rust.git libs/openapi subrepo: subdir: "libs/openapi" merged: "ae8de950" upstream: origin: "https://github.com/thuo-io/openapi-rust.git" branch: "main" commit: "ae8de950" git-subrepo: version: "0.4.6" origin: "https://github.com/Homebrew/brew" commit: "146b987fa" * remove js packages * move openapi dir * remove landing page ingress * try using tosserror * reorganize typesense error * update tosserror * git subrepo clone (merge) https://github.com/thuo-io/js-packages.git frontend subrepo: subdir: "frontend" merged: "61f63a88" upstream: origin: "https://github.com/thuo-io/js-packages.git" branch: "main" commit: "61f63a88" git-subrepo: version: "0.4.6" origin: "https://github.com/Homebrew/brew" commit: "146b987fa" * remove js packages * git subrepo clone https://github.com/thuo-io/bundle.git services/bundle subrepo: subdir: "services/bundle" merged: "867c88e7" upstream: origin: "https://github.com/thuo-io/bundle.git" branch: "main" commit: "867c88e7" git-subrepo: version: "0.4.6" origin: "https://github.com/Homebrew/brew" commit: "146b987fa" * add bundle * remove subscribe handler and increase resources for sidecar * git subrepo pull (merge) --branch=minikube services/bundle subrepo: subdir: "services/bundle" merged: "ac09ed72" upstream: origin: "https://github.com/thuo-io/bundle.git" branch: "minikube" commit: "575cb812" git-subrepo: version: "0.4.6" origin: "https://github.com/Homebrew/brew" commit: "146b987fa" * minor change * move bundle to inside web-server * add cors for products api * remove bundle * git subrepo clone --branch=minikube https://github.com/thuo-io/bundle.git services/web-server/bundle subrepo: subdir: "services/web-server/bundle" merged: "fc8a39a1" upstream: origin: "https://github.com/thuo-io/bundle.git" branch: "minikube" commit: "fc8a39a1" git-subrepo: version: "0.4.6" origin: "https://github.com/Homebrew/brew" commit: "146b987fa" * lfs * remove js ci
Hi, thank you for this very useful library! However, I've found a way to enter a supposedly unreachable state.
Here's the relevant part of the backtrace:
This issue seems to occur when two threads attempt to fetch the same key and the thread on which the value initialization was started panics. I've built a simple program to demonstrate this issue:
I think the unreachable code is being hit because the write lock obtained at line 71 of value_initializer.rs gets dropped without any value being written (here or here).
I see two possible options for handling this issue:
unreachable()
can be replaced with a panic with a more helpful message.The text was updated successfully, but these errors were encountered: