surrealdb / surrealdb.go

SurrealDB SDK for Golang
https://surrealdb.com
Apache License 2.0
239 stars 66 forks source link

panic: repeated read on failed websocket connection #144

Open JohnKinyanjui opened 2 months ago

JohnKinyanjui commented 2 months ago

Describe the bug

Am not sure but i think this error might by gorilla websocket please consider to switch to a better library, it unable to handle websocket correctly but if its from my end let me know

Steps to reproduce

this is my simple middleware in golang for echo, i use it to authorize tokens on the rest api

func AuthMiddleware(next echo.HandlerFunc) echo.HandlerFunc {

    return func(c echo.Context) error {
        token := c.Request().Header.Get("Authorization")
        tokenString := strings.Split(token, " ")
        if len(tokenString) != 2 {
            return c.JSON(http.StatusUnauthorized, map[string]string{"message": "missing or invalid token"})
        }

        db, err := database.Connection(tokenString[1])
        if err != nil {
            return c.JSON(http.StatusUnauthorized, map[string]string{"message": "invalid token"})
        }
        defer db.Close()

        c.Set("db", db)

        return next(c)
    }
}

Expected behaviour

panic: repeated read on failed websocket connection

goroutine 315 [running]:
github.com/gorilla/websocket.(*Conn).NextReader(0xc000557b80)
        C:/Users/hp/go/pkg/mod/github.com/gorilla/websocket@v1.5.1/conn.go:1055 +0x28d
github.com/gorilla/websocket.(*Conn).ReadMessage(0xc00025ff70?)
        C:/Users/hp/go/pkg/mod/github.com/gorilla/websocket@v1.5.1/conn.go:1120 +0x13
github.com/surrealdb/surrealdb.go/internal/websocket.(*WebSocket).read(0xc0005b04e0?, {0x12f51e0, 0xc0003359e0})
        C:/Users/hp/go/pkg/mod/github.com/surrealdb/surrealdb.go@v0.2.1/internal/websocket/ws.go:140 +0x25
github.com/surrealdb/surrealdb.go/internal/websocket.(*WebSocket).initialize.func1()
        C:/Users/hp/go/pkg/mod/github.com/surrealdb/surrealdb.go@v0.2.1/internal/websocket/ws.go:167 +0x5e
created by github.com/surrealdb/surrealdb.go/internal/websocket.(*WebSocket).initialize in goroutine 351
        C:/Users/hp/go/pkg/mod/github.com/surrealdb/surrealdb.go@v0.2.1/internal/websocket/ws.go:160 +0x4f

SurrealDB version

1.5.4 for windows on x86_64

Is there an existing issue for this?

Code of Conduct

ElecTwix commented 2 months ago

Am not sure but i think this error might by gorilla websocket please consider to switch to a better library, it unable to handle websocket correctly but if its from my end let me know

Hi @JohnKinyanjui thanks for your issue, I did propose to switch long before. it wasn't accepted https://github.com/surrealdb/surrealdb.go/pull/127. Then, PR was created to replace WS, which has still not been reviewed.