Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Close handshake not working correctly #164

Closed
nhooyr opened this issue Oct 22, 2019 · 0 comments · Fixed by #165
Closed

Close handshake not working correctly #164

nhooyr opened this issue Oct 22, 2019 · 0 comments · Fixed by #165
Labels

Comments

@nhooyr
Copy link
Contributor

nhooyr commented Oct 22, 2019

Was reported to me on reddit by @skwair.

Reproducible example:

package main

import (
	"context"
	"fmt"
	"time"

	"nhooyr.io/websocket"
)

func main() {
	ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
	defer cancel()

	c, _, err := websocket.Dial(ctx, "ws://localhost:3333", nil)
	if err != nil {
		// ...
	}
	defer c.Close(websocket.StatusInternalError, "the sky is falling")

	go func() {
		_, _, err := c.Read(context.TODO())
		fmt.Println("failed to read:", err)
	}()

	fmt.Println(c.Close(websocket.StatusNormalClosure, ""))
}
package main

import (
	"context"
	"log"
	"net/http"
	"time"

	"nhooyr.io/websocket"
	"nhooyr.io/websocket/wsjson"
)

func main() {
	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		c, err := websocket.Accept(w, r, nil)
		if err != nil {
			// ...
		}
		defer c.Close(websocket.StatusInternalError, "the sky is falling")

		ctx, cancel := context.WithTimeout(r.Context(), time.Second*10)
		defer cancel()

		var v interface{}
		err = wsjson.Read(ctx, c, &v)
		if err != nil {
			log.Println(err)
			return
			// ...
		}

		log.Printf("received: %v", v)

		c.Close(websocket.StatusNormalClosure, "")
	})

	http.ListenAndServe(":3333", nil)
}

Definitely some sort of race condition as it works normally sometimes for me but interestingly enough the client side Read in the other goroutine returns a nil error.

Will fix ASAP.

nhooyr added a commit that referenced this issue Oct 23, 2019
nhooyr added a commit that referenced this issue Oct 25, 2019
@nhooyr nhooyr mentioned this issue Oct 25, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant