Open elexisvenator opened 5 days ago
@elexisvenator Why are you reusing the same session in a long lived way like that? I do understand what it's doing wrong because the sessions cache the stream version, but first help me understand why you keep a session around that long?
We definitely don't have long lived sessions, though there are a few places where we fetch multiple aggregates of the same stream using FetchForWriting. This issue was picked up as part of some artificial load testing and I doubt that it has happened in real usage.
FetchForWriting creates an optimistic lock by setting startingVersion property on the stream action. However there is an edge case where if the stream action already exists and already has a startinVersion set, calling FetchForWriting will overwrite the value. This leads to the possibility of the following scenario, where two FetchForWriting calls made against the same stream will not detect a new event written between the two calls.
I think the solution to this is likely to only set the StartingVersion if it doesnt exist. This does lead to the problem where marten knows that something is going to fail before the save changes but says nothing, but I don't think this can be avoided as marten does not know if there is even going to be a save changes call at all.