From 189c66792832c83070cf363e8d57d9db5610f0e8 Mon Sep 17 00:00:00 2001 From: Chris Cummer Date: Fri, 11 Oct 2019 06:17:30 -0700 Subject: [PATCH] Fix some of the transmission module race conditions Signed-off-by: Chris Cummer --- modules/transmission/display.go | 13 +++++++++---- modules/transmission/widget.go | 29 +++++++++++++++++++---------- 2 files changed, 28 insertions(+), 14 deletions(-) diff --git a/modules/transmission/display.go b/modules/transmission/display.go index 1e0f2589..ef3e1b5c 100644 --- a/modules/transmission/display.go +++ b/modules/transmission/display.go @@ -9,15 +9,24 @@ import ( "github.com/wtfutil/wtf/utils" ) +func (widget *Widget) display() { + widget.ScrollableWidget.Redraw(widget.content) +} + func (widget *Widget) content() (string, string, bool) { + widget.mu.Lock() + defer widget.mu.Unlock() + title := widget.CommonSettings().Title if widget.err != nil { return title, widget.err.Error(), true } + data := widget.torrents if data == nil || len(data) == 0 { return title, "No data", false } + str := "" for idx, torrent := range data { @@ -38,10 +47,6 @@ func (widget *Widget) content() (string, string, bool) { return title, str, false } -func (widget *Widget) display() { - widget.ScrollableWidget.Redraw(widget.content) -} - func (widget *Widget) prettyTorrentName(name string) string { str := strings.Replace(name, "[", "(", -1) str = strings.Replace(str, "]", ")", -1) diff --git a/modules/transmission/widget.go b/modules/transmission/widget.go index 7649cb19..aa33b504 100644 --- a/modules/transmission/widget.go +++ b/modules/transmission/widget.go @@ -2,6 +2,7 @@ package transmission import ( "errors" + "sync" "github.com/hekmon/transmissionrpc" "github.com/rivo/tview" @@ -15,6 +16,7 @@ type Widget struct { client *transmissionrpc.Client settings *Settings + mu sync.Mutex torrents []*transmissionrpc.Torrent err error } @@ -43,6 +45,9 @@ func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) * // Fetch retrieves torrent data from the Transmission daemon func (widget *Widget) Fetch() ([]*transmissionrpc.Torrent, error) { + widget.mu.Lock() + widget.mu.Unlock() + if widget.client == nil { return nil, errors.New("client was not initialized") } @@ -58,16 +63,18 @@ func (widget *Widget) Fetch() ([]*transmissionrpc.Torrent, error) { // Refresh updates the data for this widget and displays it onscreen func (widget *Widget) Refresh() { torrents, err := widget.Fetch() - if err != nil { - widget.err = err - widget.torrents = nil - widget.SetItemCount(0) - } else { - widget.err = nil - widget.torrents = torrents - widget.SetItemCount(len(torrents)) + count := 0 + + if err == nil { + count = len(torrents) } + widget.mu.Lock() + widget.err = err + widget.torrents = torrents + widget.SetItemCount(count) + widget.mu.Unlock() + widget.display() } @@ -96,6 +103,9 @@ func (widget *Widget) Unselect() { // buildClient creates a persisten transmission client func buildClient(widget *Widget) { + widget.mu.Lock() + defer widget.mu.Unlock() + client, err := transmissionrpc.New(widget.settings.host, widget.settings.username, widget.settings.password, &transmissionrpc.AdvancedConfig{ Port: widget.settings.port, @@ -103,9 +113,8 @@ func buildClient(widget *Widget) { if err != nil { client = nil } - widget.client = client - widget.Refresh() + widget.client = client } func (widget *Widget) currentTorrent() *transmissionrpc.Torrent {