From a4d433bf79ad4f4cb99dc9d5b632ea9cff054a91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roman=20Smr=C5=BE?= Date: Sun, 11 Aug 2024 23:30:36 +0200 Subject: Test: avoid access to server object after stopping --- src/main.cpp | 25 +++++++++++++++++++------ 1 file 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> testHead; +mutex testHeadMutex; // for updates from main and reading from other threads optional server; struct TestPeer @@ -233,8 +234,10 @@ void createIdentity(const vector & args) ret = ret.template shared>(identity->finalOwner()); return st.store(ret); }); - if (nh) + if (nh) { + scoped_lock lock(testHeadMutex); *testHead = *nh; + } } } @@ -289,8 +292,12 @@ void startServer(const vector &) config.service() .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 & 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 & params) @@ -476,8 +485,10 @@ void updateSharedIdentity(const vector & params) b.name(params[0]); return st.store(loc->shared>(optional(b.commit()))); }); - if (nh) + if (nh) { + scoped_lock lock(testHeadMutex); *testHead = *nh; + } } void attachTo(const vector & params) @@ -538,8 +549,10 @@ void contactSetName(const vector & args) auto contacts = loc->shared>(); return st.store(loc->shared>(contacts.add(st, cc))); }); - if (nh) + if (nh) { + scoped_lock lock(testHeadMutex); *testHead = *nh; + } printLine("contact-set-name-done"); } -- cgit v1.2.3