Skip to content

fix(listener): prevent concurrent processing of the same event - #1101

Open
maxx-GENIUS wants to merge 1 commit into
Core-Foundry:mainfrom
maxx-GENIUS:fix/prevent-concurrent-event-processing
Open

maxx-GENIUS wants to merge 1 commit into
Core-Foundry:mainfrom
maxx-GENIUS:fix/prevent-concurrent-event-processing

Conversation

@maxx-GENIUS

Copy link
Copy Markdown

Summary

This PR prevents concurrent workers from processing the same event simultaneously and triggering duplicate downstream actions/notifications.

Root Cause

Previously, EventSubscriber.processEvent only performed a non-exclusive check-then-act lookup (isDuplicate), allowing concurrent worker executions to interleave. Furthermore, EventDeduplicationService.claimEvent attempted to write 'PROCESSING' status to processed_events, but the SQLite table constraint only permitted CHECK (status IN ('PROCESSED', 'SKIPPED', 'ERROR')). This caused an SQLITE_CONSTRAINT check violation which fell back into the error handler, failing open and returning claimed: true to every racing worker.

Changes

  1. Database Schema & Migrations:
    • Updated processed_events CHECK constraint in schema.sql and 006-database-data-integrity.ts to include 'PROCESSING'.
  2. Event Deduplication Service:
    • Added releaseClaim(eventId, contractAddress) method to allow explicit cleanup of in-flight claims if a process is aborted or cancelled before completion.
  3. Event Subscriber Integration:
    • Switched EventSubscriber.processEvent from isDuplicate to atomic claimEvent, ensuring only one worker holds the processing claim.
  4. Test Suite:
    • Added unit tests in concurrent-event-processing.test.ts covering concurrent claims, lease expiry recovery, and explicit claim release.

Verification

  • Ran npx jest src/services/concurrent-event-processing.test.ts src/services/event-deduplication-service.test.ts in listener:
    • 2 test suites passed, 40/40 tests green (100% pass, exit code 0).

Closes #1072

- Update processed_events CHECK constraint in schema and migration 006 to allow 'PROCESSING' status
- Wire claimEvent into EventSubscriber.processEvent to atomically acquire lock before processing
- Add releaseClaim to EventDeduplicationService to clean up abandoned/aborted claims
- Update concurrent processing unit tests verifying concurrency protection and claim release

Closes Core-Foundry#1072
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Prevent Concurrent Processing of the Same Event

1 participant