Files
InfiniteAPI/src
Claude 38511ed5bc fix(pre-key-manager): add destroyed flag with atomic protection (M2)
PROBLEM:
PreKeyManager lacked a destroyed flag, allowing operations to execute after
destroy() was called. This created race conditions where:
1. Operations could add tasks to queues after they were cleared/paused
2. New queues could be created after destroy() cleaned them up
3. Tasks could execute on destroyed resources
4. Multiple destroy() calls could cleanup resources multiple times

Race scenario 1 (Operations after destroy):
  T1: processOperations() → getQueue('pre-key')
  T2: destroy() → queue.clear(), queue.pause()
  T1: queue.add(task) → Task added to paused queue (never executes)

Race scenario 2 (Queue recreation):
  T1: destroy() → queues.clear()
  T2: processOperations() → getQueue() → Creates NEW queue!
  T2: queue.add(task) → Task executes (queue not destroyed)

SOLUTION:
Added destroyed flag with atomic check-and-set pattern, similar to V7/V8/M1:

1. Added private destroyed flag with comprehensive thread-safety documentation
2. Created checkDestroyed() method that throws error if destroyed
3. All public methods (processOperations, validateDeletions) check flag first
4. destroy() uses atomic check-and-set (checks flag, sets immediately)

Defense in depth:
- checkDestroyed() prevents new operations from starting
- Flag set BEFORE cleanup begins (closes race window)
- Reentrancy guard prevents multiple destroy() calls
- Clear error messages for debugging

VALIDATION:
✓ Protocolo de Análise complete (5 steps)
✓ TypeScript compilation verified (no new errors)
✓ Consistent with V7/V8/M1 atomic patterns
✓ All public entry points protected

FILES MODIFIED:
- src/Utils/pre-key-manager.ts:11-37 - Added destroyed flag and checkDestroyed()
- src/Utils/pre-key-manager.ts:60-61 - Added check in processOperations()
- src/Utils/pre-key-manager.ts:134-135 - Added check in validateDeletions()
- src/Utils/pre-key-manager.ts:160-171 - Atomic check-and-set in destroy()

https://claude.ai/code/session_01NTVq3RHgGpgKL289JGvw55
2026-02-05 01:48:33 +00:00
..
2025-10-09 13:14:44 +03:00