Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
81a1f12d
Unverified
Commit
81a1f12d
authored
Dec 26, 2018
by
Kubernetes Prow Robot
Committed by
GitHub
Dec 26, 2018
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #70277 from kdada/master
Fix goroutine leak of wait.poller
parents
68451f30
2306eb41
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
60 additions
and
20 deletions
+60
-20
wait.go
staging/src/k8s.io/apimachinery/pkg/util/wait/wait.go
+23
-11
wait_test.go
staging/src/k8s.io/apimachinery/pkg/util/wait/wait_test.go
+37
-9
No files found.
staging/src/k8s.io/apimachinery/pkg/util/wait/wait.go
View file @
81a1f12d
...
...
@@ -359,18 +359,30 @@ type WaitFunc func(done <-chan struct{}) <-chan struct{}
// ErrWaitTimeout will be returned if the channel is closed without fn ever
// returning true.
func
WaitFor
(
wait
WaitFunc
,
fn
ConditionFunc
,
done
<-
chan
struct
{})
error
{
c
:=
wait
(
done
)
stopCh
:=
make
(
chan
struct
{})
once
:=
sync
.
Once
{}
closeCh
:=
func
()
{
once
.
Do
(
func
()
{
close
(
stopCh
)
})
}
defer
closeCh
()
c
:=
wait
(
stopCh
)
for
{
_
,
open
:=
<-
c
ok
,
err
:=
fn
()
if
err
!=
nil
{
return
err
}
if
ok
{
return
nil
}
if
!
open
{
break
select
{
case
_
,
open
:=
<-
c
:
ok
,
err
:=
fn
()
if
err
!=
nil
{
return
err
}
if
ok
{
return
nil
}
if
!
open
{
return
ErrWaitTimeout
}
case
<-
done
:
closeCh
()
}
}
return
ErrWaitTimeout
...
...
staging/src/k8s.io/apimachinery/pkg/util/wait/wait_test.go
View file @
81a1f12d
...
...
@@ -456,18 +456,46 @@ func TestWaitFor(t *testing.T) {
}
}
func
TestWaitForWithDelay
(
t
*
testing
.
T
)
{
done
:=
make
(
chan
struct
{})
defer
close
(
done
)
WaitFor
(
poller
(
time
.
Millisecond
,
ForeverTestTimeout
),
func
()
(
bool
,
error
)
{
func
TestWaitForWithClosedChannel
(
t
*
testing
.
T
)
{
stopCh
:=
make
(
chan
struct
{})
close
(
stopCh
)
start
:=
time
.
Now
()
err
:=
WaitFor
(
poller
(
ForeverTestTimeout
,
ForeverTestTimeout
),
func
()
(
bool
,
error
)
{
return
false
,
nil
},
stopCh
)
duration
:=
time
.
Now
()
.
Sub
(
start
)
// The WaitFor should return immediately, so the duration is close to 0s.
if
duration
>=
ForeverTestTimeout
/
2
{
t
.
Errorf
(
"expected short timeout duration"
)
}
// The interval of the poller is ForeverTestTimeout, so the WaitFor should always return ErrWaitTimeout.
if
err
!=
ErrWaitTimeout
{
t
.
Errorf
(
"expected ErrWaitTimeout from WaitFunc"
)
}
}
// TestWaitForClosesStopCh verifies that after the condition func returns true, WaitFor() closes the stop channel it supplies to the WaitFunc.
func
TestWaitForClosesStopCh
(
t
*
testing
.
T
)
{
stopCh
:=
make
(
chan
struct
{})
defer
close
(
stopCh
)
waitFunc
:=
poller
(
time
.
Millisecond
,
ForeverTestTimeout
)
var
doneCh
<-
chan
struct
{}
WaitFor
(
func
(
done
<-
chan
struct
{})
<-
chan
struct
{}
{
doneCh
=
done
return
waitFunc
(
done
)
},
func
()
(
bool
,
error
)
{
time
.
Sleep
(
10
*
time
.
Millisecond
)
return
true
,
nil
},
done
)
//
If polling goroutine doesn't see the done signal it will leak timers
.
},
stopCh
)
//
The polling goroutine should be closed after WaitFor returning
.
select
{
case
done
<-
struct
{}{}
:
case
<-
time
.
After
(
ForeverTestTimeout
)
:
t
.
Errorf
(
"expected an ack of the done signal."
)
case
_
,
ok
:=
<-
doneCh
:
if
ok
{
t
.
Errorf
(
"expected closed channel after WaitFunc returning"
)
}
default
:
t
.
Errorf
(
"expected an ack of the done signal"
)
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment