Skip to content

Commit

Permalink
fix(tea): allocate msgs channel in constructor
Browse files Browse the repository at this point in the history
Race condition bug: Start() is called in a new gorotuine, then Send(). If the
Send happens before the msgs channel is allocated, the message is dropped.

Instead allocate the channel in the constructor, so msgs is never nil.

Signed-off-by: Christian Stewart <christian@paral.in>
  • Loading branch information
paralin authored and meowgorithm committed Jan 14, 2022
1 parent 66cea09 commit 608fde5
Showing 1 changed file with 2 additions and 5 deletions.
7 changes: 2 additions & 5 deletions tea.go
Expand Up @@ -239,6 +239,7 @@ func NewProgram(model Model, opts ...ProgramOption) *Program {
initialModel: model,
output: os.Stdout,
input: os.Stdin,
msgs: make(chan Msg),
CatchPanics: true,
}

Expand All @@ -252,8 +253,6 @@ func NewProgram(model Model, opts ...ProgramOption) *Program {

// StartReturningModel initializes the program. Returns the final model.
func (p *Program) StartReturningModel() (Model, error) {
p.msgs = make(chan Msg)

var (
cmds = make(chan Cmd)
errs = make(chan error)
Expand Down Expand Up @@ -554,9 +553,7 @@ func (p *Program) Start() error {
// This method is currently provisional. The method signature may alter
// slightly, or it may be removed in a future version of this package.
func (p *Program) Send(msg Msg) {
if p.msgs != nil {
p.msgs <- msg
}
p.msgs <- msg
}

// Quit is a convenience function for quitting Bubble Tea programs. Use it
Expand Down

0 comments on commit 608fde5

Please sign in to comment.