From eef65b20fcb3408dad11ecc79cd5c914993e8ed2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felf=C3=B6ldi=20Zsolt?= Date: Mon, 6 Aug 2018 14:46:30 +0200 Subject: [PATCH] p2p: use safe atomic operations when changing connFlags (#17325) --- p2p/server.go | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/p2p/server.go b/p2p/server.go index 669ef740d..8f3a511f3 100644 --- a/p2p/server.go +++ b/p2p/server.go @@ -258,13 +258,18 @@ func (c *conn) is(f connFlag) bool { } func (c *conn) set(f connFlag, val bool) { - flags := connFlag(atomic.LoadInt32((*int32)(&c.flags))) - if val { - flags |= f - } else { - flags &= ^f + for { + oldFlags := connFlag(atomic.LoadInt32((*int32)(&c.flags))) + flags := oldFlags + if val { + flags |= f + } else { + flags &= ^f + } + if atomic.CompareAndSwapInt32((*int32)(&c.flags), int32(oldFlags), int32(flags)) { + return + } } - atomic.StoreInt32((*int32)(&c.flags), int32(flags)) } // Peers returns all connected peers.