From 79e1dc9c9ad159c475476174dee0fb535913f65d Mon Sep 17 00:00:00 2001 From: Jannis Mattheis Date: Sun, 18 Nov 2018 12:32:24 +0100 Subject: [PATCH] Prevent deadlock on stream.Close() GR = goroutine [GR#1] http server gets closed [GR#2] client.NotifyClose() will be executed [GR#2] client.once.Do will be executed (lock's client.once.m) [GR#1] stream.Close will be executed (lock's stream.lock) [GR#1] client.Close will be executed (waits for client.once.m) [GR#2] stream.remove will be executed (waits for stream.lock) GR#1 holds lock stream.lock and waits for client.once.m GR#2 holds lock client.once.m and waits for stream.lock We prevent the deadlock with releasing the client.once.m lock earlier. --- api/stream/client.go | 3 +-- api/stream/once.go | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 api/stream/once.go diff --git a/api/stream/client.go b/api/stream/client.go index f2ff896..dc8d222 100644 --- a/api/stream/client.go +++ b/api/stream/client.go @@ -1,7 +1,6 @@ package stream import ( - "sync" "time" "github.com/gorilla/websocket" @@ -26,7 +25,7 @@ type client struct { write chan *model.Message userID uint token string - once sync.Once + once once } func newClient(conn *websocket.Conn, userID uint, token string, onClose func(*client)) *client { diff --git a/api/stream/once.go b/api/stream/once.go new file mode 100644 index 0000000..83e0a2d --- /dev/null +++ b/api/stream/once.go @@ -0,0 +1,36 @@ +// Copyright 2009 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package stream + +import ( + "sync/atomic" + "sync" +) + +// Modified version of sync.Once (https://github.com/golang/go/blob/master/src/sync/once.go) +// This version unlocks the mutex early and therefore doesn't hold the lock while executing func f(). +type once struct { + m sync.Mutex + done uint32 +} + +func (o *once) Do(f func()) { + if atomic.LoadUint32(&o.done) == 1 { + return + } + if o.mayExecute() { + f() + } +} + +func (o *once) mayExecute() bool { + o.m.Lock() + defer o.m.Unlock() + if o.done == 0 { + atomic.StoreUint32(&o.done, 1) + return true + } + return false +} \ No newline at end of file