Skip to content

time.sleep() inside async def blocks event loop in _push_messages #7303

Description

@Joaquinriosheredia

Hi,

While analyzing the codebase with python-vibe-guard, I found a potential
issue in backend/services/messaging/message_service.py around line 241.

The function _push_messages is an async def that uses time.sleep(0.5)
as a throttle every 10 messages:

if (i + 1) % 10 == 0:
    time.sleep(0.5)

time.sleep() blocks the entire event loop for 500ms — not just the current
coroutine. If this worker also handles other concurrent tasks (HTTP requests,
other async operations), every batch of 10 notifications introduces a 500ms
pause across all concurrent traffic on that worker.

Suggested fix (one-line change):

if (i + 1) % 10 == 0:
    await asyncio.sleep(0.5)

This preserves the throttling behavior while allowing other coroutines to
run during the wait.

Tool used: python-vibe-guard
AST-based runtime anti-pattern scanner validated on 902 real repositories.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions