Skip to content

Bug: race condition could lead to lost writes #2405

Closed

Description

@ozkatz

I believe there's a race condition in Graveler, that in some cases could lead to lost writes.
This is a design flaw with graveler.Set():

As it is currently implemented, it relies on two independent DB connections:

  1. one that tries to ensure mutual exclusion between writing to a branch, and changing it (for example, a commit will delete all records with the branch's current staging token, replacing it with a new one).
  2. one that, once acquired, relies on said mutual exclusion to read the branch's current staging token, and add records to it.

Since these are two separate mechanisms, lock safety is not guaranteed, especially in the face of a connection error or reset: the use of pg_advisory_xact_lock_shared as the locking mechanism, ties the lock's liveness to the life of the PostgreSQL transaction.

This means that the branch locker is essentially broken: it can not guarantee there are no modifications to a branch while the callback is running, which will in this case, result in writes being lost, as illustrated below:

image

While this is a possible scenario, it relies on 2 rather uncommon events happening at the same time:

  1. a network failure (or some other reason causing the Postgresql server to terminate the locker's transaction) but one that does not affect the successful completion of the writer's transaction underlying connection
  2. simultaneously, a commit (or another event that mutates the branch's staging token) must begin after the writer function has read the current staging token for the branch.

While this is (arguably?) a rare scenario, it violates a basic guarantee provided by lakeFS.

Activity

  1. added
    bug
    area/DB
    on Aug 25, 2021
  2. arielshaqed commented on Aug 25, 2021

    @arielshaqed
    Contributor

    After F2F discussions I agree that this scenario is possible.

    In general, as soon as we have 2 transactions, the onus of proving consistency is on us. I propose that we change the commit/write transaction to be safe, and use the separate locking transaction purely in order to give the desired blocking semantics.

    One way to do this (suggested by @ozkatz) is to use the SERIALIZABLE isolation level. That will work, but the performance implications will be unpleasant. We could do it with just REPEATABLE READ semantics (the default isolation level, so no overhead in the uncontended case!). Wrap every mutation operation (write or commit) with a read of the staging token. Fail if the before and after tokens don't match.

    BEGIN;
    SELECT before_token FROM get_staging_token_for_branch();
    -- SQL commands;
    SELECT after_token FROM get_staging_token_for_branch();
    -- ABORT unless before_token = after_token;
    COMMIT;
  3. ozkatz commented on Aug 25, 2021

    @ozkatz
    CollaboratorAuthor

    Fail if the before and after tokens don't match.

    @arielshaqed yep, so another change we'll have to make, unless we want to modify behavior - is to retry this in a blocking loop somewhere else in the stack.

  4. arielshaqed commented on Aug 25, 2021

    @arielshaqed
    Contributor

    Fail if the before and after tokens don't match.

    @arielshaqed yep, so another change we'll have to make, unless we want to modify behavior - is to retry this in a blocking loop somewhere else in the stack.

    That's a feature IMO: We shall have to do this anyway when we move to a db that has weaker consistency.

  5. added a commit that references this issue on Sep 27, 2021
  6. itaiad200 commented on Feb 16, 2023

    @itaiad200
    Contributor

    Fixed with the move to kv.

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

    area/DBbugteam/versioning-engine

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions