readline
readline copied to clipboard
data race on creating new terminal
I am using readline in my project and have recently run tests for it with race detector enabled. My tests caught potential race condition in your code. As you can see in terminal.go, creating new terminal starts new goroutine:
func NewTerminal(cfg *Config) (*Terminal, error) {
...
go t.ioloop()
return t, nil
}
And there is sync group variable that gets Add called inside this routine:
func (t *Terminal) ioloop() {
t.wg.Add(1)
defer t.wg.Done()
...
}
Such usage of sync groups is incorrect, as it enables race conditions. Add should be called by caller routine before ioloop starts (please see this answer).
It is worth noting that your own tests pass even with race detector enabled. I suggest you move Add call from ioloop to NewTerminal and add a test so that race detector can cover this case.