Skip to content

Commit cfe30cc

Browse files
committed
Fixed race condition on ProcessingServiceImpl's destructor
1 parent 8b64aae commit cfe30cc

2 files changed

Lines changed: 41 additions & 31 deletions

File tree

src/processing/processing_service.cpp

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,13 @@ namespace sgns::processing
145145
m_logger->debug( "List of processing channels requested" );
146146
m_timerChannelListRequestTimeout.expires_from_now( m_channelListRequestTimeout );
147147
m_timerChannelListRequestTimeout.async_wait(
148-
[instance = shared_from_this()]( const boost::system::error_code & )
149-
{ instance->HandleRequestTimeout(); } );
148+
[instance = weak_from_this()]( const boost::system::error_code & )
149+
{
150+
if ( auto strong = instance.lock() )
151+
{
152+
strong->HandleRequestTimeout();
153+
}
154+
} );
150155
}
151156

152157
void ProcessingServiceImpl::OnMessage( boost::optional<const ipfs_pubsub::GossipPubSub::Message &> message )
@@ -406,34 +411,39 @@ namespace sgns::processing
406411
m_channelListRequestTimeout = channelListRequestTimeout;
407412
}
408413

409-
ProcessingServiceImpl::ProcessingStatus ProcessingServiceImpl::GetProcessingStatus() const {
410-
if (m_isStopped) {
411-
return ProcessingStatus(Status::DISABLED, 0.0f);
414+
ProcessingServiceImpl::ProcessingStatus ProcessingServiceImpl::GetProcessingStatus() const
415+
{
416+
if ( m_isStopped )
417+
{
418+
return ProcessingStatus( Status::DISABLED, 0.0f );
412419
}
413-
414-
float totalProgress = 0.0f;
415-
size_t nodeCount = 0;
416-
420+
421+
float totalProgress = 0.0f;
422+
size_t nodeCount = 0;
423+
417424
{
418-
std::lock_guard lock(m_mutexNodes);
419-
if (m_processingNodes.empty()) {
420-
return ProcessingStatus(Status::IDLE, 0.0f);
425+
std::lock_guard lock( m_mutexNodes );
426+
if ( m_processingNodes.empty() )
427+
{
428+
return ProcessingStatus( Status::IDLE, 0.0f );
421429
}
422-
430+
423431
// Calculate average progress across all processing nodes
424-
for (const auto& [queueId, node] : m_processingNodes) {
425-
if (node) {
432+
for ( const auto &[queueId, node] : m_processingNodes )
433+
{
434+
if ( node )
435+
{
426436
totalProgress += node->GetProgress();
427437
++nodeCount;
428438
}
429439
}
430440
}
431-
432-
float averageProgress = (nodeCount > 0) ? (totalProgress / nodeCount) : 0.0f;
441+
442+
float averageProgress = ( nodeCount > 0 ) ? ( totalProgress / nodeCount ) : 0.0f;
433443
// Round to 2 decimal places
434-
averageProgress = std::round(averageProgress * 100.0f) / 100.0f;
435-
436-
return ProcessingStatus(Status::PROCESSING, averageProgress);
444+
averageProgress = std::round( averageProgress * 100.0f ) / 100.0f;
445+
446+
return ProcessingStatus( Status::PROCESSING, averageProgress );
437447
}
438448

439449
void ProcessingServiceImpl::HandleRequestTimeout()

test/src/multiaccount/multi_account_sync.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class MultiAccountTest : public ::testing::Test
7575

7676
if ( isGenesisAuthorized )
7777
{
78-
auto response = sgns::GeniusAccount::GenerateGeniusAddress( key.c_str(), outPath );
78+
auto response = GeniusAccount::GenerateGeniusAddress( key.c_str(), outPath );
7979
if ( !response.has_value() )
8080
{
8181
ADD_FAILURE() << "Failed to generate full-node address for authorization";
@@ -141,26 +141,26 @@ TEST_F( MultiAccountTest, SyncThroughEachOther )
141141
auto node_full = CreateNode( "node_multi_full",
142142
"0xcafe",
143143
"1.0",
144-
sgns::TokenID::FromBytes( { 0x00 } ),
144+
TokenID::FromBytes( { 0x00 } ),
145145
true, // is full node
146146
true, // is processor
147147
true );
148148
test::assertWaitForCondition(
149149
[&]() { return node_full->GetTransactionManagerState() == TransactionManager::State::READY; },
150150
std::chrono::milliseconds( 30000 ),
151-
"node_full not synched" );
151+
"node_full not synced" );
152152
auto node_main = CreateNode( "node_multi_1",
153153
"0xcafe",
154154
"1.0",
155-
sgns::TokenID::FromBytes( { 0x00 } ),
155+
TokenID::FromBytes( { 0x00 } ),
156156
false, // not full node
157157
false // not processor
158158
);
159159

160160
auto node_proc1 = CreateNode( "node_multi_1",
161161
"0xcafe",
162162
"1.0",
163-
sgns::TokenID::FromBytes( { 0x00 } ),
163+
TokenID::FromBytes( { 0x00 } ),
164164
false, // not full node
165165
true // is processor
166166
);
@@ -173,11 +173,11 @@ TEST_F( MultiAccountTest, SyncThroughEachOther )
173173
test::assertWaitForCondition(
174174
[&]() { return node_proc1->GetTransactionManagerState() == TransactionManager::State::READY; },
175175
std::chrono::milliseconds( 30000 ),
176-
"node_proc1 not synched" );
176+
"node_proc1 not synced" );
177177
test::assertWaitForCondition(
178178
[&]() { return node_main->GetTransactionManagerState() == TransactionManager::State::READY; },
179179
std::chrono::milliseconds( 30000 ),
180-
"node_main not synched" );
180+
"node_main not synced" );
181181

182182
// Get initial state
183183
auto transcount_main_start = node_main->GetOutTransactions().size();
@@ -195,9 +195,9 @@ TEST_F( MultiAccountTest, SyncThroughEachOther )
195195

196196
std::cout << "Mint transaction on main node completed, waiting for sync..." << std::endl;
197197

198-
test::assertWaitForCondition( [&]() { return node_proc1->GetBalance() == 50000000000; },
198+
test::assertWaitForCondition( [&] { return node_proc1->GetBalance() == 50000000000; },
199199
std::chrono::milliseconds( 30000 ),
200-
"node_proc1 balance not synched" );
200+
"node_proc1 balance not synced" );
201201

202202
//TODO - this is not working at the moment
203203
//auto mint_received = node_proc1->WaitForTransactionOutgoing(
@@ -211,9 +211,9 @@ TEST_F( MultiAccountTest, SyncThroughEachOther )
211211
std::chrono::milliseconds( OUTGOING_TIMEOUT_MILLISECONDS ) );
212212
ASSERT_TRUE( mint_result.has_value() ) << "Mint transaction failed or timed out on node_proc1";
213213

214-
test::assertWaitForCondition( [&]() { return node_main->GetBalance() == 100000000000; },
214+
test::assertWaitForCondition( [&] { return node_main->GetBalance() == 100000000000; },
215215
std::chrono::milliseconds( 30000 ),
216-
"node_main balance not synched" );
216+
"node_main balance not synced" );
217217
//TODO - this is not working at the moment
218218
//auto mint_received2 = node_main->WaitForTransactionOutgoing(
219219
// mint_result.value().first,

0 commit comments

Comments
 (0)