nicklaw5 / helix

A Twitch Helix API client written in Go.
MIT License
243 stars 89 forks source link

[bug] helixClient.CreateEventSubSubscription fails with `Failed to execute API request` #228

Open JohnnyLin-a opened 2 months ago

JohnnyLin-a commented 2 months ago

Context: Library version 2.30 Transport method: websocket

Description: The helixClient.CreateEventSubSubscription does not work anymore. It used to work fine last week (early September 2024).

Error message: Failed to execute API request: Post "https://api.twitch.tv/helix/eventsub/subscriptions": stream error: stream ID 'N'; INTERNAL_ERROR; received from peer

Code snippet:

func registerEvents(ws *WSConn, events *struct {
    Client *helix.Client
    Raw    []*helix.EventSubSubscription
}) error {
    // register events
    for _, v := range events.Raw {
        v.Transport.SessionID = ws.sessionID

        resp, err := events.Client.CreateEventSubSubscription(v)

        if err != nil {
            ws.Log.Println("Failed to subscribe to "+v.Type, err)
            if raw, ok := (*rawByChannel).Load(ws.channel); ok {
                raw.Close() // This closes my websocket connection in my internal package
            }
            return errors.New("eventsub: failed to sub " + v.Type)
        }
        ws.Log.Println("SUCCESS welcome register eventsub "+v.Type, resp.StatusCode)
    }
    return nil
}

Workaround:

func registerEvents(ws *WSConn, events *struct {
    Client *helix.Client
    Raw    []*helix.EventSubSubscription
}) error {
    httpClient := http.Client{}
    // register events
    for _, v := range events.Raw {
        v.Transport.SessionID = ws.sessionID
        events.Client.GetUsers(&helix.UsersParams{}) // TODO: remove this, only used to refresh token
        rawSubBody, _ := json.Marshal(v)
        // This function below suddenly doesn't work, which is why the manual http call is made
        // resp, err := events.Client.CreateEventSubSubscription(v)

        req, _ := http.NewRequest(http.MethodPost, "https://api.twitch.tv/helix/eventsub/subscriptions", bytes.NewBuffer(rawSubBody))
        req.Header.Set("Client-Id", data.AppCfg.TwitchAPIClientID)
        req.Header.Set("Content-Type", "application/json")
        req.Header.Set("Authorization", "Bearer "+events.Client.GetUserAccessToken())

        resp, err := httpClient.Do(req)

        if err != nil || resp.StatusCode != http.StatusAccepted {
            ws.Log.Println("Failed to subscribe to "+v.Type, err)
            if raw, ok := (*rawByChannel).Load(ws.channel); ok {
                raw.Close()
            }
            return errors.New("eventsub: failed to sub " + v.Type)
        }
        ws.Log.Println("SUCCESS welcome register eventsub "+v.Type, resp.StatusCode)
    }
    return nil
}

Full output from my app with error:

EVENTSUB_channelname 2024/09/14 17:04:01 Connecting to wss://eventsub.wss.twitch.tv/ws
EVENTSUB_channelname 2024/09/14 17:04:01 Failed to subscribe to channel.channel_points_custom_reward_redemption.add Failed to execute API request: Post "https://api.twitch.tv/helix/eventsub/subscriptions": stream error: stream ID 7; INTERNAL_ERROR; received from peer
2024/09/14 17:04:01 Log to file error when reading eventsub websocket
read tcp 10.0.0.132:47688-><redacted_public_twitch_ip>:443: use of closed network connection
goroutine 36 [running]:
runtime/debug.Stack()
        /home/ubuntu/sdk/go1.23.1/src/runtime/debug/stack.go:26 +0x64
github.com/johnnylin-a/twitch-bot/pkg/eventsub.RunWS(0x54ee88, 0x40001bc160, {0x4000182f48, 0x8})
        /mnt/vol1/ubuntu/code/twitch-bot/pkg/eventsub/eventsub.go:78 +0x3bc
main.main.gowrap1.main.func1.1({0x4000034180?, 0x847a4?}, {0x4000182f48, 0x8})
        /mnt/vol1/ubuntu/code/twitch-bot/cmd/botws/main.go:69 +0xf0
main.main.func1(...)
        /mnt/vol1/ubuntu/code/twitch-bot/cmd/botws/main.go:70
created by main.main in goroutine 1
        /mnt/vol1/ubuntu/code/twitch-bot/cmd/botws/main.go:51 +0x470

EVENTSUB_channelname 2024/09/14 17:04:01 Closing raw socket due to error

Full output from my app with workaround:

EVENTSUB_channelname 2024/09/14 17:19:56 Connecting to wss://eventsub.wss.twitch.tv/ws
EVENTSUB_channelname 2024/09/14 17:19:57 SUCCESS welcome register eventsub channel.channel_points_custom_reward_redemption.add 202channel.channel_points_custom_reward_redemption.add 202
JohnnyLin-a commented 2 months ago

I hope I included enough details to help debug this. Please let me know if I am missing anything else. I'm open to help further if necessary.

I also vaguely remember an error log that looked like a JSON parse error in addition to this on the lib version 2.28. Really odd from my POV.