fix_: logout deadlock

This commit is contained in:
frank 2024-04-15 21:20:18 +08:00
parent 1211b86151
commit d246699c5e
5 changed files with 12 additions and 13 deletions

2
go.mod
View File

@ -90,7 +90,7 @@ require (
github.com/schollz/peerdiscovery v1.7.0 github.com/schollz/peerdiscovery v1.7.0
github.com/siphiuel/lc-proxy-wrapper v0.0.0-20230516150924-246507cee8c7 github.com/siphiuel/lc-proxy-wrapper v0.0.0-20230516150924-246507cee8c7
github.com/urfave/cli/v2 v2.24.4 github.com/urfave/cli/v2 v2.24.4
github.com/waku-org/go-waku v0.8.1-0.20240322182925-dd81e1d46971 github.com/waku-org/go-waku v0.8.1-0.20240415131212-6d889ca3e2fe
github.com/wk8/go-ordered-map/v2 v2.1.7 github.com/wk8/go-ordered-map/v2 v2.1.7
github.com/yeqown/go-qrcode/v2 v2.2.1 github.com/yeqown/go-qrcode/v2 v2.2.1
github.com/yeqown/go-qrcode/writer/standard v1.2.1 github.com/yeqown/go-qrcode/writer/standard v1.2.1

4
go.sum
View File

@ -2101,8 +2101,8 @@ github.com/waku-org/go-discover v0.0.0-20240129014929-85f2c00b96a3 h1:Kk0KYXZE/u
github.com/waku-org/go-discover v0.0.0-20240129014929-85f2c00b96a3/go.mod h1:eBHgM6T4EG0RZzxpxKy+rGz/6Dw2Nd8DWxS0lm9ESDw= github.com/waku-org/go-discover v0.0.0-20240129014929-85f2c00b96a3/go.mod h1:eBHgM6T4EG0RZzxpxKy+rGz/6Dw2Nd8DWxS0lm9ESDw=
github.com/waku-org/go-libp2p-rendezvous v0.0.0-20230628220917-7b4e5ae4c0e7 h1:0e1h+p84yBp0IN7AqgbZlV7lgFBjm214lgSOE7CeJmE= github.com/waku-org/go-libp2p-rendezvous v0.0.0-20230628220917-7b4e5ae4c0e7 h1:0e1h+p84yBp0IN7AqgbZlV7lgFBjm214lgSOE7CeJmE=
github.com/waku-org/go-libp2p-rendezvous v0.0.0-20230628220917-7b4e5ae4c0e7/go.mod h1:pFvOZ9YTFsW0o5zJW7a0B5tr1owAijRWJctXJ2toL04= github.com/waku-org/go-libp2p-rendezvous v0.0.0-20230628220917-7b4e5ae4c0e7/go.mod h1:pFvOZ9YTFsW0o5zJW7a0B5tr1owAijRWJctXJ2toL04=
github.com/waku-org/go-waku v0.8.1-0.20240322182925-dd81e1d46971 h1:HSR8JmscSmCtpIAzFO5sNZRyYZbO8nw7rGM3QcC9bak= github.com/waku-org/go-waku v0.8.1-0.20240415131212-6d889ca3e2fe h1:rJF7qKODzvWx03iaLbYyjvA62crnCaDqIN661aqCQ8c=
github.com/waku-org/go-waku v0.8.1-0.20240322182925-dd81e1d46971/go.mod h1:RjTvkTrIwpoT1cM9HeQqwa2Q7t7WOkb3hpuB/zuZ6SM= github.com/waku-org/go-waku v0.8.1-0.20240415131212-6d889ca3e2fe/go.mod h1:RjTvkTrIwpoT1cM9HeQqwa2Q7t7WOkb3hpuB/zuZ6SM=
github.com/waku-org/go-zerokit-rln v0.1.14-0.20240102145250-fa738c0bdf59 h1:jisj+OCI6QydLtFq3Pyhu49wl9ytPN7oAHjMfepHDrA= github.com/waku-org/go-zerokit-rln v0.1.14-0.20240102145250-fa738c0bdf59 h1:jisj+OCI6QydLtFq3Pyhu49wl9ytPN7oAHjMfepHDrA=
github.com/waku-org/go-zerokit-rln v0.1.14-0.20240102145250-fa738c0bdf59/go.mod h1:1PdBdPzyTaKt3VnpAHk3zj+r9dXPFOr3IHZP9nFle6E= github.com/waku-org/go-zerokit-rln v0.1.14-0.20240102145250-fa738c0bdf59/go.mod h1:1PdBdPzyTaKt3VnpAHk3zj+r9dXPFOr3IHZP9nFle6E=
github.com/waku-org/go-zerokit-rln-apple v0.0.0-20230916172309-ee0ee61dde2b h1:KgZVhsLkxsj5gb/FfndSCQu6VYwALrCOgYI3poR95yE= github.com/waku-org/go-zerokit-rln-apple v0.0.0-20230916172309-ee0ee61dde2b h1:KgZVhsLkxsj5gb/FfndSCQu6VYwALrCOgYI3poR95yE=

View File

@ -49,8 +49,6 @@ func (sp *CommonDiscoveryService) GetListeningChan() <-chan PeerData {
return sp.channel return sp.channel
} }
func (sp *CommonDiscoveryService) PushToChan(data PeerData) bool { func (sp *CommonDiscoveryService) PushToChan(data PeerData) bool {
sp.RLock()
defer sp.RUnlock()
if err := sp.ErrOnNotRunning(); err != nil { if err := sp.ErrOnNotRunning(); err != nil {
return false return false
} }

View File

@ -4,6 +4,7 @@ import (
"context" "context"
"errors" "errors"
"sync" "sync"
"sync/atomic"
) )
// this is common layout for all the services that require mutex protection and a guarantee that all running goroutines will be finished before stop finishes execution. This guarantee comes from waitGroup all one has to use CommonService.WaitGroup() in the goroutines that should finish by the end of stop function. // this is common layout for all the services that require mutex protection and a guarantee that all running goroutines will be finished before stop finishes execution. This guarantee comes from waitGroup all one has to use CommonService.WaitGroup() in the goroutines that should finish by the end of stop function.
@ -12,7 +13,7 @@ type CommonService struct {
cancel context.CancelFunc cancel context.CancelFunc
ctx context.Context ctx context.Context
wg sync.WaitGroup wg sync.WaitGroup
started bool started atomic.Bool
} }
func NewCommonService() *CommonService { func NewCommonService() *CommonService {
@ -28,13 +29,13 @@ func NewCommonService() *CommonService {
func (sp *CommonService) Start(ctx context.Context, fn func() error) error { func (sp *CommonService) Start(ctx context.Context, fn func() error) error {
sp.Lock() sp.Lock()
defer sp.Unlock() defer sp.Unlock()
if sp.started { if sp.started.Load() {
return ErrAlreadyStarted return ErrAlreadyStarted
} }
sp.started = true sp.started.Store(true)
sp.ctx, sp.cancel = context.WithCancel(ctx) sp.ctx, sp.cancel = context.WithCancel(ctx)
if err := fn(); err != nil { if err := fn(); err != nil {
sp.started = false sp.started.Store(false)
sp.cancel() sp.cancel()
return err return err
} }
@ -48,18 +49,18 @@ var ErrNotStarted = errors.New("not started")
func (sp *CommonService) Stop(fn func()) { func (sp *CommonService) Stop(fn func()) {
sp.Lock() sp.Lock()
defer sp.Unlock() defer sp.Unlock()
if !sp.started { if !sp.started.Load() {
return return
} }
sp.cancel() sp.cancel()
fn() fn()
sp.wg.Wait() sp.wg.Wait()
sp.started = false sp.started.Store(false)
} }
// This is not a mutex protected function, it is up to the caller to use it in a mutex protected context // This is not a mutex protected function, it is up to the caller to use it in a mutex protected context
func (sp *CommonService) ErrOnNotRunning() error { func (sp *CommonService) ErrOnNotRunning() error {
if !sp.started { if !sp.started.Load() {
return ErrNotStarted return ErrNotStarted
} }
return nil return nil

2
vendor/modules.txt vendored
View File

@ -1020,7 +1020,7 @@ github.com/waku-org/go-discover/discover/v5wire
github.com/waku-org/go-libp2p-rendezvous github.com/waku-org/go-libp2p-rendezvous
github.com/waku-org/go-libp2p-rendezvous/db github.com/waku-org/go-libp2p-rendezvous/db
github.com/waku-org/go-libp2p-rendezvous/pb github.com/waku-org/go-libp2p-rendezvous/pb
# github.com/waku-org/go-waku v0.8.1-0.20240322182925-dd81e1d46971 # github.com/waku-org/go-waku v0.8.1-0.20240415131212-6d889ca3e2fe
## explicit; go 1.19 ## explicit; go 1.19
github.com/waku-org/go-waku/logging github.com/waku-org/go-waku/logging
github.com/waku-org/go-waku/waku/persistence github.com/waku-org/go-waku/waku/persistence