forked from distribution/distribution
-
Notifications
You must be signed in to change notification settings - Fork 0
registry: stop background goroutines leaked across registry rebuilds #1
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
Open
ranjanprasad96
wants to merge
1
commit into
patch-memory-cache
Choose a base branch
from
leak_fixes
base: patch-memory-cache
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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 hidden or 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
This file contains hidden or 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
This file contains hidden or 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,58 @@ | ||
| package registry | ||
|
|
||
| import ( | ||
| "context" | ||
| "runtime" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/distribution/distribution/v3/configuration" | ||
| "github.com/distribution/distribution/v3/internal/dcontext" | ||
| _ "github.com/distribution/distribution/v3/registry/storage/driver/inmemory" | ||
| ) | ||
|
|
||
| // TestRebuildDoesNotLeakGoroutines simulates a process that repeatedly tears | ||
| // down and recreates a registry (e.g. on credential rotation) and asserts the | ||
| // old registries' background goroutines (upload purger, event broadcaster) do | ||
| // not accumulate. | ||
| func TestRebuildDoesNotLeakGoroutines(t *testing.T) { | ||
| config := &configuration.Configuration{ | ||
| Storage: configuration.Storage{"inmemory": configuration.Parameters{}}, | ||
| } | ||
| config.HTTP.Addr = "127.0.0.1:15999" | ||
| config.Log.Level = "error" | ||
|
|
||
| rebuild := func() { | ||
| reg, err := NewRegistry(dcontext.Background(), config) | ||
| if err != nil { | ||
| t.Fatalf("NewRegistry: %v", err) | ||
| } | ||
| done := make(chan struct{}) | ||
| go func() { _ = reg.ListenAndServe(); close(done) }() | ||
| time.Sleep(100 * time.Millisecond) // let it bind | ||
| if err := reg.Shutdown(context.Background()); err != nil { | ||
| t.Fatalf("Shutdown: %v", err) | ||
| } | ||
| <-done | ||
| } | ||
|
|
||
| rebuild() // warm up process-global state (otel, health) | ||
| runtime.GC() | ||
| before := runtime.NumGoroutine() | ||
|
|
||
| const iterations = 10 | ||
| for i := 0; i < iterations; i++ { | ||
| rebuild() | ||
| } | ||
|
|
||
| var after int | ||
| for i := 0; i < 50; i++ { // allow stragglers to exit | ||
| runtime.GC() | ||
| after = runtime.NumGoroutine() | ||
| if after <= before+2 { | ||
| return | ||
| } | ||
| time.Sleep(100 * time.Millisecond) | ||
| } | ||
| t.Fatalf("goroutines grew from %d to %d over %d rebuilds", before, after, iterations) | ||
| } |
This file contains hidden or 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
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ package tracing | |
|
|
||
| import ( | ||
| "context" | ||
| "sync" | ||
|
|
||
| "github.com/distribution/distribution/v3/internal/dcontext" | ||
| "github.com/distribution/distribution/v3/version" | ||
|
|
@@ -26,9 +27,33 @@ const ( | |
| AttributePrefix = "io.cncf.distribution." | ||
| ) | ||
|
|
||
| var ( | ||
| initMu sync.Mutex | ||
| initDone bool | ||
| ) | ||
|
|
||
| // InitOpenTelemetry initializes OpenTelemetry for the application. This function sets up the | ||
| // necessary components for collecting telemetry data, such as traces. | ||
| // | ||
| // It configures process-global state (the global TracerProvider, error handler, and | ||
| // propagator), so it initializes at most once per process; once it has succeeded, | ||
| // subsequent calls are no-ops (a failed attempt may be retried). This also keeps | ||
| // repeated registry construction (e.g. on config reload) from leaking a | ||
| // BatchSpanProcessor per call. | ||
| func InitOpenTelemetry(ctx context.Context) error { | ||
|
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. would it ever make sense to disable the otel setup entirely (ex: via a |
||
| initMu.Lock() | ||
| defer initMu.Unlock() | ||
| if initDone { | ||
| return nil | ||
| } | ||
| if err := initOpenTelemetry(ctx); err != nil { | ||
| return err | ||
| } | ||
| initDone = true | ||
| return nil | ||
| } | ||
|
|
||
| func initOpenTelemetry(ctx context.Context) error { | ||
| res := resource.NewWithAttributes( | ||
| semconv.SchemaURL, | ||
| semconv.ServiceNameKey.String(serviceName), | ||
|
|
||
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.
instead of setting the cancel func on the struct, shouldn't the caller that creates the app via
NewAppjust pass in actxthat has a cancel, and callcancelwhen they want to stop it?Uh oh!
There was an error while loading. Please reload this page.
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.
oh i see we don't have a
run(ctx context.Context)function... ok nevermind this is fine