Skip to content

Commit 8e3039a

Browse files
committed
Set a finalizer on Conn
1 parent 9768e12 commit 8e3039a

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

websocket.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"context"
66
"fmt"
77
"io"
8+
"runtime"
89
"sync"
910
"time"
1011

@@ -19,7 +20,6 @@ type controlFrame struct {
1920
// Conn represents a WebSocket connection.
2021
// Pings will always be automatically responded to with pongs, you do not
2122
// have to do anything special.
22-
// TODO set finalizer
2323
type Conn struct {
2424
subprotocol string
2525
br *bufio.Reader
@@ -59,6 +59,8 @@ func (c *Conn) close(err error) {
5959
}
6060

6161
c.closeOnce.Do(func() {
62+
runtime.SetFinalizer(c, nil)
63+
6264
c.closeErr = err
6365

6466
cerr := c.closer.Close()
@@ -82,6 +84,10 @@ func (c *Conn) init() {
8284
c.read = make(chan opcode)
8385
c.readBytes = make(chan []byte)
8486

87+
runtime.SetFinalizer(c, func(c *Conn) {
88+
c.Close(StatusInternalError, "websocket: connection ended up being garbage collected")
89+
})
90+
8591
go c.writeLoop()
8692
go c.readLoop()
8793
}

0 commit comments

Comments
 (0)