-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Inconsistent Error Handling Behavior with ?
operator in Marco #[tokio::main]
#6930
Comments
I guess the issue is that we're not properly informing the closure about the return type... |
Thank you for the clarification! It makes sense regarding the closure and return type. I’ll take a closer look and experiment with it a bit more to deepen my understanding of tokio. Appreciate the insights—this is a great learning opportunity for me! |
If we can fix this in a non-intrusive way, then I'm happy to see a PR for that. But changes to this part of the macro have proven tricky in the past. |
Thanks for pointing that out! I fully understand that changes in this area can be tricky. If I come up with any ideas or plans for a potential PR, I’ll be sure to discuss them with you and the other maintainers before making any code changes or submitting anything. I really appreciate your guidance and openness to contributions! |
You don't have to ask first before creating a PR. Just be prepared that it may (or may not!) be difficult to fix this. We have previously closed several PRs in this part of the codebase because they were incorrect and we couldn't figure out how to fix them. |
Haha, thanks for the heads-up! I’ll keep that in mind and do my best. |
Hi, apologies for the delayed response; I was a bit occupied recently. I did some digging into the root cause of this Code 1 expands into the following:
Code 2 expands as follows:
The inconsistency in using the
In the expanded Code 1, since the async block’s body must match the In Code 2, the error is returned directly from @Darksonn Hi, could you help check if this explanation aligns with the intended behavior of the |
Unfortunately, You are out of luck. this can't be fixed with the current implementation due to the use of An attempt (#6882) was made in the past to address this using Drop order for async is tricky:
Perhaps we can use |
hi @nurmohammed840, thanks for your detailed response! I'm still a bit new to this, so I wanted to ask: could you help let me know which part of your code changes specifically addresses the issue I mentioned? I'd like to understand the fix better. Thanks again for your help! |
When a function annotated with Inputuse std::error::Error;
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
Err(Box::new(std::io::Error::new(
std::io::ErrorKind::Other,
"Request failed",
)))
} Outputfn main() -> Result<(), Box<dyn Error>> {
async fn main() -> Result<(), Box<dyn Error>> {
Err(Box::new(std::io::Error::new(
std::io::ErrorKind::Other,
"Request failed",
)))
}
...
} |
yeah sure, I get it that you're fixing this issue related to not using a ?. I was just trying to ask which parts/lines in your code changes fixed this issue, as it looks like you're fixing a lot of things. |
thanks man! I'll get this patch and try locally for a testing. |
Version
Tokio version:
Rust version:
Reqwest version:
Platform
Description
When using the
#[tokio::main]
macro to define an asynchronousmain()
function, I've encountered inconsistent behavior regarding error handling when returningstd::io::Error
as part of aResult
that hasBox<dyn Error>
as its error type.Specifically, in the
main()
function, it seems necessary to use the?
operator before returning it, otherwise it fails to compile. However, in other async functions,Box::new(std::io::Error)
can be returned directly without the?
operator conversion.For example, in the
main()
function, the following code is required:While in other asynchronous functions, the following code works without the need for
?
:Minimal, Complete, and Verifiable Example (MCVE)
To reproduce the issue, you can use the following minimal example:
Code1:
Code2:
Notice that in Code1, within the
main()
function, the error must use the?
operator, while in Code2, the test function, the error can be returned directly without operator?
.Expected Behavior
I expect both functions to require the same treatment for returning errors. Consistent error handling across all async functions, including
main()
, would make the code more intuitive and easier to maintain.Possible Solutions
-Documentation: Clarify in the documentation whether this is expected behavior and why.
-Bug Fix: If it's a bug, fix the behavior so that all async functions, including
main
, handle return errors consistently.This issue was also discussed in the community: https://users.rust-lang.org/t/why-must-i-convert-std-error-to-box-dyn-error-in-one-function-but-not-in-another/120141/9
Thank you for looking into this!
The text was updated successfully, but these errors were encountered: