Skip to content

Use async subprocess for ledger anchoring (event loop blocking) #15

@glandua

Description

@glandua

Summary

ledger_anchor.py:broadcast_anchor() is declared async def but calls subprocess.run() (synchronous) and time.sleep(5) in a polling loop. This blocks the entire event loop for up to 30 seconds during anchoring.

Current Code

async def broadcast_anchor(claim_rid: str, content_hash: str) -> dict:
    # ...
    tx_result = subprocess.run([regen_bin, ...], ...)  # blocks event loop
    # ...
    for attempt in range(6):
        time.sleep(5)  # blocks event loop for 5s per attempt
        query_result = subprocess.run(...)  # blocks again

Proposed Fix

Option A (preferred): Use asyncio.create_subprocess_exec() + asyncio.sleep():

proc = await asyncio.create_subprocess_exec(
    regen_bin, "tx", "data", "anchor", iri, ...,
    stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE
)
stdout, stderr = await proc.communicate()

Option B (simpler): Run blocking code in thread executor:

loop = asyncio.get_event_loop()
result = await loop.run_in_executor(None, _blocking_broadcast, ...)

Impact

Other API endpoints remain responsive during anchoring operations. No API contract change.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions