Skip to content
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

[unawaited_futures] does not complain for async function that return void #5089

Closed
stephane-archer opened this issue Sep 2, 2024 · 6 comments
Labels
P3 A lower priority bug or feature request

Comments

@stephane-archer
Copy link

if my function is :
Future<void> _foo() async {
the linter will complain if I don't await _foo()
but if my function is:
void _foo() async {
no complain.
I know I'm not "unawait a future" here but what I want is to await all async functions even if they return void over Future<void>

@eernstg
Copy link
Member

eernstg commented Sep 2, 2024

The support for having return type void with a function whose body is async exists specifically in order to indicate that the caller should not await the future. This can be used for a task that should be initiated, but where clients are never expected to wait for its completion (e.g., it might free some resources, add a line to a log file, etc). So the standard advice would be to stop using the return type void in this situation (or stop wanting to wait for it ;-).

@pq pq added the P3 A lower priority bug or feature request label Sep 9, 2024
@lrhn
Copy link
Member

lrhn commented Sep 11, 2024

is to await all async functions

Your code doesn't know, and mustn't know, if a function it calls is async. All it should depend on is whether the function returns a Future or not, as defined by its return type.
It's a non-breaking change, not even worthy of a minor semver version increment, to change a function implementation from async to not, or the other direction. That a function body uses async is an implementation detail that can change at any time.

So, as @eernstg says, if you don't want the future of _foo to be lost, make sure the return type is a Future type. That's how you communicate to the rest of the program that there is a future to await.

@stephane-archer
Copy link
Author

I see, my function returned void and not future void by mistake. I usually want my fonction to wait the function it calls before continuing execution. Usually this linter helps me do that (it's really easy to forget to await a future void). Because of the exception model for error handling returning future void is quite common in my code base. Do you think it's normal or should I do something different?

@eernstg
Copy link
Member

eernstg commented Sep 11, 2024

You could enable avoid_void_async in order to get a heads-up whenever there is a function whose body is async and whose return type is void.

This makes sense if you almost never want to write a "fire and forget" async function, and then you can mark the ones you might have anyway with // ignore: avoid_void_async and possibly a comment that explains why this function is an exception.

@stephane-archer
Copy link
Author

I didn't notice this linter, thanks for mentioning it, I will use it moving forward 🙏

@srawlins
Copy link
Member

Duplicate of #836

@srawlins srawlins marked this as a duplicate of #836 Sep 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
P3 A lower priority bug or feature request
Projects
None yet
Development

No branches or pull requests

5 participants