Skip to content

fix(async): replace time.sleep with asyncio.sleep in ensure_opg_approval#271

Open
verseon0980 wants to merge 1 commit intoOpenGradient:mainfrom
verseon0980:fix/async-event-loop-block-ensure-opg-approval
Open

fix(async): replace time.sleep with asyncio.sleep in ensure_opg_approval#271
verseon0980 wants to merge 1 commit intoOpenGradient:mainfrom
verseon0980:fix/async-event-loop-block-ensure-opg-approval

Conversation

@verseon0980
Copy link
Copy Markdown

Problem

ensure_opg_approval() and _send_approve_tx() are synchronous functions
that contain a polling loop calling time.sleep(1) up to 120 times.

Every official example in this repo calls ensure_opg_approval() directly
inside an async def main() function without await:

async def main():
    llm = og.LLM(private_key=...)
    llm.ensure_opg_approval(min_allowance=0.1)  # no await, blocks loop
    result = await llm.chat(...)

Python's asyncio event loop is single-threaded. Calling time.sleep()
inside an async context does not yield to the event loop. It hands control
to the OS sleep directly. This freezes the entire event loop for up to
120 seconds. During that window:

  • No other coroutines can execute
  • No background tasks can progress
  • No timeouts or cancellations can fire
  • No network I/O can complete

Every developer following the official examples ships broken async code
without knowing it.

Affected file:

  • src/opengradient/client/opg_token.py

Fix

Added two new async functions alongside the existing sync ones:

_send_approve_tx_async() - identical to _send_approve_tx() but uses
await asyncio.sleep(ALLOWANCE_POLL_INTERVAL) instead of
time.sleep(ALLOWANCE_POLL_INTERVAL), so the event loop is free between
each poll.

ensure_opg_approval_async() - identical logic to ensure_opg_approval()
but calls await _send_approve_tx_async() internally.

The original synchronous functions are preserved completely unchanged for
backward compatibility. No existing code breaks.

Changes

  • Added import asyncio at the top
  • Added _send_approve_tx_async()
  • Added ensure_opg_approval_async()

…_approval

Signed-off-by: verseon0980 <klokrc74@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant