[lxc-devel] [lxd/master] Better lock around event listeners

stgraber on Github lxc-bot at linuxcontainers.org
Mon Feb 29 23:03:23 UTC 2016


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 485 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20160229/a798cabc/attachment.bin>
-------------- next part --------------
From effd45e40ea7a43ad679b6c24b020d8b753ef0c9 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Mon, 29 Feb 2016 18:02:21 -0500
Subject: [PATCH] Better lock around event listeners
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

This should avoid the case where we attempt to read a now deleted
listener and also fix a race condition on listener deletion.

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

diff --git a/lxd/events.go b/lxd/events.go
index 54e830a..95820c1 100644
--- a/lxd/events.go
+++ b/lxd/events.go
@@ -51,6 +51,7 @@ type eventListener struct {
 	active       chan bool
 	id           string
 	msgLock      sync.Mutex
+	wgUsed       sync.WaitGroup
 }
 
 type eventsServe struct {
@@ -109,18 +110,20 @@ func eventSend(eventType string, eventMessage interface{}) error {
 
 	eventsLock.Lock()
 	listeners := eventListeners
-	eventsLock.Unlock()
-
 	for _, listener := range listeners {
 		if !shared.StringInSlice(eventType, listener.messageTypes) {
 			continue
 		}
 
+		listener.wgUsed.Add(1)
 		go func(listener *eventListener, body []byte) {
 			listener.msgLock.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
 
@@ -129,9 +132,12 @@ func eventSend(eventType string, eventMessage interface{}) error {
 				eventsLock.Unlock()
 
 				shared.Debugf("Disconnected events listener: %s", listener.id)
+				return
 			}
+
 		}(listener, body)
 	}
+	eventsLock.Unlock()
 
 	return nil
 }


More information about the lxc-devel mailing list