-
Notifications
You must be signed in to change notification settings - Fork 30
SONARPY-2893 Create rule S7487: Async functions should not contain synchronous subprocess calls #5004
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
Open
github-actions
wants to merge
5
commits into
master
Choose a base branch
from
rule/add-RSPEC-S7487
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
SONARPY-2893 Create rule S7487: Async functions should not contain synchronous subprocess calls #5004
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a311952
Create rule S7487: Async functions should not contain synchronous sub…
guillaume-dequenne 5b2d5a4
Fix after review
guillaume-dequenne 9778f95
Further fixes
guillaume-dequenne d1d8995
Merge branch 'master' into rule/add-RSPEC-S7487
guillaume-dequenne a7ea4d4
Fix after review
guillaume-dequenne File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
{ | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
{ | ||
"title": "Async functions should not contain synchronous subprocess calls", | ||
"type": "BUG", | ||
"status": "ready", | ||
"remediation": { | ||
"func": "Constant\/Issue", | ||
"constantCost": "5min" | ||
}, | ||
"tags": [ | ||
"async", "asyncio", "AnyIO", "Trio" | ||
], | ||
"defaultSeverity": "Major", | ||
"ruleSpecification": "RSPEC-7487", | ||
"sqKey": "S7487", | ||
"scope": "All", | ||
"defaultQualityProfiles": ["Sonar way"], | ||
"quickfix": "unknown", | ||
"code": { | ||
"impacts": { | ||
"RELIABILITY": "HIGH" | ||
}, | ||
"attribute": "EFFICIENT" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not sure of the attribute, but I can't find a better one There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My rationale was that the resulting problem would mostly be a performance one. |
||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
This rule raises an issue when synchronous subprocess calls are used within asynchronous functions. | ||
|
||
== Why is this an issue? | ||
|
||
Using synchronous subprocess calls like `subprocess.Popen` or similar functions in asynchronous code blocks the entire event loop. This undermines the primary advantage of asynchronous programming - the ability to perform concurrent operations without blocking execution. | ||
|
||
When an async function makes a synchronous call to create a subprocess: | ||
|
||
* The event loop is completely blocked until the subprocess operation completes | ||
* No other coroutines can run during this time, even if they're ready to execute | ||
* The responsiveness of the application is degraded | ||
* In server applications, this can cause timeouts or failures for other concurrent requests | ||
|
||
Instead, async libraries provide dedicated APIs for running subprocesses in a non-blocking way: | ||
|
||
* `asyncio.create_subprocess_exec()` and `asyncio.create_subprocess_shell()` for asyncio | ||
* `trio.run_process()` for Trio | ||
* `anyio.run_process()` for AnyIO | ||
|
||
Using these APIs allows other tasks to continue executing while waiting for the subprocess to complete. | ||
|
||
== How to fix it in Asyncio | ||
|
||
Replace synchronous subprocess calls with `asyncio.create_subprocess_exec()` or `asyncio.create_subprocess_shell()` depending on whether you need to run a specific command with arguments or a shell command string. | ||
|
||
=== Code examples | ||
|
||
==== Noncompliant code example | ||
|
||
[source,python,diff-id=1,diff-type=noncompliant] | ||
---- | ||
import subprocess | ||
|
||
async def process_data(): | ||
subprocess.run(["wget", "https://example.com/file.zip"]) # Noncompliant | ||
---- | ||
|
||
==== Compliant solution | ||
|
||
[source,python,diff-id=1,diff-type=compliant] | ||
---- | ||
import asyncio | ||
|
||
async def process_data(): | ||
proc = await asyncio.create_subprocess_exec("wget", "https://example.com/file.zip") | ||
result = await proc.wait() | ||
---- | ||
|
||
== How to fix it in Trio | ||
|
||
Replace synchronous subprocess calls with `trio.run_process()`, which handles both command arrays and shell commands. | ||
|
||
=== Code examples | ||
|
||
==== Noncompliant code example | ||
|
||
[source,python,diff-id=2,diff-type=noncompliant] | ||
---- | ||
import trio | ||
import subprocess | ||
|
||
async def download_files(): | ||
result = subprocess.run(["wget", "https://example.com/file.zip"]) # Noncompliant | ||
---- | ||
|
||
==== Compliant solution | ||
|
||
[source,python,diff-id=2,diff-type=compliant] | ||
---- | ||
import trio | ||
|
||
async def download_files(): | ||
result = await trio.run_process(["wget", "https://example.com/file.zip"]) | ||
---- | ||
|
||
== How to fix it in AnyIO | ||
|
||
Replace synchronous subprocess calls with `anyio.run_process()`, which works similar to Trio's API and supports both command arrays and shell commands. | ||
|
||
=== Code examples | ||
|
||
==== Noncompliant code example | ||
|
||
[source,python,diff-id=3,diff-type=noncompliant] | ||
---- | ||
import subprocess | ||
|
||
async def process_image(): | ||
result = subprocess.run(["wget", "https://example.com/file.zip"]) # Noncompliant | ||
---- | ||
|
||
==== Compliant solution | ||
|
||
[source,python,diff-id=3,diff-type=compliant] | ||
---- | ||
import anyio | ||
|
||
async def process_image(): | ||
result = await anyio.run_process(["wget", "https://example.com/file.zip"]) | ||
---- | ||
|
||
== Resources | ||
|
||
=== Documentation | ||
* Python asyncio - https://docs.python.org/3/library/asyncio-subprocess.html[Subprocess] | ||
* Trio - https://trio.readthedocs.io/en/stable/reference-io.html#trio.run_process[run_process() documentation] | ||
* AnyIO - https://anyio.readthedocs.io/en/stable/subprocesses.html[Subprocesses] | ||
|
||
=== Articles & blog posts | ||
* Python - https://realpython.com/python-concurrency/[Concurrency and Parallelism in Python] | ||
|
||
ifdef::env-github,rspecator-view[] | ||
|
||
''' | ||
== Implementation Specification | ||
(visible only on this page) | ||
|
||
=== Message | ||
Use an async subprocess call in this async function instead of a synchronous one. | ||
|
||
=== Highlighting | ||
* Primary locations: the `subprocess` callee within an async function | ||
* Secondary locations: the enclosing async function `aync` keyword (message: "this is an asynchronous function") | ||
|
||
endif::env-github,rspecator-view[] | ||
|
||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The case is different than in other rules