CRITICAL: Global State Race Conditions #2
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?
What the Problem Is
Multiple global variables in
globals.pyare accessed and modified by different async functions without any locking mechanism. In async code, multiple coroutines can be running "at the same time" (awaiting different I/O operations), and they can read and write to shared variables simultaneously, causing data corruption.Where It Occurs
bot/globals.py#L18-19-conversation_history(defaultdict)bot/globals.py#L52-54-BIPOLAR_WEBHOOKS(dict)bot/globals.py#L58-62-BIPOLAR_ARGUMENT_IN_PROGRESS(dict)bot/globals.py#L108-111-VOICE_SESSION_ACTIVE(bool)bot/globals.py#L108-111-TEXT_MESSAGE_QUEUE(list)Why This Is a Problem
In async Python, when you
await, the function yields control to the event loop. Another coroutine can run and modify the same variable.What Can Go Wrong
Scenario 1: Voice Session State Corruption
VOICE_SESSION_ACTIVEset toTrueTrue, so both try to pause text channelsTEXT_MESSAGE_QUEUE = []to start freshTEXT_MESSAGE_QUEUE = [](first one lost)Scenario 2: Conversation History Loss
conversation_history[user_id]maxlen=5(automatically removes oldest when full)Proposed Fix
Create locks for each shared resource in globals.py:
Severity
CRITICAL - Data corruption, lost messages, inconsistent state, unpredictable behavior.
Files Affected
5 files including globals.py, voice_manager.py, autonomous.py, bipolar_mode.py