Skip to content
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: create a new CID from a clean slice to avoid holding on to a larger memory range #150

Merged
merged 2 commits into from
Nov 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions blocks/randompruner.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,9 +289,14 @@ func (pruner *RandomPruner) prune(ctx context.Context, bytesToPrune uint64) erro
return nil
}

func (pruner *RandomPruner) updatePin(cid cid.Cid) {
func (pruner *RandomPruner) updatePin(pin cid.Cid) {
// clone the Cid to ensure we don't hang on to memory that may come from the network stack
// where the Cid was decoded as part of a larger message
emptyPinClone := make([]byte, 0, len(pin.Bytes()))
pinClone := append(emptyPinClone, pin.Bytes()...)
pin = cid.MustParse(pinClone)
pruner.pinsLk.Lock()
pruner.pins[cid] = time.Now()
pruner.pins[pin] = time.Now()
pruner.pinsLk.Unlock()
}

Expand All @@ -300,6 +305,8 @@ func (pruner *RandomPruner) cleanPins(ctx context.Context) error {
pruner.pinsLk.Lock()
defer pruner.pinsLk.Unlock()

log.Debugf("Considering %d pins for possible clearing", len(pruner.pins))

now := time.Now()

for cid, lastUse := range pruner.pins {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ require (
github.com/ipfs/go-bitswap v0.6.0
github.com/ipfs/go-block-format v0.0.3
github.com/ipfs/go-blockservice v0.3.0
github.com/ipfs/go-cid v0.2.0
github.com/ipfs/go-cid v0.3.2
github.com/ipfs/go-datastore v0.5.1
github.com/ipfs/go-ds-flatfs v0.5.1
github.com/ipfs/go-ds-leveldb v0.5.0
Expand Down
3 changes: 2 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -782,8 +782,9 @@ github.com/ipfs/go-cid v0.0.6-0.20200501230655-7c82f3b81c00/go.mod h1:plgt+Y5MnO
github.com/ipfs/go-cid v0.0.6/go.mod h1:6Ux9z5e+HpkQdckYoX1PG/6xqKspzlEIR5SDmgqgC/I=
github.com/ipfs/go-cid v0.0.7/go.mod h1:6Ux9z5e+HpkQdckYoX1PG/6xqKspzlEIR5SDmgqgC/I=
github.com/ipfs/go-cid v0.1.0/go.mod h1:rH5/Xv83Rfy8Rw6xG+id3DYAMUVmem1MowoKwdXmN2o=
github.com/ipfs/go-cid v0.2.0 h1:01JTiihFq9en9Vz0lc0VDWvZe/uBonGpzo4THP0vcQ0=
github.com/ipfs/go-cid v0.2.0/go.mod h1:P+HXFDF4CVhaVayiEb4wkAy7zBHxBwsJyt0Y5U6MLro=
github.com/ipfs/go-cid v0.3.2 h1:OGgOd+JCFM+y1DjWPmVH+2/4POtpDzwcr7VgnB7mZXc=
github.com/ipfs/go-cid v0.3.2/go.mod h1:gQ8pKqT/sUxGY+tIwy1RPpAojYu7jAyCp5Tz1svoupw=
github.com/ipfs/go-cidutil v0.0.2/go.mod h1:ewllrvrxG6AMYStla3GD7Cqn+XYSLqjK0vc+086tB6s=
github.com/ipfs/go-cidutil v0.1.0 h1:RW5hO7Vcf16dplUU60Hs0AKDkQAVPVplr7lk97CFL+Q=
github.com/ipfs/go-cidutil v0.1.0/go.mod h1:e7OEVBMIv9JaOxt9zaGEmAoSlXW9jdFZ5lP/0PwcfpA=
Expand Down