Skip to content

Fix goroutine and memory leaks in engine - #6760

Merged
sipsma merged 2 commits into
dagger:mainfrom
sipsma:fix-leaks
Feb 27, 2024
Merged

sipsma merged 2 commits into
dagger:mainfrom
sipsma:fix-leaks

Conversation

@sipsma

@sipsma sipsma commented Feb 27, 2024

Copy link
Copy Markdown
Contributor

Fixes: #6719

See individual commit messages for explanations.

Running the full TestModule suite locally now shows RSS maxing out around 1GB (still high-ish but much more reasonable than 6GB). And most importantly, whereas previously the engine's RSS stayed high after the test suite ended (it stuck around 2-3 GB RSS), now the RSS drops down to reasonable 600MB afterwards, with pprof showing reasonable usage mostly from buildkit's in memory cache metadata:

File: dagger-engine
Type: inuse_space
Time: Feb 27, 2024 at 10:00am (PST)
Entering interactive mode (type "help" for commands, "o" for options)
(pprof) top
Showing nodes accounting for 38.15MB, 58.54% of 65.17MB total
Showing top 10 nodes out of 306
      flat  flat%   sum%        cum   cum%
      11MB 16.88% 16.88%    12.50MB 19.18%  github.com/moby/buildkit/cache/metadata.newStorageItem.func1
    5.50MB  8.44% 25.32%     5.50MB  8.44%  github.com/moby/buildkit/cache.(*cacheRecord).ref
       5MB  7.67% 33.00%        5MB  7.67%  github.com/moby/buildkit/cache/metadata.(*StorageItem).setValue
    3.50MB  5.37% 38.37%     3.50MB  5.37%  github.com/moby/buildkit/cache.(*cacheRecord).layerDigestChain
       3MB  4.60% 42.97%        3MB  4.60%  runtime.malg
    2.55MB  3.92% 46.89%    20.06MB 30.77%  github.com/moby/buildkit/cache.(*cacheManager).getRecord
    2.29MB  3.52% 50.41%     2.29MB  3.52%  github.com/moby/buildkit/solver/pb.(*Op).Marshal
       2MB  3.07% 53.48%     2.50MB  3.84%  github.com/moby/buildkit/cache/metadata.NewValue
    1.75MB  2.69% 56.16%     1.75MB  2.69%  github.com/moby/buildkit/util/tracing/detect.(*TraceRecorder).ExportSpans
    1.55MB  2.37% 58.54%     1.55MB  2.37%  google.golang.org/grpc/internal/transport.newBufWriter

Also, previously there were 20000+ goroutines laying around after the test suite was done. Now there's only 21.

Turns out that the goroutine used to call cancel when the grpc hijack
conn's closeCh is closed is just not necessary at all and can be
removed.

That closeCh is called when .Close is explicitly called, but in that
case there's no need to cancel our context; the context will be
cancelled anyways when the method returns.

Signed-off-by: Erik Sipsma <[email protected]>
The defer used to log "handler done" could block forever in the case
where the HandlerFunc never ends up actually being executed (i.e. some
error or disconnection happens before it even gets invoked). This is
because the chan we waited on was closed from within that HandlerFunc.

The fix here just removes the chan entirely and moves the "handler done"
log to the HandlerFunc itself, which is also much simpler anyways.

Signed-off-by: Erik Sipsma <[email protected]>
@sipsma sipsma added this to the v0.10.1 milestone Feb 27, 2024
@sipsma
sipsma requested review from jedevc and vito February 27, 2024 18:13
@sipsma
sipsma merged commit a0b622a into dagger:main Feb 27, 2024
@jedevc

jedevc commented Feb 28, 2024

Copy link
Copy Markdown
Contributor

Nice 🎉 Didn't get a chance to review this before merging, but LGTM 🎉

Also fixes #6604.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Very high memory usage in dagger engine

3 participants