-
Notifications
You must be signed in to change notification settings - Fork 4.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix close in use certificate providers after double Close()
method call on wrapper object
#7128
Merged
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
3bf8e1f
Fix close in use certificate providers after double `Close()` method …
bozaro 64fdac1
fixup! Fix close in use certificate providers after double `Close()` …
bozaro 5c3ab05
fixup! Fix close in use certificate providers after double `Close()` …
bozaro 527266f
fixup! Fix close in use certificate providers after double `Close()` …
bozaro a842747
fixup! Fix close in use certificate providers after double `Close()` …
bozaro File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
|
@@ -19,6 +19,7 @@ | |
package certprovider | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"sync" | ||
) | ||
|
@@ -53,6 +54,23 @@ type wrappedProvider struct { | |
store *store | ||
} | ||
|
||
// closedProvider always returns errProviderClosed error. | ||
type closedProvider struct{} | ||
|
||
func (c closedProvider) KeyMaterial(ctx context.Context) (*KeyMaterial, error) { | ||
return nil, errProviderClosed | ||
} | ||
|
||
func (c closedProvider) Close() { | ||
} | ||
|
||
// singleCloseWrappedProvider wraps a provider instance with a reference count to avoid double | ||
// close still in use provider. | ||
type singleCloseWrappedProvider struct { | ||
mu sync.RWMutex | ||
provider Provider | ||
} | ||
|
||
// store is a collection of provider instances, safe for concurrent access. | ||
type store struct { | ||
mu sync.Mutex | ||
|
@@ -75,6 +93,25 @@ func (wp *wrappedProvider) Close() { | |
} | ||
} | ||
|
||
// Close overrides the Close method of the embedded provider to avoid release the | ||
// already released reference. | ||
func (w *singleCloseWrappedProvider) Close() { | ||
w.mu.Lock() | ||
defer w.mu.Unlock() | ||
|
||
w.provider.Close() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we do this call without the lock held? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have replaced the use of |
||
w.provider = closedProvider{} | ||
} | ||
|
||
// KeyMaterial returns the key material sourced by the Provider. | ||
// Callers are expected to use the returned value as read-only. | ||
func (w *singleCloseWrappedProvider) KeyMaterial(ctx context.Context) (*KeyMaterial, error) { | ||
w.mu.RLock() | ||
defer w.mu.RUnlock() | ||
|
||
return w.provider.KeyMaterial(ctx) | ||
} | ||
|
||
// BuildableConfig wraps parsed provider configuration and functionality to | ||
// instantiate provider instances. | ||
type BuildableConfig struct { | ||
|
@@ -112,7 +149,7 @@ func (bc *BuildableConfig) Build(opts BuildOptions) (Provider, error) { | |
} | ||
if wp, ok := provStore.providers[sk]; ok { | ||
wp.refCount++ | ||
return wp, nil | ||
return &singleCloseWrappedProvider{provider: wp}, nil | ||
} | ||
|
||
provider := bc.starter(opts) | ||
|
@@ -126,7 +163,7 @@ func (bc *BuildableConfig) Build(opts BuildOptions) (Provider, error) { | |
store: provStore, | ||
} | ||
provStore.providers[sk] = wp | ||
return wp, nil | ||
return &singleCloseWrappedProvider{provider: wp}, nil | ||
} | ||
|
||
// String returns the provider name and config as a colon separated string. | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit:
... with a reference count to properly handle multiple calls to Close.