Race condition on mod-patron-blocks when handling parallel check-out requests

Summary

In the process of analyzing the problem with the loan policy limits (Concurrent Check-outs via Self-check do not check for patron item limits, more details on the link https://wiki.folio.org/pages/viewpage.action?spaceKey=~Raman+Auramau&title=CIRC-1345), another potential problem was revealed when processing several check-out requests in parallel - this time with the so-called automated patron block. This document describes the context and some details of the mentioned problem. 

Context

The automated patron block is the calculation and verification of limits at the level of patron.

The mechanism is as follows -

  1. at the very beginning of processing a check-out request, a request is made to the mod-patron-blocks for the presence of a patron block.
  2. If there is no blocking, then the following steps are performed on the check-out flow,
  3. and at the very end, already when creating a loan, a notification is sent that the loan has been created.
  4. Notification goes through OkApi to the mod-pub-sub, then to Kafka. In turn, the mod-patron-blocks is subscribed to these notifications in the mod-pub-sub
  5. After a while the mod-patron-blocks receives this notification.
  6. Having received the notification, the mod-patron-blocks checks its rules and limits, and when the conditions are met, it blocks the patron.

As a result, there is a significant gap between, say, the creation of the last permitted loan (step 3 from the above), and the moment when it is checked and processed in mod-patron-blocks (step 6).

The image below shows the described flow as a sequence diagram.

PlantUML diagram

 Source code for PlantUML...

@startuml
!pragma teoz true

title Race condition on mod-patron-blocks when handling parallel check-out requests
 
participant "mod-circulation" as modc
participant "mod-pub-sub" as modps
queue "Kafka topic" as kafka
participant "mod-patron-blocks" as modpb

[-> modc: CheckOut Request
activate modc
note left of modc
CheckOutByBarcodeResource#checkOut
end note

note right of modc
.#refuseWhenCheckOutActionIsBlockedAutomaticallyForPatron
end note
modc -> modpb: Check Patron Blocked
modpb -> modpb: Verification
modpb -> modc: Not blocked

modc -> modc: CheckOut process

note right of modc
.#publishItemCheckedOutEvent
end note
{start} modc -> modps: publish an event
[<- modc: Done
deactivate modc

modps -> kafka: publish the event
activate kafka
deactivate kafka

modpb -> modps: retrieve the event
modps -> modpb: the event
{end} modpb -> modpb: Update counters and limits
{start} <-> {end}: time delay between\nthe actual check-out and\nthe processing of this event\nin mod-patron-block
@enduml

Apparently, to date, this problem has not yet manifested itself (at least, it has not been reported as a bug).