From 7e7697bf4bb026545b9365408328a96cb98a7415 Mon Sep 17 00:00:00 2001 From: Gary Yu Date: Tue, 9 Oct 2018 20:38:36 +0800 Subject: [PATCH] fix: always put txhashset.write before store.batch to avoid dead lock (#1699) * fix: to avoid dead lock, always put txhashset.write before store.batch * fix: move txhashset.write behind store.batch --- chain/src/chain.rs | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/chain/src/chain.rs b/chain/src/chain.rs index 2d3c29a38..c604125ec 100644 --- a/chain/src/chain.rs +++ b/chain/src/chain.rs @@ -219,8 +219,8 @@ impl Chain { fn process_block_single(&self, b: Block, opts: Options) -> Result, Error> { let maybe_new_head: Result, Error>; { - let batch = self.store.batch()?; let mut txhashset = self.txhashset.write().unwrap(); + let batch = self.store.batch()?; let mut ctx = self.new_ctx(opts, batch, &mut txhashset)?; maybe_new_head = pipe::process_block(&b, &mut ctx); @@ -299,8 +299,8 @@ impl Chain { /// Process a block header received during "header first" propagation. pub fn process_block_header(&self, bh: &BlockHeader, opts: Options) -> Result<(), Error> { - let batch = self.store.batch()?; let mut txhashset = self.txhashset.write().unwrap(); + let batch = self.store.batch()?; let mut ctx = self.new_ctx(opts, batch, &mut txhashset)?; pipe::process_block_header(bh, &mut ctx)?; ctx.batch.commit()?; @@ -315,8 +315,8 @@ impl Chain { headers: &Vec, opts: Options, ) -> Result<(), Error> { - let batch = self.store.batch()?; let mut txhashset = self.txhashset.write().unwrap(); + let batch = self.store.batch()?; let mut ctx = self.new_ctx(opts, batch, &mut txhashset)?; pipe::sync_block_headers(headers, &mut ctx)?; @@ -666,17 +666,6 @@ impl Chain { status.on_save(); - // Replace the chain txhashset with the newly built one. - { - let mut txhashset_ref = self.txhashset.write().unwrap(); - *txhashset_ref = txhashset; - } - - debug!( - LOGGER, - "chain: txhashset_write: replaced our txhashset with the new one" - ); - // Save the new head to the db and rebuild the header by height index. { let tip = Tip::from_block(&header); @@ -693,6 +682,17 @@ impl Chain { "chain: txhashset_write: finished committing the batch (head etc.)" ); + // Replace the chain txhashset with the newly built one. + { + let mut txhashset_ref = self.txhashset.write().unwrap(); + *txhashset_ref = txhashset; + } + + debug!( + LOGGER, + "chain: txhashset_write: replaced our txhashset with the new one" + ); + // Check for any orphan blocks and process them based on the new chain state. self.check_orphans(header.height + 1);