From 9f006c1b5736c46f6a288f598356578fd7608dee Mon Sep 17 00:00:00 2001
From: Matej Ferencevic <matej.ferencevic@memgraph.io>
Date: Fri, 20 Mar 2020 11:08:17 +0100
Subject: [PATCH] Fix storage GC indices/constraints subtle race condition

Summary:
In order for this race condition to cause damage, an index/constraint must be
created/dropped at the exact moment that the GC is cleaning
indices/constraints.

Reviewers: teon.banek

Reviewed By: teon.banek

Subscribers: pullbot

Differential Revision: https://phabricator.memgraph.io/D2733
---
 src/storage/v2/storage.cpp | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/src/storage/v2/storage.cpp b/src/storage/v2/storage.cpp
index 3ba75cd38..4309663e8 100644
--- a/src/storage/v2/storage.cpp
+++ b/src/storage/v2/storage.cpp
@@ -1147,6 +1147,11 @@ Transaction Storage::CreateTransaction() {
 }
 
 void Storage::CollectGarbage() {
+  // Because the garbage collector iterates through the indices and constraints
+  // to clean them up, it must take the main lock for reading to make sure that
+  // the indices and constraints aren't concurrently being modified.
+  std::shared_lock<utils::RWLock> main_guard(main_lock_);
+
   // Garbage collection must be performed in two phases. In the first phase,
   // deltas that won't be applied by any transaction anymore are unlinked from
   // the version chains. They cannot be deleted immediately, because there