Fix concurrent map read/write, maybe

This commit is contained in:
Daniel 2020-04-24 09:59:36 +02:00
parent e7149ac226
commit 350555f843
2 changed files with 13 additions and 4 deletions

View file

@ -41,8 +41,14 @@ func cleanConnections() (activePIDs map[int]struct{}) {
now := time.Now().Unix() now := time.Now().Unix()
deleteOlderThan := time.Now().Add(-deleteConnsAfterEndedThreshold).Unix() deleteOlderThan := time.Now().Add(-deleteConnsAfterEndedThreshold).Unix()
// network connections // lock both together because we cannot fully guarantee in which map a connection lands
// of course every connection should land in the correct map, but this increases resilience
connsLock.Lock() connsLock.Lock()
defer connsLock.Unlock()
dnsConnsLock.Lock()
defer dnsConnsLock.Unlock()
// network connections
for key, conn := range conns { for key, conn := range conns {
conn.Lock() conn.Lock()
@ -67,10 +73,8 @@ func cleanConnections() (activePIDs map[int]struct{}) {
conn.Unlock() conn.Unlock()
} }
connsLock.Unlock()
// dns requests // dns requests
dnsConnsLock.Lock()
for _, conn := range dnsConns { for _, conn := range dnsConns {
conn.Lock() conn.Lock()
@ -82,7 +86,6 @@ func cleanConnections() (activePIDs map[int]struct{}) {
conn.Unlock() conn.Unlock()
} }
dnsConnsLock.Unlock()
return nil return nil
}) })

View file

@ -77,9 +77,11 @@ func (s *StorageInterface) processQuery(q *query.Query, it *iterator.Iterator) {
if slashes <= 1 { if slashes <= 1 {
// processes // processes
for _, proc := range process.All() { for _, proc := range process.All() {
proc.Lock()
if q.Matches(proc) { if q.Matches(proc) {
it.Next <- proc it.Next <- proc
} }
proc.Unlock()
} }
} }
@ -87,9 +89,11 @@ func (s *StorageInterface) processQuery(q *query.Query, it *iterator.Iterator) {
// dns scopes only // dns scopes only
dnsConnsLock.RLock() dnsConnsLock.RLock()
for _, dnsConn := range dnsConns { for _, dnsConn := range dnsConns {
dnsConn.Lock()
if q.Matches(dnsConn) { if q.Matches(dnsConn) {
it.Next <- dnsConn it.Next <- dnsConn
} }
dnsConn.Unlock()
} }
dnsConnsLock.RUnlock() dnsConnsLock.RUnlock()
} }
@ -98,9 +102,11 @@ func (s *StorageInterface) processQuery(q *query.Query, it *iterator.Iterator) {
// connections // connections
connsLock.RLock() connsLock.RLock()
for _, conn := range conns { for _, conn := range conns {
conn.Lock()
if q.Matches(conn) { if q.Matches(conn) {
it.Next <- conn it.Next <- conn
} }
conn.Unlock()
} }
connsLock.RUnlock() connsLock.RUnlock()
} }