Skip to content

Fix memory leak in clients/horizonclient/client.go:166 - stream#5747

Merged
urvisavla merged 5 commits intostellar:masterfrom
vxxvvxxv:master
Jul 18, 2025
Merged

Fix memory leak in clients/horizonclient/client.go:166 - stream#5747
urvisavla merged 5 commits intostellar:masterfrom
vxxvvxxv:master

Conversation

@vxxvvxxv
Copy link
Copy Markdown
Contributor

@vxxvvxxv vxxvvxxv commented Jul 9, 2025

PR Checklist

PR Structure

  • This PR has reasonably narrow scope (if not, break it down into smaller PRs).
  • This PR avoids mixing refactoring changes with feature changes (split into two PRs
    otherwise).
  • This PR's title starts with name of package that is most changed in the PR, ex.
    services/friendbot, or all or doc if the changes are broad or impact many
    packages.

Thoroughness

  • This PR adds tests for the most critical parts of the new functionality or fixes.
  • I've updated any docs (developer docs, .md
    files, etc... affected by this change). Take a look in the docs folder for a given service,
    like this one.

Release planning

  • I've reviewed the changes in this PR and if I consider them worthwhile for being mentioned on release notes then I have updated the relevant CHANGELOG.md within the component folder structure. For example, if I changed horizon, then I updated (services/horizon/CHANGELOG.md. I add a new line item describing the change and reference to this PR. If I don't update a CHANGELOG, I acknowledge this PR's change may not be mentioned in future release notes.
  • I've decided if this PR requires a new major/minor version according to
    semver, or if it's mainly a patch change. The PR is targeted at the next
    release branch if it's not a patch change.

What

A memory leak has been detected in the steam function in file clients/horizonclient/client.go:166 (link). This behavior was discovered when attempting to use a non-existent address. The issue is that resp.Close is called after checking the resp.Status.

Why

As a test, I used 4000 non-existent addresses and the allocated memory started to grow rapidly (bytes.growSlice). Below, I provide a comparison through pprof/heap before and after my changes.

Before:

File: ___cursors
Build ID: cdd21ff2deb667b43aacc7fac45d96da8d08bfe2
Type: inuse_space
Time: 2025-07-09 16:56:12 +05
Entering interactive mode (type "help" for commands, "o" for options)
(pprof) top
Showing nodes accounting for 413.45MB, 79.39% of 520.80MB total
Dropped 131 nodes (cum <= 2.60MB)
Showing top 10 nodes out of 98
      flat  flat%   sum%        cum   cum%
  149.23MB 28.65% 28.65%   149.23MB 28.65%  bytes.growSlice
   72.78MB 13.97% 42.63%    72.78MB 13.97%  bufio.NewReaderSize (inline)
   67.26MB 12.92% 55.54%    67.26MB 12.92%  bufio.NewWriterSize (inline)
   28.01MB  5.38% 60.92%    28.01MB  5.38%  runtime.malg
   24.02MB  4.61% 65.53%    24.02MB  4.61%  crypto/internal/fips140/aes/gcm.NewGCMForTLS12
      17MB  3.26% 68.80%   194.06MB 37.26%  net/http.(*Transport).dialConn
   16.50MB  3.17% 71.97%    16.50MB  3.17%  net/textproto.MIMEHeader.Set
   16.01MB  3.07% 75.04%    16.01MB  3.07%  crypto/tls.Client (inline)
   12.56MB  2.41% 77.45%    12.56MB  2.41%  sync.(*Pool).pinSlow
   10.08MB  1.94% 79.39%    10.08MB  1.94%  crypto/internal/fips140/mlkem.NewDecapsulationKey768

After:

File: ___cursors
Build ID: 124c76d253b380d966a33f1b0c4ed666bd15dbb8
Type: inuse_space
Time: 2025-07-09 17:02:16 +05
Entering interactive mode (type "help" for commands, "o" for options)
(pprof) top
Showing nodes accounting for 32.54MB, 76.25% of 42.68MB total
Showing top 10 nodes out of 119
      flat  flat%   sum%        cum   cum%
   12.01MB 28.13% 28.13%    12.01MB 28.13%  runtime.malg
   10.02MB 23.48% 51.61%    10.02MB 23.48%  runtime.allocm
    2.01MB  4.71% 56.32%     2.01MB  4.71%  sync.(*Pool).pinSlow
    1.50MB  3.52% 59.83%     1.50MB  3.52%  github.com/sirupsen/logrus.New
    1.50MB  3.52% 63.35%     1.50MB  3.52%  net.(*Resolver).lookupIPAddr
    1.50MB  3.52% 66.86%        2MB  4.69%  context.withCancel (inline)
       1MB  2.35% 69.22%        1MB  2.35%  bytes.growSlice
       1MB  2.34% 71.56%        1MB  2.34%  main.main.WithFields.func3
       1MB  2.34% 73.90%     1.50MB  3.52%  context.(*cancelCtx).Done
       1MB  2.34% 76.25%        4MB  9.37%  private_repository/***/cursors.(*Service).ListenAndServeStellarPayments

@urvisavla urvisavla merged commit be943c3 into stellar:master Jul 18, 2025
20 checks passed
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.

3 participants