summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoan Touzet <joant@atypical.net>2017-04-30 02:49:20 -0400
committerJoan Touzet <joant@atypical.net>2017-05-02 13:49:38 -0400
commit1aa48ef245a9f5d8d336d43070767363ef092ed1 (patch)
tree5d9a5c541708274bef25d790a475d48a1ac5e82c
parentd1b16e28d41020d69d968d3b271c4476af6bde66 (diff)
downloadcouchdb-3402-mem3-race.tar.gz
Fix error on race condition in mem3 startup3402-mem3-race
During mem3 startup, 2 paths attempt to call `couch_server:create/2` on `_dbs`: ``` gen_server:init_it/6 -> mem3_shards:init/1 -> mem3_shards:get_update_seq/0 -> couch_server:create/2 ``` and ``` mem3_sync:initial_sync/1 -> mem3_shards:fold/2 -> couch_server:create/2 ``` Normally, the first path completes before the second. If the second path finishes first, the first path fails because it does not expect a `file_exists` response. This patch makes `mem3_util:ensure_enxists/1` more robust in the face of a race to create `_dbs`. Fixes COUCHDB-3402. Approved by @davisp and @iilyak
-rw-r--r--src/mem3/src/mem3_util.erl7
1 files changed, 4 insertions, 3 deletions
diff --git a/src/mem3/src/mem3_util.erl b/src/mem3/src/mem3_util.erl
index 2cd444d8c..71ef5b6c9 100644
--- a/src/mem3/src/mem3_util.erl
+++ b/src/mem3/src/mem3_util.erl
@@ -214,11 +214,12 @@ shard_info(DbName) ->
ensure_exists(DbName) when is_list(DbName) ->
ensure_exists(list_to_binary(DbName));
ensure_exists(DbName) ->
- case couch_db:open(DbName, [nologifmissing, sys_db | [?ADMIN_CTX]]) of
+ Options = [nologifmissing, sys_db, {create_if_missing, true}, ?ADMIN_CTX],
+ case couch_db:open(DbName, Options) of
{ok, Db} ->
{ok, Db};
- _ ->
- couch_server:create(DbName, [?ADMIN_CTX])
+ file_exists ->
+ couch_db:open(DbName, [sys_db, ?ADMIN_CTX])
end.