Giter VIP home page Giter VIP logo

Comments (5)

dtolnay avatar dtolnay commented on June 14, 2024

It makes anyhow::Error usable in async code. Without this, it would not be usable in async code. For example:

// Not Send or Sync
struct Error(Box<dyn std::error::Error>);

async fn f() -> Result<(), Error> {
    Ok(())
}

#[tokio::main]
async fn main() {
    tokio::spawn(f()).await;
}
error[E0277]: `(dyn std::error::Error + 'static)` cannot be sent between threads safely
   --> src/main.rs:10:5
    |
10  |     tokio::spawn(f()).await;
    |     ^^^^^^^^^^^^ `(dyn std::error::Error + 'static)` cannot be sent between threads safely

from anyhow.

dtolnay avatar dtolnay commented on June 14, 2024

Regarding PoisonError, that is not intended to be stored in an error variant. It signals that some other thread panicked while holding the same Mutex or RwLock and that the current thread should either recover (via into_inner) or panic also (via unwrap). Either way the PoisonError should never end up passed around in return types.

from anyhow.

d-e-s-o avatar d-e-s-o commented on June 14, 2024

Ah, the explanation about async makes sense to me. Thanks!

I didn't understand the argument against PoisonError, though. It's also not PoisonError that is not Send, but the inner guard. So even if the error variant does not contain the PoisonError directly but the inner guard, the problem would still exist.

Are you suggesting that the pattern of allowing for recovery from such cases is bad? This error is from a library that does not necessarily control what is done with a mutex held, so it seems legitimate to allow clients of the code to recover by passing out the guard.

I understand we are off topic now, but can you please elaborate some more?

from anyhow.

dtolnay avatar dtolnay commented on June 14, 2024

Regarding poisons:
Panics are for signifying a bug in the program (only). They are the mechanism for safely bringing down a program that has identified itself as buggy. Consequently a lock is only poisoned if the program has discovered it is buggy and must safely shut down. PoisonError is how a thread finds out that another thread has identified that the program must shut down, thus the correct response is usually to panic the current thread as well because the program is buggy and the runtime state is unknowable. The only exception would be inside specialized abstractions whose purpose is bug isolation, such as a server architecture that isolates all state to individual requests.

Regarding guards:
It would be extremely unusual for an error enum to need to contain a mutex guard. An error is only for identifying a runtime failure in a correct program, and we wouldn't need to hold the lock beyond what it takes to figure out what failed. You haven't described that much about the use case but if there is more complicated threading around of mutex guards going on, it would be better not to use errors or Result but instead a dedicated enum to represent that state machine.

from anyhow.

d-e-s-o avatar d-e-s-o commented on June 14, 2024

Thanks for the explanation!

from anyhow.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.