Skip to content

Close should guarantee no more goroutines #330

Closed

Description

@kylecarbs

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)
	}
}

Activity

  1. kylecarbs commented on Feb 20, 2022

    @kylecarbs
    MemberAuthor

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

  2. reopened this on Oct 19, 2023
  3. added this to the v1.8.8 milestone on Oct 19, 2023
  4. nhooyr commented on Oct 19, 2023

    @nhooyr
    Contributor

    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.

  5. changed the title [-]Close() should accept a context to allow for clean exits[/-] [+]Close should guarantee no more goroutines[/+] on Oct 19, 2023
  6. nhooyr commented on Oct 19, 2023

    @nhooyr
    Contributor

    Fixed in dev.

  7. added 5 commits that reference this issue on Oct 19, 2023
    d7a55cf
    5b2b438
    77f7262
    e72696e
    d91a212
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions