Skip to content

Latest commit

 

History

History
90 lines (60 loc) · 2.24 KB

File metadata and controls

90 lines (60 loc) · 2.24 KB

✅ Subscription Deletion Bug - FIXED

Problem

When stopping the event monitor (Ctrl+C or timeout), the subscription deletion was happening twice, causing a 500 error on the second attempt:

🧹 Cleaning up subscription: sub_fa77ad23-289
✅ Subscription deleted
🧹 Cleaning up subscription: sub_fa77ad23-289  ← DUPLICATE!
⚠️  Failed to delete subscription: 500          ← ERROR!

Root Cause

Double-call scenario:

  1. Signal handler (Ctrl+C) → Calls stop() → Deletes subscription (200 OK)
  2. Finally block (Python cleanup) → Calls stop() again → 500 error (already deleted)

The issue was that sys.exit(0) in the signal handler doesn't prevent Python's finally block from executing, leading to stop() being called twice.

Solution

Added an idempotency guard to prevent stop() from executing twice:

def stop(self):
    """Stop monitoring and cleanup"""
    # Guard against double-call
    if hasattr(self, '_stopped') and self._stopped:
        return  # Already stopped, do nothing
    self._stopped = True
    
    # ... rest of cleanup code

How It Works

  • First call: _stopped is not set → Proceeds with cleanup
  • Second call: _stopped is True → Returns immediately (no-op)

Results

Before Fix ❌

Server logs:

[23:21:57] 200 -    1.838099ms DELETE /api/v1/subscriptions/sub_fa77ad23-289
[23:21:57] 500 -      11.941µs DELETE /api/v1/subscriptions/sub_fa77ad23-289

After Fix ✅

Server logs:

[23:35:21] 200 -    1.821782ms DELETE /api/v1/subscriptions/sub_b8a4d9e4-161

Only ONE DELETE request! No 500 error!

Benefits

Clean shutdown - No confusing error messages
Idempotent - Safe to call stop() multiple times
Efficient - Reduces unnecessary API calls
Professional - Better user experience

Testing

Tested with multiple scenarios:

  • ✅ Ctrl+C shutdown
  • ✅ Timeout termination (SIGTERM)
  • ✅ Normal exit

All scenarios now show single deletion with 200 OK status.

Files Modified

  • event_monitor.py (Lines 394-397): Added _stopped guard

Documentation

Full technical details in: BUGFIX_DOUBLE_DELETION.md

Date

October 5, 2025

Status

FIXED AND VERIFIED