Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
etcd: Fix incorrect context usage in session renewal
The context passed into NewSession() was supposed to enforce a timeout on the NewSession() operation which is triggering a Grant() and KeepAlive() instruction. However, the context passed into NewSession() will also be associated with the resulting lease. As per etcd documentation, this will result in: > If the context is canceled before Close() completes, the session's lease will > be abandoned and left to expire instead of being revoked. Because of this, any session renewal triggering a new session would create a session that is immediately closed again due to the context passed into NewSession being cancelled when the controller run ends successfully. This resulted in any renewed session to have an effective lifetime of 10 milliseconds before requiring renewal again. Fixes: #12619 Fixes: 8524fca ("kvstore: Add session renew backoff") Signed-off-by: Thomas Graf <thomas@cilium.io>
- Loading branch information
Showing
3 changed files
with
149 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Copyright 2020 Authors of Cilium | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package contexthelpers | ||
|
||
import ( | ||
"context" | ||
"time" | ||
) | ||
|
||
type SuccessChan chan bool | ||
|
||
// NewConditionalTimeoutContext returns a context which is cancelled when | ||
// success is not reported within the specified timeout | ||
func NewConditionalTimeoutContext(ctx context.Context, timeout time.Duration) (context.Context, context.CancelFunc, SuccessChan) { | ||
ch := make(SuccessChan) | ||
c, cancel := context.WithCancel(ctx) | ||
|
||
go func() { | ||
select { | ||
case success := <-ch: | ||
if !success { | ||
cancel() | ||
return | ||
} | ||
case <-time.After(timeout): | ||
cancel() | ||
} | ||
}() | ||
|
||
return c, cancel, ch | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// Copyright 2020 Authors of Cilium | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
// +build !privileged_tests | ||
|
||
package contexthelpers | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"gopkg.in/check.v1" | ||
) | ||
|
||
func Test(t *testing.T) { | ||
check.TestingT(t) | ||
} | ||
|
||
type ContextSuite struct{} | ||
|
||
var _ = check.Suite(&ContextSuite{}) | ||
|
||
func (b *ContextSuite) TestConditionalTimeoutContext(c *check.C) { | ||
ctx, cancel, ch := NewConditionalTimeoutContext(context.Background(), 10*time.Millisecond) | ||
c.Assert(ctx, check.Not(check.IsNil)) | ||
c.Assert(cancel, check.Not(check.IsNil)) | ||
c.Assert(ch, check.Not(check.IsNil)) | ||
|
||
// validate that the context is being cancelled due to the 10 | ||
// millisecond timeout specified | ||
select { | ||
case <-ctx.Done(): | ||
case <-time.After(time.Second): | ||
c.Errorf("conditional timeout was not triggered") | ||
} | ||
|
||
ctx, cancel, ch = NewConditionalTimeoutContext(context.Background(), 10*time.Millisecond) | ||
// report success via the channel | ||
ch <- true | ||
close(ch) | ||
|
||
// validate that the context is not being cancelled as success has been | ||
// reported | ||
select { | ||
case <-ctx.Done(): | ||
c.Errorf("context cancelled despite reporting success") | ||
case <-time.After(100 * time.Millisecond): | ||
} | ||
|
||
cancel() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters