Skip to content

[PERFORMANCE]: Use async I/O instead of blocking calls in async functions (S7493, S7487) #2164

@crivetimihai

Description

@crivetimihai

Problem

15 locations use synchronous I/O operations inside async functions, blocking the event loop and degrading performance.

SonarQube Rules: python:S7493 (file I/O), python:S7487 (subprocess)
Severity: MAJOR

Sync File I/O (12 issues)

# BAD
async def load_data():
    with open("file.json") as f:
        return json.load(f)

# GOOD
async def load_data():
    async with aiofiles.open("file.json") as f:
        content = await f.read()
        return json.loads(content)

Affected Files

File Lines
mcpgateway/cli_export_import.py 145, 187
mcpgateway/services/grpc_service.py 435, 437
mcpgateway/translate_grpc.py 100, 102
mcpgateway/admin.py 13263, 15861
mcpgateway/services/catalog_service.py 76
plugins/external/clamav_server/clamav_plugin.py 219
plugins/virus_total_checker/virus_total_checker.py 561, 570

Sync Subprocess (3 issues)

# BAD
async def run_command():
    result = subprocess.run(["cmd"], capture_output=True)

# GOOD
async def run_command():
    proc = await asyncio.create_subprocess_exec("cmd", stdout=asyncio.subprocess.PIPE)
    stdout, _ = await proc.communicate()

Affected Files

File Lines
mcpgateway/tools/builder/dagger_deploy.py 416, 418, 420

Fix Strategy

  1. Add aiofiles dependency if not present
  2. Replace open() with aiofiles.open()
  3. Replace subprocess.run() with asyncio.create_subprocess_exec()
  4. For small files, consider using asyncio.to_thread() as alternative

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingperformancePerformance related itemspythonPython / backend development (FastAPI)sonarSonarQube code quality findings

Type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions