Skip to content

Commit 81ee7c5

Browse files
committed
Fix error on race condition in mem3 startup
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
1 parent 63278f2 commit 81ee7c5

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

src/mem3/src/mem3_util.erl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -214,11 +214,12 @@ shard_info(DbName) ->
214214
ensure_exists(DbName) when is_list(DbName) ->
215215
ensure_exists(list_to_binary(DbName));
216216
ensure_exists(DbName) ->
217-
case couch_db:open(DbName, [nologifmissing, sys_db | [?ADMIN_CTX]]) of
217+
Options = [nologifmissing, sys_db, {create_if_missing, true}, ?ADMIN_CTX],
218+
case couch_db:open(DbName, Options) of
218219
{ok, Db} ->
219220
{ok, Db};
220-
_ ->
221-
couch_server:create(DbName, [?ADMIN_CTX])
221+
file_exists ->
222+
couch_db:open(DbName, [sys_db, ?ADMIN_CTX])
222223
end.
223224

224225

0 commit comments

Comments
 (0)