[GH-ISSUE #318] Graceful shutdown #243

Closed
opened 2026-03-04 01:03:16 +03:00 by kerem · 1 comment
Owner

Originally created by @kennykarnama on GitHub (Jun 29, 2019).
Original GitHub issue: https://github.com/rivo/tview/issues/318

Hi it is very awesome library. Actually i try to build a simple app, that run music in background when displaying pages. The problem arose because my golang version doesn't support syscall/js. so i try another way by using ffplayer invoked by using exec function in golang. I have already create the stop function in order to stop the music when the ui got closed (normally). But here is the trouble i found, when we give interrupt signal (ctrl+c), it is caught by thread of tview. and stop the application, but not the ffplayer that has been run before. So, the graceful shutdown scheme not work quite well.
so, just wondering, how about we implement wrapper pattern inside the stop function in twview ?
cause by doing that i was able to gracefully shutdown all the goroutine. Here is my current solution :

func (a *Application) HandleStop(stop func()) {
	var s []func()
	s = append(s, stop)
	a.stopFunc = s
}

on the existing code, inside the event loop in start()

// Ctrl-C closes the application.
				if event.Key() == tcell.KeyCtrlC {
					stop := a.stopFunc[0]
					stop()
				}

it would be better if i just show my PR ? thanks

Originally created by @kennykarnama on GitHub (Jun 29, 2019). Original GitHub issue: https://github.com/rivo/tview/issues/318 Hi it is very awesome library. Actually i try to build a simple app, that run music in background when displaying pages. The problem arose because my golang version doesn't support syscall/js. so i try another way by using ffplayer invoked by using exec function in golang. I have already create the stop function in order to stop the music when the ui got closed (normally). But here is the trouble i found, when we give interrupt signal (ctrl+c), it is caught by thread of tview. and stop the application, but not the ffplayer that has been run before. So, the graceful shutdown scheme not work quite well. so, just wondering, how about we implement wrapper pattern inside the stop function in twview ? cause by doing that i was able to gracefully shutdown all the goroutine. Here is my current solution : ``` func (a *Application) HandleStop(stop func()) { var s []func() s = append(s, stop) a.stopFunc = s } ``` on the existing code, inside the event loop in start() ``` // Ctrl-C closes the application. if event.Key() == tcell.KeyCtrlC { stop := a.stopFunc[0] stop() } ``` it would be better if i just show my PR ? thanks
kerem closed this issue 2026-03-04 01:03:16 +03:00
Author
Owner

@rivo commented on GitHub (Jul 10, 2019):

Have you tried Application.SetInputCapture()? The description explicitly says:

Such a handler can intercept the Ctrl-C event which closes the application.

When you detect a Ctrl-C, you can also perform any other cleanup.

Without knowing the details of your program, however, I would think that cleaning up your threads could go at the end of your program (i.e. after the Run() function). Even in the case of a Ctrl-C interrupt signal, that part of your program should still be executed.

<!-- gh-comment-id:510026083 --> @rivo commented on GitHub (Jul 10, 2019): Have you tried [`Application.SetInputCapture()`](https://godoc.org/github.com/rivo/tview#Application.SetInputCapture)? The description explicitly says: > Such a handler can intercept the Ctrl-C event which closes the application. When you detect a Ctrl-C, you can also perform any other cleanup. Without knowing the details of your program, however, I would think that cleaning up your threads could go at the end of your program (i.e. after the `Run()` function). Even in the case of a `Ctrl-C` interrupt signal, that part of your program should still be executed.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/tview#243
No description provided.