[GH-ISSUE #563] App is crashing because of a textView #413

Closed
opened 2026-03-04 01:04:46 +03:00 by kerem · 3 comments
Owner

Originally created by @inmylo on GitHub (Feb 8, 2021).
Original GitHub issue: https://github.com/rivo/tview/issues/563

My application has a textView, which is being updated very often, for example each millisecond. It sometimes crashes when I try to close a modal window with app.SetRoot(pages, true). I've figured out, that textview.go's t.buffer is sometimes empty, so I tried to add an additional check and it seems to help. You can choose a more efficient way if such exists.

Created a PR #562

Some time ago I got a similar error at the line 826 str := t.buffer[line.Line][line.Pos:line.NextPos] - maybe it should be fixed too.

Panic's error:

panic: runtime error: index out of range [1] with length 0 [recovered]
	panic: runtime error: index out of range [1] with length 0

goroutine 1 [running]:
github.com/rivo/tview.(*Application).Run.func1(0xc00015e000)
	/path/src/github.com/rivo/tview/application.go:243 +0x87
panic(0x5916e0, 0xc0001b40c0)
	/path/src/runtime/panic.go:969 +0x1b9
github.com/rivo/tview.(*TextView).Draw(0xc000152300, 0x5da440, 0xc0000d6600)
	/path/src/github.com/rivo/tview/textview.go:947 +0xded
github.com/rivo/tview.(*Grid).Draw(0xc000162090, 0x5da440, 0xc0000d6600)
	/path/src/github.com/rivo/tview/grid.go:626 +0x12cf
github.com/rivo/tview.(*Pages).Draw(0xc00009ad80, 0x5da440, 0xc0000d6600)
	/path/src/github.com/rivo/tview/pages.go:277 +0xb9
github.com/rivo/tview.(*Application).draw(0xc00015e000, 0x0)
	/path/src/github.com/rivo/tview/application.go:595 +0xd9
github.com/rivo/tview.(*Application).Run(0xc00015e000, 0x0, 0x0)
	/path/src/github.com/rivo/tview/application.go:343 +0x625
main.main()
	/path/src/github.com/user/myapp/main.go:43 +0x88
exit status 2
Originally created by @inmylo on GitHub (Feb 8, 2021). Original GitHub issue: https://github.com/rivo/tview/issues/563 My application has a **textView**, which is being updated very often, for example each millisecond. It sometimes crashes when I try to close a modal window with `app.SetRoot(pages, true)`. I've figured out, that `textview.go`'s `t.buffer` is sometimes empty, so I tried to add an additional check and it seems to help. You can choose a more efficient way if such exists. Created a PR #562 Some time ago I got a similar error at the line `826` `str := t.buffer[line.Line][line.Pos:line.NextPos]` - maybe it should be fixed too. Panic's error: ``` panic: runtime error: index out of range [1] with length 0 [recovered] panic: runtime error: index out of range [1] with length 0 goroutine 1 [running]: github.com/rivo/tview.(*Application).Run.func1(0xc00015e000) /path/src/github.com/rivo/tview/application.go:243 +0x87 panic(0x5916e0, 0xc0001b40c0) /path/src/runtime/panic.go:969 +0x1b9 github.com/rivo/tview.(*TextView).Draw(0xc000152300, 0x5da440, 0xc0000d6600) /path/src/github.com/rivo/tview/textview.go:947 +0xded github.com/rivo/tview.(*Grid).Draw(0xc000162090, 0x5da440, 0xc0000d6600) /path/src/github.com/rivo/tview/grid.go:626 +0x12cf github.com/rivo/tview.(*Pages).Draw(0xc00009ad80, 0x5da440, 0xc0000d6600) /path/src/github.com/rivo/tview/pages.go:277 +0xb9 github.com/rivo/tview.(*Application).draw(0xc00015e000, 0x0) /path/src/github.com/rivo/tview/application.go:595 +0xd9 github.com/rivo/tview.(*Application).Run(0xc00015e000, 0x0, 0x0) /path/src/github.com/rivo/tview/application.go:343 +0x625 main.main() /path/src/github.com/user/myapp/main.go:43 +0x88 exit status 2 ```
kerem closed this issue 2026-03-04 01:04:46 +03:00
Author
Owner

@rivo commented on GitHub (Feb 17, 2021):

How do you update the TextView's content? Do you write to it using the io.Writer interface or do you call SetText() on it?

<!-- gh-comment-id:780491744 --> @rivo commented on GitHub (Feb 17, 2021): How do you update the `TextView`'s content? Do you write to it using the `io.Writer` interface or do you call `SetText()` on it?
Author
Owner

@inmylo commented on GitHub (Feb 17, 2021):

Currently I do it this way:

go func() {
	for {
		widget.SetText(text)
		app.Draw()
		time.Sleep(32 * time.Millisecond)
	}
}()

But I'm not sure whether I have to call Draw() every iteration, but it also seems to help to prevent crashing. Maybe it can be avoided to decrease CPU load

<!-- gh-comment-id:780685469 --> @inmylo commented on GitHub (Feb 17, 2021): Currently I do it this way: ``` go func() { for { widget.SetText(text) app.Draw() time.Sleep(32 * time.Millisecond) } }() ``` But I'm not sure whether I have to call `Draw()` every iteration, but it also seems to help to prevent crashing. Maybe it can be avoided to decrease CPU load
Author
Owner

@rivo commented on GitHub (Apr 27, 2021):

When you call app.Draw(), it will not redraw immediately but queue a redraw request to be completed at the next possible opportunity (from within the main loop). Doing this every 32 milliseconds might lead that request queue to become too big if the loop can't keep up.

Also, the SetText() function is not thread-safe and shouldn't be called in a non-main goroutine.

I would suggest that you rather do something like this:

go func() {
	for {
		app.QueueUpdateDraw(func() {
			textView.SetText(text)
		})
	}
}()

If you still get crashes with this code, please post some (short) code that reproduces that issue so I can get to the root of it.

<!-- gh-comment-id:827562945 --> @rivo commented on GitHub (Apr 27, 2021): When you call `app.Draw()`, it will not redraw immediately but queue a redraw request to be completed at the next possible opportunity (from within the main loop). Doing this every 32 milliseconds might lead that request queue to become too big if the loop can't keep up. Also, the `SetText()` function is not thread-safe and shouldn't be called in a non-main goroutine. I would suggest that you rather do something like this: ```go go func() { for { app.QueueUpdateDraw(func() { textView.SetText(text) }) } }() ``` If you still get crashes with this code, please post some (short) code that reproduces that issue so I can get to the root of it.
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#413
No description provided.