8000 Revert "Cleanly close agent goroutines" · pion/ice@89093bb · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Commit 89093bb

Browse files
committed
Revert "Cleanly close agent goroutines"
This reverts commit d8341e7.
1 parent 11845a7 commit 89093bb

File tree

4 files changed

+6
-60
lines changed

4 files changed

+6
-60
lines changed

agent.go

+4-8
Original file line numberDiff line numberDiff line change
@@ -220,9 +220,9 @@ func NewAgent(config *AgentConfig) (*Agent, error) { //nolint:gocognit
220220

221221
userBindingRequestHandler: config.BindingRequestHandler,
222222
}
223-
a.connectionStateNotifier = &handlerNotifier{connectionStateFunc: a.onConnectionStateChange, done: make(chan struct{})}
224-
a.candidateNotifier = &handlerNotifier{candidateFunc: a.onCandidate, done: make(chan struct{})}
225-
a.selectedCandidatePairNotifier = &handlerNotifier{candidatePairFunc: a.onSelectedCandidatePairChange, done: make(chan struct{})}
223+
a.connectionStateNotifier = &handlerNotifier{connectionStateFunc: a.onConnectionStateChange}
224+
a.candidateNotifier = &handlerNotifier{candidateFunc: a.onCandidate}
225+
a.selectedCandidatePairNotifier = &handlerNotifier{candidatePairFunc: a.onSelectedCandidatePairChange}
226226

227227
if a.net == nil {
228228
a.net, err = stdnet.NewNet()
@@ -849,11 +849,7 @@ func (a *Agent) removeUfragFromMux() {
849849

850850
// Close cleans up the Agent
851851
func (a *Agent) Close() error {
852-
err := a.loop.Close()
853-
a.connectionStateNotifier.Close()
854-
a.candidateNotifier.Close()
855-
a.selectedCandidatePairNotifier.Close()
856-
return err
852+
return a.loop.Close()
857853
}
858854

859855
// Remove all candidates. This closes any listening sockets

agent_handlers.go

+1-44
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,7 @@ func (a *Agent) onConnectionStateChange(s ConnectionState) {
4545

4646
type handlerNotifier struct {
4747
sync.Mutex
48-
running bool
49-
notifiers sync.WaitGroup
48+
running bool
5049

5150
connectionStates []ConnectionState
5251
connectionStateFunc func(ConnectionState)
@@ -56,38 +55,13 @@ type handlerNotifier struct {
5655

5756
selectedCandidatePairs []*CandidatePair
5857
candidatePairFunc func(*CandidatePair)
59-
60-
// State for closing
61-
done chan struct{}
62-
}
63-
64-
func (h *handlerNotifier) Close() {
65-
h.Lock()
66-
67-
select {
68-
case <-h.done:
69-
h.Unlock()
70-
return
71-
default:
72-
}
73-
close(h.done)
74-
h.Unlock()
75-
76-
h.notifiers.Wait()
7758
}
7859

7960
func (h *handlerNotifier) EnqueueConnectionState(s ConnectionState) {
8061
h.Lock()
8162
defer h.Unlock()
8263

83-
select {
84-
case <-h.done:
85-
return
86-
default:
87-
}
88-
8964
notify := func() {
90-
defer h.notifiers.Done()
9165
for {
9266
h.Lock()
9367
if len(h.connectionStates) == 0 {
@@ -105,7 +79,6 @@ func (h *handlerNotifier) EnqueueConnectionState(s ConnectionState) {
10579
h.connectionStates = append(h.connectionStates, s)
10680
if !h.running {
10781
h.running = true
108-
h.notifiers.Add(1)
10982
go notify()
11083
}
11184
}
@@ -114,14 +87,7 @@ func (h *handlerNotifier) EnqueueCandidate(c Candidate) {
11487
h.Lock()
11588
defer h.Unlock()
11689

117-
select {
118-
case <-h.done:
119-
return
120-
default:
121-
}
122-
12390
notify := func() {
124-
defer h.notifiers.Done()
12591
for {
12692
h.Lock()
12793
if len(h.candidates) == 0 {
@@ -139,7 +105,6 @@ func (h *handlerNotifier) EnqueueCandidate(c Candidate) {
139105
h.candidates = append(h.candidates, c)
140106
if !h.running {
141107
h.running = true
142-
h.notifiers.Add(1)
143108
go notify()
144109
}
145110
}
@@ -148,14 +113,7 @@ func (h *handlerNotifier) EnqueueSelectedCandidatePair(p *CandidatePair) {
148113
h.Lock()
149114
defer h.Unlock()
150115

151-
select {
152-
case <-h.done:
153-
return
154-
default:
155-
}
156-
157116
notify := func() {
158-
defer h.notifiers.Done()
159117
for {
160118
h.Lock()
161119
if len(h.selectedCandidatePairs) == 0 {
@@ -173,7 +131,6 @@ func (h *handlerNotifier) EnqueueSelectedCandidatePair(p *CandidatePair) {
173131
h.selectedCandidatePairs = append(h.selectedCandidatePairs, p)
174132
if !h.running {
175133
h.running = true
176-
h.notifiers.Add(1)
177134
go notify()
178135
}
179136
}

agent_handlers_test.go

-4
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ func TestConnectionStateNotifier(t *testing.T) {
1919
connectionStateFunc: func(_ ConnectionState) {
2020
updates <- struct{}{}
2121
},
22-
done: make(chan struct{}),
2322
}
2423
// Enqueue all updates upfront to ensure that it
2524
// doesn't block
@@ -39,7 +38,6 @@ func TestConnectionStateNotifier(t *testing.T) {
3938
close(done)
4039
}()
4140
<-done
42-
c.Close()
4341
})
4442
t.Run("TestUpdateOrdering", func(t *testing.T) {
4543
defer test.CheckRoutines(t)()
@@ -48,7 +46,6 @@ func TestConnectionStateNotifier(t *test 10000 ing.T) {
4846
connectionStateFunc: func(cs ConnectionState) {
4947
updates <- cs
5048
},
51-
done: make(chan struct{}),
5249
}
5350
done := make(chan struct{})
5451
go func() {
@@ -69,6 +66,5 @@ func TestConnectionStateNotifier(t *testing.T) {
6966
c.EnqueueConnectionState(ConnectionState(i))
7067
}
7168
<-done
72-
c.Close()
7369
})
7470
}

agent_test.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -1379,12 +1379,11 @@ func TestCloseInConnectionStateCallback(t *testing.T) {
13791379

13801380
isClosed := make(chan interface{})
13811381
isConnected := make(chan interface{})
1382-
connectionStateConnectedSeen := make(chan interface{})
13831382
err = aAgent.OnConnectionStateChange(func(c ConnectionState) {
13841383
switch c {
13851384
case ConnectionStateConnected:
13861385
<-isConnected
1387-
close(connectionStateConnectedSeen)
1386+
require.NoError(t, aAgent.Close())
13881387
case ConnectionStateClosed:
13891388
close(isClosed)
13901389
default:
@@ -1394,8 +1393,6 @@ func TestCloseInConnectionStateCallback(t *testing.T) {
13941393

13951394
connect(aAgent, bAgent)
13961395
close(isConnected)
1397-
<-connectionStateConnectedSeen
1398-
require.NoError(t, aAgent.Close())
13991396

14001397
<-isClosed
14011398
require.NoError(t, bAgent.Close())

0 commit comments

Comments
 (0)
0