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

Detect asyncio-dangling-task (RUF006) when discarding return value #8863

Closed
tjkuson opened this issue Nov 28, 2023 · 7 comments · Fixed by #9060
Closed

Detect asyncio-dangling-task (RUF006) when discarding return value #8863

tjkuson opened this issue Nov 28, 2023 · 7 comments · Fixed by #9060
Assignees
Labels
bug Something isn't working

Comments

@tjkuson
Copy link
Contributor

tjkuson commented Nov 28, 2023

import asyncio

for i in range(10):
    asyncio.create_task(foo)

triggers asyncio-dangling-task but

import asyncio

for i in range(10):
    _ = asyncio.create_task(foo)

does not.

I would expect Ruff to trigger asyncio-dangling-task when the return value of asyncio.create_task is discarded, following the rationale of the rule.

@tjkuson tjkuson changed the title Detect asyncio-dangling-task when discarding return value Detect asyncio-dangling-task(RUF006) when discarding return value Nov 28, 2023
@tjkuson tjkuson changed the title Detect asyncio-dangling-task(RUF006) when discarding return value Detect asyncio-dangling-task (RUF006) when discarding return value Nov 28, 2023
@zanieb zanieb added the good first issue Good for newcomers label Nov 28, 2023
@zanieb
Copy link
Member

zanieb commented Nov 28, 2023

This seems reasonable to me!

@zanieb zanieb added the bug Something isn't working label Nov 28, 2023
@allaboutevemirolive
Copy link

allaboutevemirolive commented Dec 5, 2023

Hye, @tjkuson

Minimizing

I'm new to Ruff's contributions. I try to duplicate your issue by compiling Ruff using cargo build --release and running the command on a Python file.

./ruff check /home/nemesis/Documents/Github/test/python/game.py

game.py:

import asyncio


async def some_coro(param):
    print(f"Executing coroutine with param: {param}")
    await asyncio.sleep(1)
    print(f"Coroutine with param {param} completed")


async def main():
    for i in range(10):
        asyncio.create_task(some_coro(param=i))


if __name__ == "__main__":
    asyncio.run(main())

I expected it would produce an error: asyncio-dangling-task, like you mentioned above, but it didn't.

The result is also the same for this snippet.

_ = asyncio.create_task(some_coro(param=i))

Python Version:

$ python3 --version
Python 3.12.0

Local Machine:

Release 11 (bullseye) 64-bit
Kernel Linux 5.10.0-26-amd64 x86_64
Memory: 7.5 GiB
Processor: Intel® Core™ i5-8250U CPU @ 1.60GHz × 8 
Graphics: Mesa Intel® UHD Graphics 620 (KBL GT2)

Edit:

The rule is disabled by default. Use:

./ruff check --select RUF006 [PATH]

@MichaReiser
Copy link
Member

I'm not sure if this behavior is intentional, similar to that ruff allows _ variables to be unused. @charliermarsh was it an intentional decision to not raise the lint for variables starting with _?

@charliermarsh
Copy link
Member

@MichaReiser - Not intentional -- this rule doesn't look at assignments, it only looks at standalone expressions that include asyncio.create_task(...), and then assumes that those are dangling by virtue of being a standalone expression statement.

@charliermarsh charliermarsh removed the good first issue Good for newcomers label Dec 5, 2023
@tjkuson
Copy link
Contributor Author

tjkuson commented Dec 5, 2023

@allaboutevemirolive I think you need to select the rule (it isn't enabled by default) using --select RUF006.

@asafamr-mm
Copy link
Contributor

Hi,I gave it a try.
I'm new to rust (and the project codebase) and would love feedback. What do you think?

@charliermarsh
Copy link
Member

Thanks @asafamr-mm!

charliermarsh pushed a commit that referenced this issue Dec 9, 2023
#9060)

## Summary

Fixes #8863 : Detect asyncio-dangling-task (RUF006) when discarding
return value

## Test Plan

added new two testcases, changed result of an old one that was made more
specific
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants