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
206ecb4a
Unverified
Commit
206ecb4a
authored
Mar 22, 2019
by
Kubernetes Prow Robot
Committed by
GitHub
Mar 22, 2019
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #71326 from shomron/issue-71277-polluntil-leak
Fix goroutine leak in pkg/util/wait PollUntil()
parents
3dfe9072
0869e636
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
55 additions
and
2 deletions
+55
-2
wait.go
staging/src/k8s.io/apimachinery/pkg/util/wait/wait.go
+25
-2
wait_test.go
staging/src/k8s.io/apimachinery/pkg/util/wait/wait_test.go
+30
-0
No files found.
staging/src/k8s.io/apimachinery/pkg/util/wait/wait.go
View file @
206ecb4a
...
...
@@ -250,6 +250,25 @@ func (b *Backoff) Step() time.Duration {
return
duration
}
// contextForChannel derives a child context from a parent channel.
//
// The derived context's Done channel is closed when the returned cancel function
// is called or when the parent channel is closed, whichever happens first.
//
// Note the caller must *always* call the CancelFunc, otherwise resources may be leaked.
func
contextForChannel
(
parentCh
<-
chan
struct
{})
(
context
.
Context
,
context
.
CancelFunc
)
{
ctx
,
cancel
:=
context
.
WithCancel
(
context
.
Background
())
go
func
()
{
select
{
case
<-
parentCh
:
cancel
()
case
<-
ctx
.
Done
()
:
}
}()
return
ctx
,
cancel
}
// ExponentialBackoff repeats a condition check with exponential backoff.
//
// It checks the condition up to Steps times, increasing the wait by multiplying
...
...
@@ -353,7 +372,9 @@ func PollImmediateInfinite(interval time.Duration, condition ConditionFunc) erro
// PollUntil always waits interval before the first run of 'condition'.
// 'condition' will always be invoked at least once.
func
PollUntil
(
interval
time
.
Duration
,
condition
ConditionFunc
,
stopCh
<-
chan
struct
{})
error
{
return
WaitFor
(
poller
(
interval
,
0
),
condition
,
stopCh
)
ctx
,
cancel
:=
contextForChannel
(
stopCh
)
defer
cancel
()
return
WaitFor
(
poller
(
interval
,
0
),
condition
,
ctx
.
Done
())
}
// PollImmediateUntil tries a condition func until it returns true, an error or stopCh is closed.
...
...
@@ -422,7 +443,9 @@ func WaitFor(wait WaitFunc, fn ConditionFunc, done <-chan struct{}) error {
// timeout has elapsed and then closes the channel.
//
// Over very short intervals you may receive no ticks before the channel is
// closed. A timeout of 0 is interpreted as an infinity.
// closed. A timeout of 0 is interpreted as an infinity, and in such a case
// it would be the caller's responsibility to close the done channel.
// Failure to do so would result in a leaked goroutine.
//
// Output ticks are not buffered. If the channel is not ready to receive an
// item, the tick is skipped.
...
...
staging/src/k8s.io/apimachinery/pkg/util/wait/wait_test.go
View file @
206ecb4a
...
...
@@ -664,3 +664,33 @@ func TestBackoff_Step(t *testing.T) {
}
}
}
func
TestContextForChannel
(
t
*
testing
.
T
)
{
var
wg
sync
.
WaitGroup
parentCh
:=
make
(
chan
struct
{})
done
:=
make
(
chan
struct
{})
for
i
:=
0
;
i
<
3
;
i
++
{
wg
.
Add
(
1
)
go
func
()
{
defer
wg
.
Done
()
ctx
,
cancel
:=
contextForChannel
(
parentCh
)
defer
cancel
()
<-
ctx
.
Done
()
}()
}
go
func
()
{
wg
.
Wait
()
close
(
done
)
}()
// Closing parent channel should cancel all children contexts
close
(
parentCh
)
select
{
case
<-
done
:
case
<-
time
.
After
(
ForeverTestTimeout
)
:
t
.
Errorf
(
"unexepcted timeout waiting for parent to cancel child contexts"
)
}
}
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