Skip to content

Commit

Permalink
Merge pull request #592 from steemit/36-callback-crash-fix
Browse files Browse the repository at this point in the history
fixes for #36 #355 and #439
  • Loading branch information
Michael Vandeberg committed Nov 22, 2016
2 parents 641cc5b + 1ac33f4 commit e8c1deb
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 5 deletions.
54 changes: 49 additions & 5 deletions libraries/app/api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,54 @@ namespace steemit { namespace app {

void network_broadcast_api::on_applied_block( const signed_block& b )
{
int32_t block_num = int32_t(b.block_num());
/// we need to ensure the database_api is not deleted for the life of the async operation
auto capture_this = shared_from_this();

fc::async( [this,capture_this,b]() {
int32_t block_num = int32_t(b.block_num());
if( _callbacks.size() )
{
for( int32_t trx_num = 0; trx_num < b.transactions.size(); ++trx_num )
{
const auto& trx = b.transactions[trx_num];
auto id = trx.id();
auto itr = _callbacks.find(id);
if( itr == _callbacks.end() ) continue;
confirmation_callback callback = itr->second;
itr->second = [](variant){};
callback( fc::variant(transaction_confirmation( id, block_num, trx_num, false )) );
}
}

/// clear all expirations
while( true )
{
auto exp_it = _callbacks_expirations.begin();
if( exp_it == _callbacks_expirations.end() )
break;
if( exp_it->first >= b.timestamp )
break;
for( const transaction_id_type& txid : exp_it->second )
{
auto cb_it = _callbacks.find( txid );
// If it's empty, that means the transaction has been confirmed and has been deleted by the above check.
if( cb_it == _callbacks.end() )
continue;

confirmation_callback callback = cb_it->second;
transaction_id_type txid_byval = txid; // can't pass in by reference as it's going to be deleted
callback( fc::variant(transaction_confirmation{ txid_byval, block_num, -1, true}) );

//fc::async( [capture_this,block_num,txid_byval,callback](){
_callbacks.erase( cb_it );
}
_callbacks_expirations.erase( exp_it );
}
}); /// fc::async

/*
if( _callbacks.size() )
{
/// we need to ensure the database_api is not deleted for the life of the async operation
auto capture_this = shared_from_this();
for( int32_t trx_num = 0; trx_num < b.transactions.size(); ++trx_num )
{
const auto& trx = b.transactions[trx_num];
Expand All @@ -162,14 +205,13 @@ namespace steemit { namespace app {
}
}
}

/// clear all expirations
while( true )
{
auto exp_it = _callbacks_expirations.begin();
if( exp_it == _callbacks_expirations.end() )
break;
if( exp_it->first > b.timestamp )
if( exp_it->first >= b.timestamp )
break;
for( const transaction_id_type& txid : exp_it->second )
{
Expand All @@ -185,6 +227,8 @@ namespace steemit { namespace app {
}
_callbacks_expirations.erase( exp_it );
}
*/

}

void network_broadcast_api::broadcast_transaction(const signed_transaction& trx)
Expand Down
4 changes: 4 additions & 0 deletions libraries/app/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,7 @@ namespace detail {
throw;
}
}
return false;
} FC_CAPTURE_AND_RETHROW( (blk_msg)(sync_mode) ) }

virtual void handle_transaction(const graphene::net::trx_message& transaction_message) override
Expand Down Expand Up @@ -962,6 +963,9 @@ void application::get_max_block_age( int32_t& result )

void application::shutdown_plugins()
{
my->_p2p_network->close();
my->_p2p_network.reset();
fc::usleep( fc::seconds(1) );
for( auto& entry : my->_plugins_enabled )
entry.second->plugin_shutdown();
return;
Expand Down

0 comments on commit e8c1deb

Please sign in to comment.