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 should guarantee no more goroutines #330

Closed
kylecarbs opened this issue Feb 20, 2022 · 3 comments
Closed

Close should guarantee no more goroutines #330

kylecarbs opened this issue Feb 20, 2022 · 3 comments
Milestone

Comments

@kylecarbs
Copy link

If Close is called after the HTTP server has been closed, a goroutine currently leaks.

package main

import (
	"context"
	"net/http"
	"net/http/httptest"

	"go.uber.org/goleak"
	"nhooyr.io/websocket"
)

func main() {
	done := make(chan struct{})
	srv := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
		conn, err := websocket.Accept(rw, r, nil)
		if err != nil {
			panic(err)
		}
		close(done)
		conn.Close(websocket.StatusNormalClosure, "")
	}))
	_, _, err := websocket.Dial(context.Background(), srv.URL, nil)
	if err != nil {
		panic(err)
	}
	<-done
	srv.Close()
	err = goleak.Find()
	if err != nil {
		panic(err)
	}
}
@kylecarbs
Copy link
Author

Nevermind, it shouldn't. The connection should always send a closure so the opposing end doesn't timeout on the connection.

@nhooyr nhooyr reopened this Oct 19, 2023
@nhooyr nhooyr added this to the v1.8.8 milestone Oct 19, 2023
@nhooyr
Copy link
Owner

nhooyr commented Oct 19, 2023

Nah this is an issue. I forgot to respond. Glad I remembered this lol.

After Close() returns, you should be guaranteed that there are no more goroutines for the websocket. This is currently not the case.

@nhooyr nhooyr changed the title Close() should accept a context to allow for clean exits Close should guarantee no more goroutines Oct 19, 2023
nhooyr added a commit that referenced this issue Oct 19, 2023
nhooyr added a commit that referenced this issue Oct 19, 2023
@nhooyr
Copy link
Owner

nhooyr commented Oct 19, 2023

Fixed in dev.

@nhooyr nhooyr closed this as completed Oct 19, 2023
nhooyr added a commit that referenced this issue Oct 19, 2023
nhooyr added a commit that referenced this issue Oct 19, 2023
nhooyr added a commit that referenced this issue Oct 19, 2023
nhooyr added a commit that referenced this issue Oct 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants