|
| 1 | +# Seen Submission Race Condition |
| 2 | + |
| 3 | +**Priority**: 1 **Type**: bug **Status**: open **Created**: 2025-07-04 |
| 4 | +**Updated**: 2025-07-04 |
| 5 | + |
| 6 | +## Description |
| 7 | + |
| 8 | +Race condition exists between filtering submissions and marking them as seen, |
| 9 | +potentially allowing duplicate notifications if the monitoring loop runs |
| 10 | +multiple times rapidly or if API responses change between calls. |
| 11 | + |
| 12 | +## Reproduction Steps |
| 13 | + |
| 14 | +1. Set up monitoring targets with very short poll intervals (difficult to |
| 15 | + reproduce consistently) |
| 16 | +2. Force multiple rapid checks using `.trigger` command |
| 17 | +3. Monitor for duplicate notifications of the same submission |
| 18 | +4. Check database for timing issues in `seen_submissions` table |
| 19 | + |
| 20 | +## Expected vs Actual Behavior |
| 21 | + |
| 22 | +- **Expected**: Each submission should only be notified once, regardless of |
| 23 | + timing |
| 24 | +- **Actual**: Rapid polling or API inconsistencies could cause duplicate |
| 25 | + notifications |
| 26 | + |
| 27 | +## Technical Details |
| 28 | + |
| 29 | +### Code Location |
| 30 | + |
| 31 | +- **File**: `src/cogs/runner.py` |
| 32 | +- **Functions**: `filter_new_submissions()` (line 386-398), |
| 33 | + `post_submissions()`, `mark_submissions_seen()` |
| 34 | + |
| 35 | +### Race Condition Flow |
| 36 | + |
| 37 | +```python |
| 38 | +# Time T1: First check starts |
| 39 | +submissions = await fetch_submissions_for_location(...) # API call |
| 40 | +new_submissions = self.db.filter_new_submissions(channel_id, submissions) # Query DB |
| 41 | + |
| 42 | +# Time T2: Second check starts (before first completes) |
| 43 | +submissions_2 = await fetch_submissions_for_location(...) # Same API response |
| 44 | +new_submissions_2 = self.db.filter_new_submissions(channel_id, submissions_2) # Same query result |
| 45 | + |
| 46 | +# Time T3: Both post notifications |
| 47 | +await self.post_submissions(new_submissions) # Posts duplicates |
| 48 | +await self.post_submissions(new_submissions_2) # Posts duplicates |
| 49 | + |
| 50 | +# Time T4: Both mark as seen |
| 51 | +self.db.mark_submissions_seen(channel_id, submission_ids) # Second call gets IntegrityError |
| 52 | +``` |
| 53 | + |
| 54 | +### Database Protection |
| 55 | + |
| 56 | +- `seen_submissions` table has unique constraint on |
| 57 | + `(channel_id, submission_id)` |
| 58 | +- `mark_submissions_seen()` handles `IntegrityError` gracefully |
| 59 | +- **Problem**: Notifications already sent before constraint violation |
| 60 | + |
| 61 | +### Potential Triggers |
| 62 | + |
| 63 | +- Multiple rapid manual checks (`.trigger` command) |
| 64 | +- Cloud Run scaling events causing multiple instances |
| 65 | +- API response timing variations |
| 66 | +- Database connection delays |
| 67 | + |
| 68 | +## Proposed Solutions |
| 69 | + |
| 70 | +### Option 1: Atomic Transaction |
| 71 | + |
| 72 | +```python |
| 73 | +async def process_submissions_atomically(self, channel_id, submissions): |
| 74 | + async with self.db.transaction(): |
| 75 | + new_submissions = self.db.filter_new_submissions(channel_id, submissions) |
| 76 | + if new_submissions: |
| 77 | + self.db.mark_submissions_seen(channel_id, [s["id"] for s in new_submissions]) |
| 78 | + await self.post_submissions(new_submissions) |
| 79 | +``` |
| 80 | + |
| 81 | +### Option 2: Channel-Level Locking |
| 82 | + |
| 83 | +```python |
| 84 | +class Runner: |
| 85 | + def __init__(self): |
| 86 | + self.channel_locks = {} |
| 87 | + |
| 88 | + async def run_checks_for_channel(self, channel_id, config, is_manual_check=False): |
| 89 | + if channel_id not in self.channel_locks: |
| 90 | + self.channel_locks[channel_id] = asyncio.Lock() |
| 91 | + |
| 92 | + async with self.channel_locks[channel_id]: |
| 93 | + # Existing check logic |
| 94 | +``` |
| 95 | + |
| 96 | +### Option 3: Submission ID Tracking |
| 97 | + |
| 98 | +```python |
| 99 | +# Track recently processed submission IDs in memory |
| 100 | +self.recently_processed = {} # {channel_id: set(submission_ids)} |
| 101 | + |
| 102 | +async def filter_new_submissions(self, channel_id, submissions): |
| 103 | + # Filter by database AND recent memory |
| 104 | + recent = self.recently_processed.get(channel_id, set()) |
| 105 | + truly_new = [s for s in db_filtered if s["id"] not in recent] |
| 106 | + return truly_new |
| 107 | +``` |
| 108 | + |
| 109 | +## Acceptance Criteria |
| 110 | + |
| 111 | +- [ ] No duplicate notifications under rapid polling scenarios |
| 112 | +- [ ] Multiple manual checks don't cause duplicates |
| 113 | +- [ ] Database integrity maintained |
| 114 | +- [ ] Performance impact minimal |
| 115 | +- [ ] Cloud Run scaling doesn't trigger race conditions |
| 116 | +- [ ] Test with concurrent `.trigger` commands |
| 117 | + |
| 118 | +## Notes |
| 119 | + |
| 120 | +- Difficult to reproduce consistently in testing |
| 121 | +- May require load testing to verify fix |
| 122 | +- Related to Cloud Run scaling and multiple instances |
| 123 | +- Consider distributed locking for multi-instance deployments |
0 commit comments