Skip to content

Proposal: expose tea.msg to handle multithreading scenarios #281

@Adjective-Object

Description

@Adjective-Object

I'm using bubbletea as a persistent UI in a logger.
The logger has its own main loop that receives messages from multiple routines, and in that loop, calls tea.Send() on some subset of messages.

Unfortunately, if the program self-exits, there's a possibility that the loop deadlocks if a message is received by the main loop before we know the tea program has exited or not, because tea.Send() blocks when there is no receiver on the other side of the channel.

The canonical go solution would to do something like this

teaProg := tea.Program(myProg{})
teaExited := make(chan error)
go func() {
  // notify when tea is done
  teaExited <- teaProg.Start()
}()

go func() {
  // in my main loop
  select  {
  case tea.msg <- someMessage:
  case <-teaExited:
    // clean up the tea program, and
    // stop trying to send messages to tea
  }
}

there's a similar problem if you try to Quit() a dead tea program.

I was wondering if it makes sense to expose the tea message channel so it could be selected on.

Spitballing on alternatives, I thought for a bit about if this would be possible to implement internally without exposing the message channel, but I think if multiple routines are calling tea.Send(), the only way to guarantee that all of them recieve the teaExited message is if a goroutine that infinitely writes to teaExited is leaked on program quit, which doesn't seem acceptable.
Or, if you try to react to teaExited in Send() and set a flag, then we either have a possible race with simultaneous writes after the program exits, or have to put a lock around every call to tea.Send(). And locking on every Send() seems like it would be really bad for perf to me.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions