Race Condition in RCON Connection #5
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
with MCRcon(MINECRAFT_RCON_HOST, MINECRAFT_RCON_PASSWORD) as mcr:
response = mcr.command(f"whitelist add {minecraft_username}")
Issue: No timeout, no retry logic, blocking call in async context.
Fix:
import asyncio
from functools import partial
async def rcon_command(host: str, password: str, command: str, timeout: float = 5.0):
"""Execute RCON command with timeout"""
try:
loop = asyncio.get_event_loop()
func = partial(execute_rcon, host, password, command)
return await asyncio.wait_for(loop.run_in_executor(None, func), timeout=timeout)
except asyncio.TimeoutError:
raise Exception("RCON timeout - server may be offline")
except Exception as e:
raise Exception(f"RCON error: {str(e)}")
def execute_rcon(host: str, password: str, command: str):
with MCRcon(host, password, timeout=3) as mcr:
return mcr.command(command)