bitshares / bitshares1-core

Software to run the old chain (before 2015-10-13). Code for current chain is https://github.com/bitshares/bitshares-core
https://bitshares.org/
The Unlicense
219 stars 174 forks source link

Prevent opening multiple clients #1093

Open drltc opened 9 years ago

drltc commented 9 years ago

I get this error when accidentally running two clients with the same datadir:

Loading blockchain from: /home/cc/btsxt/.BitSharesXTS-Test67/chain
Loading config from file: /home/cc/btsxt/.BitSharesXTS-Test67/config.json
------------ error --------------
10 assert_exception: Assert Exception
_map->is_open(): Database is not open!
    {}
    th_a  level_map.hpp:305 commit
error applying batch
    {}
    th_a  level_map.hpp:314 commit

    {}
    th_a  cached_level_map.hpp:52 flush

    {}
    th_a  cached_level_map.hpp:28 close

    {}
    th_a  chain_database.cpp:1295 close

    {"data_dir":"/home/cc/btsxt/.BitSharesXTS-Test67/chain"}
    th_a  chain_database.cpp:1251 open

    {"data_dir":"/home/cc/btsxt/.BitSharesXTS-Test67"}
    th_a  client.cpp:1294 open

We should explicitly check if a client is running and print a nice error message, instead of an ugly exception report. I am thinking Boost file lock on a file in the data directory should do the trick: http://www.boost.org/doc/libs/1_57_0/doc/html/interprocess/synchronization_mechanisms.html#interprocess.synchronization_mechanisms.file_lock

It claims the lock is released by the OS if we crash, so if the "am I already running?" logic is implemented with this, it should robustly give the right answer even if the previous process crashed.

Alternatively we could just catch and wrap the exception that's already being thrown.

vikramrajkumar commented 9 years ago

This error is actually not directly related to having 2 clients open, but was a bug that could show up in other circumstances as well: https://github.com/BitShares/bitshares/commit/3930bfef2ba30977250b1df01af21fc74122dcd3

We do need a way to prevent multiple clients from being open, however.

vikramrajkumar commented 9 years ago

I forgot I had a client reindexing, and I opened a copy of the GUI:

~/bitshares/build (bitshares ✘)✹ ᐅ ./programs/client/bitshares_client --rebuild-index
Loading config from file: /Users/vikram/Library/Application Support/BitShares/config.json
Using blockchain checkpoints from file: /Users/vikram/Library/Application Support/BitShares/checkpoints.json
Erasing all state
Initializing state from built-in genesis file
Please be patient, this will take several minutes...
Replaying blockchain... Approximately 16.69% complete.------------ error --------------
10000 level_map_failure: level_map failure
database error: IO error: /Users/vikram/Library/Application Support/BitShares/chain/index/fork_db/000045.log: No such file or directory
    {"msg":"IO error: /Users/vikram/Library/Application Support/BitShares/chain/index/fork_db/000045.log: No such file or directory"}
    th_a  level_map.hpp:361 store
error storing c150b5f0b7279f00cff17043f13ba4dc9d6a15af = {"next_blocks":[],"is_linked":true,"is_valid":null,"invalid_reason":null,"is_included":false,"is_known":true}
    {"key":"c150b5f0b7279f00cff17043f13ba4dc9d6a15af","value":{"next_blocks":[],"is_linked":true,"is_valid":null,"invalid_reason":null,"is_included":false,"is_known":true}}
    th_a  level_map.hpp:363 store

    {"block_id":"c150b5f0b7279f00cff17043f13ba4dc9d6a15af"}
    th_a  chain_database.cpp:657 store_and_index

    {"block_data":{"previous":"9d481fc03bb8c59084ef74b225e44b460dddbf59","block_num":291736,"timestamp":"2014-08-22T22:21:20","transaction_digest":"c8cf12fe3180ed901a58a0697a522f1217de72d04529bd255627a4ad6164f0f0","next_secret_hash":"5b498bfbef87888147858c22f45a3d1767de7733","previous_secret":"2bd31a26eff63a65a2b59a2dcccc318d5d44238b","delegate_signature":"1f1f6b206eb656b416e248f0458d96e79893061e01d92b29bce004b4af243b9d7ec3c186aa4fa429bf5e5cf0c2c54f1b722e67e873508dfdf7f07590d36d5fa5f7","user_transactions":[]}}
    th_a  chain_database.cpp:1802 push_block

    {"data_dir":"/Users/vikram/Library/Application Support/BitShares/chain"}
    th_a  chain_database.cpp:1484 open

    {"data_dir":"/Users/vikram/Library/Application Support/BitShares"}
    th_a  client.cpp:1346 open