spring-modulith icon indicating copy to clipboard operation
spring-modulith copied to clipboard

Completed event publications can be claimed for resubmission and marked as failed again

Open char-yb opened this issue 1 month ago • 2 comments

The status transitions in the event publication repositories only check that a publication isn't already in the target state. They don't check whether it has completed in the meantime. In JpaEventPublicationRepository:

// used by markFailed(UUID) and markProcessing(UUID)
update DefaultJpaEventPublication p
   set p.status = ?1
 where p.id = ?2
   and (status is null or status != ?1)

// markResubmitted(UUID, Instant)
update DefaultJpaEventPublication p
   set p.status = …RESUBMITTED, …
 where p.id = ?2
   and (p.status is null or p.status != …RESUBMITTED)

The JDBC and Neo4j repositories use the same conditions (I only checked JPA by running tests). The MongoDB repository already guards both markFailed(…) and markResubmitted(…) with a completionDate null check, so this would bring the other stores in line with it.

processFailedPublications(…) reads failed publications first and claims each one with markResubmitted(…) afterwards. If a publication completes between the read and the claim, the claim still succeeds, and the listener is invoked again for an event that has already been processed. That can happen when two instances call resubmit(…) around the same time, or on a single instance when a slow listener invocation gets flagged by the staleness monitor and its original invocation completes right before the claim.

The markFailed(UUID) side has the same gap. The staleness monitor marks publications failed by id, so a publication that completes just after being picked up as stale ends up FAILED with a completion date set. It then shows up in FailedEventPublications and gets resubmitted once more. The same happens if the duplicate delivery from above fails.

With the UPDATE completion mode this is easy to reproduce on the repository level. These two tests fail on main (c103395) in JpaEventPublicationRepositoryIntegrationTests (WithUpdateCompletionTests). The DELETE and ARCHIVE variants pass, since the completed row is no longer in the table:

@Test
void doesNotResubmitCompletedPublication() {

      var publication = createPublication(new TestEvent("completed"));

      repository.markCompleted(publication.getIdentifier(), Instant.now());
      em.flush();
      em.clear();

      assertThat(repository.markResubmitted(publication.getIdentifier(), Instant.now())).isFalse();
}

@Test
void doesNotMarkCompletedPublicationFailed() {

      var publication = createPublication(new TestEvent("completed"));

      repository.markCompleted(publication.getIdentifier(), Instant.now());
      em.flush();
      em.clear();

      repository.markFailed(publication.getIdentifier());
      em.clear();

      assertThat(repository.findByStatus(Status.FAILED)).isEmpty();
}

Adding and p.completionDate is null to both statements makes them pass, and the rest of the JPA module tests still pass. This would be in line with doesNotAbandonPublicationThatHasBeenConcurrentlyResubmitted, which already protects the abandon transition against a similar race.

Related, but not the same: #926 describes duplicates from two resubmission paths running at the same time, which predates the markResubmitted(…) claim. The guards were last changed in ef3f400b to also cover null statuses, and this would extend them so that completed publications are left alone. It doesn't need cross-instance locking like #871, since the condition alone is enough to prevent claiming a completed publication.

Found on 2.1.1, main has the same statements. I'm happy to send a PR covering the JPA, JDBC and Neo4j repositories with tests, plus a regression test for MongoDB.

char-yb avatar Oct 06 '26 06:10 char-yb

That's fixed by applying your PR. Please give the snapshots a try!

odrotbohm avatar Oct 09 '26 12:10 odrotbohm

@odrotbohm Thanks for applying the PR and backporting it!

I gave 2.1.2-SNAPSHOT (2.1.2-20261009.123853-22) a try with the UPDATE completion mode, and it works as expected: completed publications are no longer claimed for resubmission or marked as failed again, so their listeners aren't invoked a second time.

char-yb avatar Oct 09 '26 13:10 char-yb