Skip to content

Commit 160a4e6

Browse files
committed
wsutil: Fixed data races involving getters
1 parent 16a408b commit 160a4e6

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

utils/wsutil/conn.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -85,24 +85,26 @@ func (c *Conn) Dial(ctx context.Context, addr string) error {
8585
// BUG which prevents stream compression.
8686
// See https://github.com/golang/go/issues/31514.
8787

88-
conn, _, err := c.dialer.DialContext(ctx, addr, headers)
89-
if err != nil {
90-
return errors.Wrap(err, "failed to dial WS")
91-
}
92-
93-
events := make(chan Event, WSBuffer)
94-
go startReadLoop(conn, events)
88+
var err error
9589

9690
c.mutex.Lock()
9791
defer c.mutex.Unlock()
9892

99-
c.Conn = conn
100-
c.events = events
93+
c.Conn, _, err = c.dialer.DialContext(ctx, addr, headers)
94+
if err != nil {
95+
return errors.Wrap(err, "failed to dial WS")
96+
}
97+
98+
c.events = make(chan Event, WSBuffer)
99+
go startReadLoop(c.Conn, c.events)
101100

102101
return err
103102
}
104103

105104
func (c *Conn) Listen() <-chan Event {
105+
c.mutex.Lock()
106+
defer c.mutex.Unlock()
107+
106108
return c.events
107109
}
108110

0 commit comments

Comments
 (0)