Comments (5)
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.
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.
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.
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.
Thanks for the explanation!
from anyhow.
Related Issues (20)
- Should `anyhow::Error::chain` return `dyn Error + Send + Sync` HOT 2
- Customize backtrace logic?
- `anyhow!(e)` doesn't preserve `source` for `&Error` HOT 1
- A way to disable anyhow stacktraces (without disabling stacktraces from other crates) HOT 2
- Default Ok-type to `()` in `anyhow::Result` typedef HOT 2
- Updating from version 1.0.76 breaks backtraces HOT 2
- Make backtrace support optional HOT 9
- Possible performance regression on Windows HOT 5
- as_ref() type must be known at this point
- Depending on `CARGO_ENCODED_RUSTFLAGS` may produce stale builds HOT 1
- rust-analyzer nightly throws needless_return warning on bail! HOT 2
- Implement Context for Error
- Question regarding stacktrace
- anyhow::Error to Box<dyn Error> isn't compatible with other libraries HOT 2
- Are you open to de-duplicating the build.rs build probe code? HOT 7
- `anyhow::ensure!` doesn't work with custom error type HOT 1
- Short backtrace
- Feature suggestion: ensure_some!() for unwrapping options HOT 3
- Cannot compile when using `ensure!` HOT 1
- Unsound usages of unsafe implementation from `u8` to `str` HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from anyhow.