Skip to content
This repository has been archived by the owner on Dec 16, 2021. It is now read-only.

Commit

Permalink
Fixed potential deadlock during channel loading
Browse files Browse the repository at this point in the history
Due to the fact that channels where loaded in a background thread which
then moved 99% of it's logic back onto the UI-thread and waited for it
to finish, switching channels very quickly (Alt+L - Load previous
channel) caused a deadlock. The reasoning behind this back then, was to
reduce timeframes in which the UI doesn't respond, but it was basically
incorrect either way.
  • Loading branch information
Bios-Marcel committed Oct 14, 2020
1 parent dcab2d1 commit e1276b0
Showing 1 changed file with 26 additions and 33 deletions.
59 changes: 26 additions & 33 deletions ui/window.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,16 +154,10 @@ func NewWindow(doRestart chan bool, app *tview.Application, session *discordgo.S
channelTree.SetOnChannelSelect(func(channelID string) {
channel, cacheError := window.session.State.Channel(channelID)
if cacheError == nil && channel.Type != discordgo.ChannelTypeGuildCategory {
go func() {
window.chatView.Lock()
defer window.chatView.Unlock()
window.QueueUpdateDrawSynchronized(func() {
loadError := window.LoadChannel(channel)
if loadError != nil {
window.ShowErrorDialog(loadError.Error())
}
})
}()
loadError := window.LoadChannel(channel)
if loadError != nil {
window.ShowErrorDialog(loadError.Error())
}
}
})
window.registerGuildChannelHandler()
Expand Down Expand Up @@ -235,28 +229,24 @@ func NewWindow(doRestart chan bool, app *tview.Application, session *discordgo.S
return
}

go func() {
window.chatView.Lock()
defer window.chatView.Unlock()
window.QueueUpdateDrawSynchronized(func() {
window.userList.Clear()
window.RefreshLayout()
window.chatView.Lock()
defer window.chatView.Unlock()

if channel.Type == discordgo.ChannelTypeGroupDM {
window.userList.Clear()
window.RefreshLayout()

if window.userList.internalTreeView.IsVisible() {
loadError := window.userList.LoadGroup(channel.ID)
if loadError != nil {
fmt.Fprintln(window.commandView.commandOutput, "Error loading users for channel.")
}
}
}
if channel.Type == discordgo.ChannelTypeGroupDM && window.userList.internalTreeView.IsVisible() {
loadError := window.userList.LoadGroup(channel.ID)
//The userlist isn't important, so we'll still try loading the channel.
if loadError != nil {
fmt.Fprintln(window.commandView.commandOutput, "Error loading users for channel.")
}
}

})
window.QueueUpdateDrawSynchronized(func() {
window.LoadChannel(channel)
})
}()
loadError := window.LoadChannel(channel)
if loadError != nil {
window.ShowErrorDialog(loadError.Error())
}
})

window.privateList.SetOnFriendSelect(func(userID string) {
Expand Down Expand Up @@ -2230,10 +2220,6 @@ func (window *Window) handleGlobalShortcuts(event *tcell.EventKey) *tcell.EventK
func (window *Window) toggleUserContainer() {
config.Current.ShowUserContainer = !config.Current.ShowUserContainer

if !config.Current.ShowUserContainer && window.app.GetFocus() == window.userList.internalTreeView {
window.app.SetFocus(window.messageInput.GetPrimitive())
}

if config.Current.ShowUserContainer {
if !window.userList.IsLoaded() {
if window.selectedChannel != nil && window.selectedChannel.GuildID == "" {
Expand All @@ -2243,6 +2229,13 @@ func (window *Window) toggleUserContainer() {
}
}
} else {
//If the userList was focused before, we focus the input, so that the
//user can still properly navigate around via alt+arrowkey.
if window.app.GetFocus() == window.userList.internalTreeView {
window.app.SetFocus(window.messageInput.GetPrimitive())
}
//If we hide away the user list, we remove all data and allow the GC
//to clean it up.
window.userList.Clear()
}

Expand Down

0 comments on commit e1276b0

Please sign in to comment.