Skip to content

Commit

Permalink
add some safe goroutines
Browse files Browse the repository at this point in the history
WIP
  • Loading branch information
jesseduffield committed Oct 9, 2020
1 parent ba4c3e5 commit 79e59d5
Show file tree
Hide file tree
Showing 15 changed files with 84 additions and 64 deletions.
5 changes: 3 additions & 2 deletions pkg/commands/oscommands/exec_live_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"unicode/utf8"

"github.com/go-errors/errors"
"github.com/jesseduffield/lazygit/pkg/utils"

"github.com/creack/pty"
)
Expand All @@ -31,14 +32,14 @@ func RunCommandWithOutputLiveWrapper(c *OSCommand, command string, output func(s
return err
}

go func() {
go utils.Safe(func() {
scanner := bufio.NewScanner(ptmx)
scanner.Split(scanWordsWithNewLines)
for scanner.Scan() {
toOutput := strings.Trim(scanner.Text(), " ")
_, _ = ptmx.WriteString(output(toOutput))
}
}()
})

err = cmd.Wait()
ptmx.Close()
Expand Down
4 changes: 2 additions & 2 deletions pkg/commands/oscommands/os.go
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ func (c *OSCommand) PipeCommands(commandStrings ...string) error {

for _, cmd := range cmds {
currentCmd := cmd
go func() {
go utils.Safe(func() {
stderr, err := currentCmd.StderrPipe()
if err != nil {
c.Log.Error(err)
Expand All @@ -432,7 +432,7 @@ func (c *OSCommand) PipeCommands(commandStrings ...string) error {
}

wg.Done()
}()
})
}

wg.Wait()
Expand Down
8 changes: 4 additions & 4 deletions pkg/gui/app_status_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ func (m *statusManager) getStatusString() string {

// WithWaitingStatus wraps a function and shows a waiting status while the function is still executing
func (gui *Gui) WithWaitingStatus(name string, f func() error) error {
go func() {
go utils.Safe(func() {
gui.statusManager.addWaitingStatus(name)

defer func() {
gui.statusManager.removeStatus(name)
}()

go func() {
go utils.Safe(func() {
ticker := time.NewTicker(time.Millisecond * 50)
defer ticker.Stop()
for range ticker.C {
Expand All @@ -67,14 +67,14 @@ func (gui *Gui) WithWaitingStatus(name string, f func() error) error {
}
gui.renderString("appStatus", appStatus)
}
}()
})

if err := f(); err != nil {
gui.g.Update(func(g *gocui.Gui) error {
return gui.surfaceError(err)
})
}
}()
})

return nil
}
8 changes: 4 additions & 4 deletions pkg/gui/branches_panel.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,11 @@ func (gui *Gui) handleGitFetch(g *gocui.Gui, v *gocui.View) error {
if err := gui.createLoaderPanel(v, gui.Tr.FetchWait); err != nil {
return err
}
go func() {
go utils.Safe(func() {
err := gui.fetch(true)
gui.handleCredentialsPopup(err)
_ = gui.refreshSidePanels(refreshOptions{mode: ASYNC})
}()
})
return nil
}

Expand Down Expand Up @@ -385,7 +385,7 @@ func (gui *Gui) handleFastForward(g *gocui.Gui, v *gocui.View) error {
"to": branch.Name,
},
)
go func() {
go utils.Safe(func() {
_ = gui.createLoaderPanel(v, message)

if gui.State.Panels.Branches.SelectedLineIdx == 0 {
Expand All @@ -395,7 +395,7 @@ func (gui *Gui) handleFastForward(g *gocui.Gui, v *gocui.View) error {
gui.handleCredentialsPopup(err)
_ = gui.refreshSidePanels(refreshOptions{mode: ASYNC, scope: []int{BRANCHES}})
}
}()
})
return nil
}

Expand Down
16 changes: 8 additions & 8 deletions pkg/gui/commits_panel.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ func (gui *Gui) handleCommitSelect() error {
state := gui.State.Panels.Commits
if state.SelectedLineIdx > 290 && state.LimitCommits {
state.LimitCommits = false
go func() {
go utils.Safe(func() {
if err := gui.refreshCommitsWithLimit(); err != nil {
_ = gui.surfaceError(err)
}
}()
})
}

gui.escapeLineByLinePanel()
Expand Down Expand Up @@ -60,11 +60,11 @@ func (gui *Gui) handleCommitSelect() error {
func (gui *Gui) refreshReflogCommitsConsideringStartup() {
switch gui.State.StartupStage {
case INITIAL:
go func() {
go utils.Safe(func() {
_ = gui.refreshReflogCommits()
gui.refreshBranches()
gui.State.StartupStage = COMPLETE
}()
})

case COMPLETE:
_ = gui.refreshReflogCommits()
Expand All @@ -78,14 +78,14 @@ func (gui *Gui) refreshCommits() error {
wg := sync.WaitGroup{}
wg.Add(2)

go func() {
go utils.Safe(func() {
gui.refreshReflogCommitsConsideringStartup()

gui.refreshBranches()
wg.Done()
}()
})

go func() {
go utils.Safe(func() {
_ = gui.refreshCommitsWithLimit()
context, ok := gui.Contexts.CommitFiles.Context.GetParentContext()
if ok && context.GetKey() == BRANCH_COMMITS_CONTEXT_KEY {
Expand All @@ -102,7 +102,7 @@ func (gui *Gui) refreshCommits() error {
}
}
wg.Done()
}()
})

wg.Wait()

Expand Down
4 changes: 2 additions & 2 deletions pkg/gui/confirmation_panel.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,14 +189,14 @@ func (gui *Gui) createPopupPanel(opts createPopupPanelOpts) error {
}
confirmationView.Editable = opts.editable
if opts.editable {
go func() {
go utils.Safe(func() {
// TODO: remove this wait (right now if you remove it the EditGotoToEndOfLine method doesn't seem to work)
time.Sleep(time.Millisecond)
gui.g.Update(func(g *gocui.Gui) error {
confirmationView.EditGotoToEndOfLine()
return nil
})
}()
})
}

gui.renderString("confirmation", opts.prompt)
Expand Down
2 changes: 1 addition & 1 deletion pkg/gui/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -727,7 +727,7 @@ func (gui *Gui) mustContextForContextKey(contextKey string) Context {
context, ok := gui.contextForContextKey(contextKey)

if !ok {
panic(fmt.Sprintf("context now found for key %s", contextKey))
panic(fmt.Sprintf("context not found for key %s", contextKey))
}

return context
Expand Down
5 changes: 3 additions & 2 deletions pkg/gui/file_watching.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/fsnotify/fsnotify"
"github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/jesseduffield/lazygit/pkg/utils"
"github.com/sirupsen/logrus"
)

Expand Down Expand Up @@ -120,7 +121,7 @@ func (gui *Gui) watchFilesForChanges() {
if gui.fileWatcher.Disabled {
return
}
go func() {
go utils.Safe(func() {
for {
select {
// watch for events
Expand All @@ -141,5 +142,5 @@ func (gui *Gui) watchFilesForChanges() {
}
}
}
}()
})
}
6 changes: 3 additions & 3 deletions pkg/gui/files_panel.go
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ func (gui *Gui) pullFiles(opts PullFilesOptions) error {

mode := gui.Config.GetUserConfig().Git.Pull.Mode

go gui.pullWithMode(mode, opts)
go utils.Safe(func() { gui.pullWithMode(mode, opts) })

return nil
}
Expand Down Expand Up @@ -551,7 +551,7 @@ func (gui *Gui) pushWithForceFlag(v *gocui.View, force bool, upstream string, ar
if err := gui.createLoaderPanel(v, gui.Tr.PushWait); err != nil {
return err
}
go func() {
go utils.Safe(func() {
branchName := gui.getCheckedOutBranch().Name
err := gui.GitCommand.Push(branchName, force, upstream, args, gui.promptUserForCredential)
if err != nil && !force && strings.Contains(err.Error(), "Updates were rejected") {
Expand All @@ -571,7 +571,7 @@ func (gui *Gui) pushWithForceFlag(v *gocui.View, force bool, upstream string, ar
}
gui.handleCredentialsPopup(err)
_ = gui.refreshSidePanels(refreshOptions{mode: ASYNC})
}()
})
return nil
}

Expand Down
22 changes: 11 additions & 11 deletions pkg/gui/gui.go
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ func (gui *Gui) Run() error {
defer g.Close()

if recordEvents {
go gui.recordEvents()
go utils.Safe(gui.recordEvents)
}

if gui.State.Modes.Filtering.Active() {
Expand Down Expand Up @@ -481,10 +481,10 @@ func (gui *Gui) Run() error {

gui.waitForIntro.Add(1)
if gui.Config.GetUserConfig().Git.AutoFetch {
go gui.startBackgroundFetch()
go utils.Safe(gui.startBackgroundFetch)
}

gui.goEvery(time.Second*10, gui.stopChan, gui.refreshFilesAndSubmodules)
gui.goEvery(time.Millisecond*50, gui.stopChan, gui.refreshFilesAndSubmodules)

g.SetManager(gocui.ManagerFunc(gui.layout), gocui.ManagerFunc(gui.getFocusLayout()))

Expand All @@ -499,7 +499,7 @@ func (gui *Gui) Run() error {
// otherwise it handles the error, possibly by quitting the application
func (gui *Gui) RunWithSubprocesses() error {
gui.StartTime = time.Now()
go gui.replayRecordedEvents()
go utils.Safe(gui.replayRecordedEvents)

for {
gui.stopChan = make(chan struct{})
Expand Down Expand Up @@ -584,18 +584,18 @@ func (gui *Gui) showInitialPopups(tasks []func(chan struct{}) error) {
gui.waitForIntro.Add(len(tasks))
done := make(chan struct{})

go func() {
go utils.Safe(func() {
for _, task := range tasks {
go func() {
go utils.Safe(func() {
if err := task(done); err != nil {
_ = gui.surfaceError(err)
}
}()
})

<-done
gui.waitForIntro.Done()
}
}()
})
}

func (gui *Gui) showIntroPopupMessage(done chan struct{}) error {
Expand All @@ -614,7 +614,7 @@ func (gui *Gui) showIntroPopupMessage(done chan struct{}) error {
}

func (gui *Gui) goEvery(interval time.Duration, stop chan struct{}, function func() error) {
go func() {
go utils.Safe(func() {
ticker := time.NewTicker(interval)
defer ticker.Stop()
for {
Expand All @@ -625,7 +625,7 @@ func (gui *Gui) goEvery(interval time.Duration, stop chan struct{}, function fun
return
}
}
}()
})
}

func (gui *Gui) startBackgroundFetch() {
Expand All @@ -641,7 +641,7 @@ func (gui *Gui) startBackgroundFetch() {
prompt: gui.Tr.NoAutomaticGitFetchBody,
})
} else {
gui.goEvery(time.Second*60, gui.stopChan, func() error {
gui.goEvery(time.Millisecond*50, gui.stopChan, func() error {
err := gui.fetch(false)
return err
})
Expand Down
5 changes: 3 additions & 2 deletions pkg/gui/recording.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"time"

"github.com/jesseduffield/gocui"
"github.com/jesseduffield/lazygit/pkg/utils"
)

func recordingEvents() bool {
Expand All @@ -28,10 +29,10 @@ func (gui *Gui) replayRecordedEvents() {
return
}

go func() {
go utils.Safe(func() {
time.Sleep(time.Second * 20)
log.Fatal("20 seconds is up, lazygit recording took too long to complete")
}()
})

events, err := gui.loadRecordedEvents()
if err != nil {
Expand Down
10 changes: 5 additions & 5 deletions pkg/gui/view_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func (gui *Gui) refreshSidePanels(options refreshOptions) error {
wg.Add(1)
func() {
if options.mode == ASYNC {
go gui.refreshCommits()
go utils.Safe(func() { gui.refreshCommits() })
} else {
gui.refreshCommits()
}
Expand All @@ -122,7 +122,7 @@ func (gui *Gui) refreshSidePanels(options refreshOptions) error {
wg.Add(1)
func() {
if options.mode == ASYNC {
go gui.refreshFilesAndSubmodules()
go utils.Safe(func() { gui.refreshFilesAndSubmodules() })
} else {
gui.refreshFilesAndSubmodules()
}
Expand All @@ -134,7 +134,7 @@ func (gui *Gui) refreshSidePanels(options refreshOptions) error {
wg.Add(1)
func() {
if options.mode == ASYNC {
go gui.refreshStashEntries()
go utils.Safe(func() { gui.refreshStashEntries() })
} else {
gui.refreshStashEntries()
}
Expand All @@ -146,7 +146,7 @@ func (gui *Gui) refreshSidePanels(options refreshOptions) error {
wg.Add(1)
func() {
if options.mode == ASYNC {
go gui.refreshTags()
go utils.Safe(func() { gui.refreshTags() })
} else {
gui.refreshTags()
}
Expand All @@ -158,7 +158,7 @@ func (gui *Gui) refreshSidePanels(options refreshOptions) error {
wg.Add(1)
func() {
if options.mode == ASYNC {
go gui.refreshRemotes()
go utils.Safe(func() { gui.refreshRemotes() })
} else {
gui.refreshRemotes()
}
Expand Down
Loading

0 comments on commit 79e59d5

Please sign in to comment.