andersfylling / disgord

Go module for interacting with the documented Discord's bot interface; Gateway, REST requests and voice
BSD 3-Clause "New" or "Revised" License
496 stars 70 forks source link

Bot hangs after channel update #396

Closed kanielc closed 3 years ago

kanielc commented 3 years ago

Describe the bug After calling channel update, bot is no longer responsive to any commands. Executed method looks like:

func (sm slowmode) Exec(s disgord.Session, data *disgord.MessageCreate) {
    msg := data.Message

    mentionedChannel, _ := 123456789
    amount := 15
    reason := "just a reason"

    _, err := s.Channel(disgord.Snowflake(mentionedChannel)).UpdateBuilder().SetRateLimitPerUser(uint(amount)).Execute()

    if err != nil {
        log.Println(err)
    }

    msg.Reply(noCtx, s, fmt.Sprintf("Set the slowmode to **%d** in the channel **<#%d>** with reason **%s**", amount, mentionedChannel, reason))
}

Handler looks like

// handleMsg is a basic command handler
func handleMsg(s disgord.Session, data *disgord.MessageCreate) {
    msg := data.Message

    var cmdStr string
    spaceLoc := strings.Index(msg.Content, " ")
    if spaceLoc == -1 {
        cmdStr = msg.Content
    } else {
        cmdStr = msg.Content[:spaceLoc]
    }

    cmd, ok := commands.Commands[cmdStr]

    // if command not found, just return
    if !ok {
        return
    }

    cmd.Exec(s, data)
}

Expected behavior Follow up commands should invoke the handleMsg function

Error messages No errors are thrown

Desktop (please complete the following information):

Additional context

Took images of the call stack with debugger and noticed a network connection thread seemed to still be active. Everything is fine after the first image, but the second one is where it hangs. afterping afterslow

kanielc commented 3 years ago

This seems to be the place where things go wrong.

image

andersfylling commented 3 years ago

So that fixes one potential deadlock, but i dont think that is the actual issue

andersfylling commented 3 years ago

Yeah apparently I lock the same mutex twice. Once in updateChannel and once before calling it in a special branch: https://github.com/andersfylling/disgord/blob/develop/cache.go#L174

Currently writing a bunch of tests to fix this and detect other issues.

andersfylling commented 3 years ago

fix is not yet released. but u can checkout the latest commit and give it a try

kanielc commented 3 years ago

It works, thanks!