From d92b20438ef1f98ade7924fa0edcc67cae9a4e54 Mon Sep 17 00:00:00 2001 From: gVisor bot Date: Fri, 15 Jun 2018 00:49:52 +0800 Subject: [PATCH] Fixed: race condition in update config --- tunnel/tunnel.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tunnel/tunnel.go b/tunnel/tunnel.go index c1edd9a2..f68096b2 100644 --- a/tunnel/tunnel.go +++ b/tunnel/tunnel.go @@ -24,6 +24,7 @@ type Tunnel struct { proxys map[string]C.Proxy observable *observable.Observable logCh chan interface{} + configLock *sync.RWMutex } func (t *Tunnel) Add(req C.ServerAdapter) { @@ -31,6 +32,9 @@ func (t *Tunnel) Add(req C.ServerAdapter) { } func (t *Tunnel) UpdateConfig() (err error) { + t.configLock.Lock() + defer t.configLock.Unlock() + cfg, err := C.GetConfig() if err != nil { return @@ -113,6 +117,9 @@ func (t *Tunnel) handleConn(localConn C.ServerAdapter) { } func (t *Tunnel) match(addr *C.Addr) C.Proxy { + t.configLock.RLock() + defer t.configLock.RUnlock() + for _, rule := range t.rules { if rule.IsMatch(addr) { a, ok := t.proxys[rule.Adapter()] @@ -134,6 +141,7 @@ func newTunnel() *Tunnel { proxys: make(map[string]C.Proxy), observable: observable.NewObservable(logCh), logCh: logCh, + configLock: &sync.RWMutex{}, } go tunnel.process() go tunnel.subscribeLogs()