Skip to content

WithoutSignalHandler() #1359

@JetSetIlly

Description

@JetSetIlly

Describe the bug

WithoutSignalHandler() doesn't work as I would expect

Setup

go 1.24.0
bubbletea 1.3.4
xterm

To Reproduce

With reference to the program below, the first ctrl-c press is correctly received by the channel set up with signal.Notify().

Once the bubbletea loop starts, pressing keys causes the message to change. Pressing the r key however causes the program to wait for another ctrl-c press. However, the program never receives the ctrl-c press over the previously established channel

The choice of r key isn't part of the problem. It's just a way of getting the loop to pause and wait for a signal.

Source Code

package main

import (
	"fmt"
	"os"
	"os/signal"
	"syscall"

	tea "github.com/charmbracelet/bubbletea"
)

type model struct {
	sig     chan os.Signal
	message string
}

func (m *model) Init() tea.Cmd {
	return nil
}

func (m *model) View() string {
	return m.message
}

func (m *model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
	switch msg := msg.(type) {
	case tea.KeyMsg:
		switch msg.String() {
		case "r":
			fmt.Println("press ctrl-c to resume")
			<-m.sig
			m.message = "completed"
		default:
			m.message = fmt.Sprintf("%s pressed", msg.String())
		}
	}
	return m, nil
}

func main() {
	m := &model{
		sig:     make(chan os.Signal, 1),
		message: "press a key",
	}

	signal.Notify(m.sig, syscall.SIGINT)
	fmt.Println("press ctrl-c to start")
	<-m.sig

	p := tea.NewProgram(m, tea.WithoutSignalHandler())
	p.Run()
}

Expected behavior

When WithoutSignalHandler() has been used as an option to NewProgram() then I would expect the SIGINT to be sent over the sig channel after the bubbletea loop has started.

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