# Potential Race Condition - ID: python-race-condition - Severity: MEDIUM - CWE: Race Condition (CWE-362) - Languages: Python ## Description Detects potential race conditions in concurrent Python code. Common race condition patterns: 1. Global variables accessed from threads without locking 2. TOCTOU (Time-of-check Time-of-use) file operations 3. Shared data structures modified in threads without synchronization 4. Check-then-act patterns without atomicity Python's GIL doesn't prevent all race conditions, especially with I/O operations and multi-process code. ## Remediation Use locks for shared data and atomic operations for file access. ```python import threading import os # Use locks for shared data lock = threading.Lock() with lock: counter += 1 # Atomic file creation (no TOCTOU) try: fd = os.open(path, os.O_CREAT | os.O_EXCL | os.O_WRONLY) os.close(fd) except FileExistsError: pass # Or use makedirs with exist_ok os.makedirs(path, exist_ok=True) ``` Learn more: https://shoulder.dev/learn/python/cwe-362/race-condition ## Documentation [object Object] ## Related Rules - **Concurrent Slice Access** [HIGH]: - **Direct Map Access on Thread-Safe Struct** [HIGH]: - **Potential Race Condition** [MEDIUM]: - **WaitGroup Misuse** [HIGH]: - **Race Condition in Concurrent Operations** [HIGH]: