[lxc-devel] [lxd/master] Rework event locking

stgraber on Github lxc-bot at linuxcontainers.org
Tue Mar 8 23:39:57 UTC 2016


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 522 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20160308/5ef4fc5b/attachment.bin>
-------------- next part --------------
From d05133ec1a28eeaf4d9e568ef41adca501c43a12 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Tue, 8 Mar 2016 18:39:00 -0500
Subject: [PATCH] Rework event locking
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

The waitgroups were leading to more panics due to the code occasionally
adding to the group before all waits were done.

Instead, lets just use good old mutexes.

Signed-off-by: Stéphane Graber <stgraber at ubuntu.com>
---
 lxd/events.go | 19 +++++++++----------
 1 file changed, 9 insertions(+), 10 deletions(-)

diff --git a/lxd/events.go b/lxd/events.go
index 95820c1..082651d 100644
--- a/lxd/events.go
+++ b/lxd/events.go
@@ -50,8 +50,7 @@ type eventListener struct {
 	messageTypes []string
 	active       chan bool
 	id           string
-	msgLock      sync.Mutex
-	wgUsed       sync.WaitGroup
+	lock         sync.Mutex
 }
 
 type eventsServe struct {
@@ -115,26 +114,26 @@ func eventSend(eventType string, eventMessage interface{}) error {
 			continue
 		}
 
-		listener.wgUsed.Add(1)
 		go func(listener *eventListener, body []byte) {
-			listener.msgLock.Lock()
+			if listener == nil {
+				return
+			}
+
+			listener.lock.Lock()
 			err = listener.connection.WriteMessage(websocket.TextMessage, body)
-			listener.msgLock.Unlock()
-			listener.wgUsed.Done()
 
 			if err != nil {
-				listener.wgUsed.Wait()
 				listener.connection.Close()
 				listener.active <- false
 
 				eventsLock.Lock()
 				delete(eventListeners, listener.id)
-				eventsLock.Unlock()
-
 				shared.Debugf("Disconnected events listener: %s", listener.id)
-				return
+				eventsLock.Unlock()
 			}
 
+			listener.lock.Unlock()
+			return
 		}(listener, body)
 	}
 	eventsLock.Unlock()


More information about the lxc-devel mailing list