diff options
author | Roman Smrž <roman.smrz@seznam.cz> | 2024-08-11 23:30:36 +0200 |
---|---|---|
committer | Roman Smrž <roman.smrz@seznam.cz> | 2024-08-11 23:32:51 +0200 |
commit | a4d433bf79ad4f4cb99dc9d5b632ea9cff054a91 (patch) | |
tree | c465190887ceb3a945f8e4ced920f1eaa8cd1f66 /src/main.cpp | |
parent | 7b27c1106d77f27808639336610caeaf49f5274c (diff) |
Test: avoid access to server object after stopping
Diffstat (limited to 'src/main.cpp')
-rw-r--r-- | src/main.cpp | 25 |
1 files changed, 19 insertions, 6 deletions
diff --git a/src/main.cpp b/src/main.cpp index a0a9458..c92d630 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -69,6 +69,7 @@ void printLine(const string & line) Storage st(getErebosDir()); optional<Head<LocalState>> testHead; +mutex testHeadMutex; // for updates from main and reading from other threads optional<Server> server; struct TestPeer @@ -233,8 +234,10 @@ void createIdentity(const vector<string> & args) ret = ret.template shared<optional<Identity>>(identity->finalOwner()); return st.store(ret); }); - if (nh) + if (nh) { + scoped_lock lock(testHeadMutex); *testHead = *nh; + } } } @@ -289,8 +292,12 @@ void startServer(const vector<string> &) config.service<DirectMessageService>() .onUpdate([](const DirectMessageThread & thread, ssize_t, ssize_t) { - if (thread.at(0).from()->sameAs(server->identity().finalOwner())) - return; + { + scoped_lock lock(testHeadMutex); + if (auto locIdentity = testHead.value()->identity()) + if (thread.at(0).from()->sameAs(locIdentity->finalOwner())) + return; + } ostringstream ss; @@ -456,8 +463,10 @@ void updateLocalIdentity(const vector<string> & params) b.name(params[0]); return st.store(loc->identity(b.commit())); }); - if (nh) + if (nh) { + scoped_lock lock(testHeadMutex); *testHead = *nh; + } } void updateSharedIdentity(const vector<string> & params) @@ -476,8 +485,10 @@ void updateSharedIdentity(const vector<string> & params) b.name(params[0]); return st.store(loc->shared<optional<Identity>>(optional(b.commit()))); }); - if (nh) + if (nh) { + scoped_lock lock(testHeadMutex); *testHead = *nh; + } } void attachTo(const vector<string> & params) @@ -538,8 +549,10 @@ void contactSetName(const vector<string> & args) auto contacts = loc->shared<Set<Contact>>(); return st.store(loc->shared<Set<Contact>>(contacts.add(st, cc))); }); - if (nh) + if (nh) { + scoped_lock lock(testHeadMutex); *testHead = *nh; + } printLine("contact-set-name-done"); } |