panic: runtime error: index out of range [0] with length 0 in StatusManager.GetStatusString

Open Beginner friendly
#5,676 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
76/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Quiet
Tech stack
go
Domain
cli

Research direction

Start in pkg/gui/status/status_manager.go, reading GetStatusString and HasStatus alongside addStatus and removeStatus to understand the mutex usage. Check the call site in pkg/gui/controllers/helpers/app_status_helper.go. Done means both read methods hold the mutex for the complete status read so concurrent removal cannot cause the reported index-out-of-range panic.

Written by the indexing model from the issue text.

Description

StatusManager.GetStatusString (and HasStatus) read self.statuses without holding the mutex, while addStatus and removeStatus both do hold it. This creates a TOCTOU race: the len == 0 check passes, another goroutine calls removeStatus between the check and the self.statuses[0] access, and the index-out-of-range panic results.

Stack trace:

panic: runtime error: index out of range [0] with length 0

goroutine 2424671 [running]:
github.com/jesseduffield/lazygit/pkg/gui/status.(*StatusManager).GetStatusString(...)
        pkg/gui/status/status_manager.go:76
github.com/jesseduffield/lazygit/pkg/gui/controllers/helpers.(*AppStatusHelper).renderAppStatus.func1(...)
        pkg/gui/controllers/helpers/app_status_helper.go:106

Fix: lock the mutex for the full read in both GetStatusString and HasStatus:

func (self *StatusManager) GetStatusString(userConfig *config.UserConfig) (string, gocui.Attribute) {
    self.mutex.Lock()
    defer self.mutex.Unlock()

    if len(self.statuses) == 0 {
        return "", gocui.ColorDefault
    }
    // ...
}

func (self *StatusManager) HasStatus() bool {
    self.mutex.Lock()
    defer self.mutex.Unlock()

    return len(self.statuses) > 0
}
Dominant language
Go
Stars
82.5k
Forks
3k
Avg merge
1d 14h
Merged PRs (30d)
22

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from jesseduffield/lazygit

All issues in jesseduffield/lazygit

Similar issues

More Go issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.