Skip to content

Commit

Permalink
chore: bump pubgrub
Browse files Browse the repository at this point in the history
fix: fix race conditions
  • Loading branch information
Vilsol committed Dec 6, 2023
1 parent 9f7c801 commit 9d6039a
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 6 deletions.
7 changes: 7 additions & 0 deletions cli/installations.go
Original file line number Diff line number Diff line change
Expand Up @@ -548,11 +548,14 @@ func downloadAndExtractMod(modReference string, version string, link string, has

var extractUpdates chan utils.GenericProgress

var wg sync.WaitGroup
if updates != nil {
// Forward the inner updates as InstallUpdates
extractUpdates = make(chan utils.GenericProgress)

wg.Add(1)
go func() {
defer wg.Done()
for up := range extractUpdates {
select {
case updates <- InstallUpdate{
Expand Down Expand Up @@ -585,8 +588,12 @@ func downloadAndExtractMod(modReference string, version string, link string, has
}:
default:
}

close(extractUpdates)
}

wg.Wait()

return nil
}

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ require (
github.com/charmbracelet/glamour v0.5.0
github.com/charmbracelet/lipgloss v0.6.0
github.com/jlaffaye/ftp v0.1.0
github.com/mircearoata/pubgrub-go v0.3.2
github.com/mircearoata/pubgrub-go v0.3.3
github.com/muesli/reflow v0.3.0
github.com/pkg/errors v0.9.1
github.com/pterm/pterm v0.12.67
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -269,8 +269,8 @@ github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh
github.com/microcosm-cc/bluemonday v1.0.17/go.mod h1:Z0r70sCuXHig8YpBzCc5eGHAap2K7e/u082ZUpDRRqM=
github.com/microcosm-cc/bluemonday v1.0.21 h1:dNH3e4PSyE4vNX+KlRGHT5KrSvjeUkoNPwEORjffHJg=
github.com/microcosm-cc/bluemonday v1.0.21/go.mod h1:ytNkv4RrDrLJ2pqlsSI46O6IVXmZOBBD4SaJyDwwTkM=
github.com/mircearoata/pubgrub-go v0.3.2 h1:AeC2bvHebii6YrfyN+AST06WiQUENKQkREA7Xoi4/HY=
github.com/mircearoata/pubgrub-go v0.3.2/go.mod h1:9oWL9ZXdjFYvnGl95qiM1dTciFNx1MN8fUnG3SUwDi8=
github.com/mircearoata/pubgrub-go v0.3.3 h1:XGwL8Xh5GX+mbnvWItbM/lVJxAq3NZtfUtbJ/hUf2ig=
github.com/mircearoata/pubgrub-go v0.3.3/go.mod h1:9oWL9ZXdjFYvnGl95qiM1dTciFNx1MN8fUnG3SUwDi8=
github.com/mitchellh/mapstructure v1.2.3/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
Expand Down
8 changes: 5 additions & 3 deletions utils/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os"
"path/filepath"
"sync"
"sync/atomic"

"github.com/pkg/errors"

Expand Down Expand Up @@ -101,12 +102,13 @@ func ExtractMod(f io.ReaderAt, size int64, location string, hash string, updates
}

totalExtracted := int64(0)
totalExtractedPtr := &totalExtracted

channelUsers := sync.WaitGroup{}

if updates != nil {
defer func() {
channelUsers.Wait()
close(updates)
}()
}

Expand All @@ -126,7 +128,7 @@ func ExtractMod(f io.ReaderAt, size int64, location string, hash string, updates
defer channelUsers.Done()
for fileUpdate := range fileUpdates {
updates <- GenericProgress{
Completed: totalExtracted + fileUpdate.Completed,
Completed: atomic.LoadInt64(totalExtractedPtr) + fileUpdate.Completed,
Total: totalSize,
}
}
Expand All @@ -137,7 +139,7 @@ func ExtractMod(f io.ReaderAt, size int64, location string, hash string, updates
return err
}

totalExtracted += int64(file.UncompressedSize64)
atomic.AddInt64(totalExtractedPtr, int64(file.UncompressedSize64))
}
}

Expand Down

0 comments on commit 9d6039a

Please sign in to comment.