From d88484862584afd9bebdc2c5249b821af54c39ee Mon Sep 17 00:00:00 2001 From: Boqin Qin Date: Mon, 8 Jun 2020 19:39:50 +0800 Subject: [PATCH] client/api: fix possible deadlock when comparing with itself (#6277) --- substrate/client/api/src/in_mem.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/substrate/client/api/src/in_mem.rs b/substrate/client/api/src/in_mem.rs index 45c41fbcb7..1de2747eb4 100644 --- a/substrate/client/api/src/in_mem.rs +++ b/substrate/client/api/src/in_mem.rs @@ -19,6 +19,7 @@ //! In memory client backend use std::collections::HashMap; +use std::ptr; use std::sync::Arc; use parking_lot::RwLock; use sp_core::{ @@ -191,11 +192,19 @@ impl Blockchain { /// Compare this blockchain with another in-mem blockchain pub fn equals_to(&self, other: &Self) -> bool { + // Check ptr equality first to avoid double read locks. + if ptr::eq(self, other) { + return true; + } self.canon_equals_to(other) && self.storage.read().blocks == other.storage.read().blocks } /// Compare canonical chain to other canonical chain. pub fn canon_equals_to(&self, other: &Self) -> bool { + // Check ptr equality first to avoid double read locks. + if ptr::eq(self, other) { + return true; + } let this = self.storage.read(); let other = other.storage.read(); this.hashes == other.hashes