diff options
author | Joan Touzet <joant@atypical.net> | 2017-04-30 02:49:20 -0400 |
---|---|---|
committer | Joan Touzet <wohali@users.noreply.github.com> | 2017-05-02 14:14:58 -0400 |
commit | 81ee7c5ac71e617a03e967b4fc5d0358f4ba9459 (patch) | |
tree | 510a883f44c575ac68f9f2fc100cba16821d9196 | |
parent | 63278f25733ef7b08d84060192b9e67badec8072 (diff) | |
download | couchdb-81ee7c5ac71e617a03e967b4fc5d0358f4ba9459.tar.gz |
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
-rw-r--r-- | src/mem3/src/mem3_util.erl | 7 |
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. |