Skip to content
This repository was archived by the owner on Aug 15, 2024. It is now read-only.
This repository was archived by the owner on Aug 15, 2024. It is now read-only.

Concurrent appends to same stream and ExpectedVersion.Any on Postgres throws WrongExpectedVersion #478

Description

@stoft

We tried to concurrently append to the same stream with ExpectedVersion.Any (commutative events), using Postgres as underlying DB, and it fails throwing WrongExpectedVersion. We've tried both using a single connection and with dedicated connections but with the same result.

For comparison I tried doing the same with MySQL which deadlocked on a single connection but passed on dedicated connections. I may give SQL Server a whirl tomorrow. The In Memory Store works fine.

SSS version: 1.2.0-beta.8 (latest published version of SSS.Postgres on nuget)
Postgres: 9.6 (local docker) & 12.3 (AWS RDS)

The example test case below uses dedicated connections (F# with Expecto, if needed I could probably convert it to C# within a few days):

ftest "repro concurrency error" {
    let streamStore () =
        let settings =
            SqlStreamStore.PostgresStreamStoreSettings
                ("Host=localhost;Port=5432;User Id=user;Password=password;Database=db")

        settings.Schema <- "teststreamstore"

        new SqlStreamStore.PostgresStreamStore(settings)

    let appendMsg msg =
        try
            (streamStore ())
                .AppendToStream(SqlStreamStore.Streams.StreamId
                                    ("test"),
                                SqlStreamStore.Streams.ExpectedVersion.Any,
                                [| msg |])
            |> Async.AwaitTask
            with exn ->
                failtest ("should not fail: " + exn.Message)

    let message () =
        SqlStreamStore.Streams.NewStreamMessage
            (System.Guid.NewGuid (), "some-type", "{}", "{}")

    List.map (fun _ -> appendMsg (message ())) [ 0 .. 10 ]
    |> List.map Async.RunSynchronously
};
Error Message:
   Append failed due to WrongExpectedVersion.Stream: test, Expected version: -2
  Stack Trace:
     at SqlStreamStore.PostgresStreamStore.AppendToStreamInternal(String streamId, Int32 expectedVersion, NewStreamMessage[] messages, CancellationToken cancellationToken)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions