Browse Source

Set firstErr before signaling the connection close

pull/1422/head
Steven Allen 5 years ago
parent
commit
9da7d27a46
  1. 13
      p2p/transport/websocket/conn_browser.go

13
p2p/transport/websocket/conn_browser.go

@ -38,7 +38,7 @@ type Conn struct {
dataSignal chan struct{} dataSignal chan struct{}
localAddr net.Addr localAddr net.Addr
remoteAddr net.Addr remoteAddr net.Addr
firstErr error firstErr error // only read this _after_ observing that closeSignal has been closed.
} }
// NewConn creates a Conn given a regular js/wasm WebSocket Conn. // NewConn creates a Conn given a regular js/wasm WebSocket Conn.
@ -134,14 +134,15 @@ func (c *Conn) Write(b []byte) (n int, err error) {
func (c *Conn) Close() error { func (c *Conn) Close() error {
c.closeOnce.Do(func() { c.closeOnce.Do(func() {
c.Call("close") c.Call("close")
c.signalClose() c.signalClose(nil)
c.releaseHandlers() c.releaseHandlers()
}) })
return nil return nil
} }
func (c *Conn) signalClose() { func (c *Conn) signalClose(err error) {
c.closeSignalOnce.Do(func() { c.closeSignalOnce.Do(func() {
c.firstErr = err
close(c.closeSignal) close(c.closeSignal)
}) })
} }
@ -225,11 +226,7 @@ func (c *Conn) setUpHandlers() {
closeHandler := js.FuncOf(func(this js.Value, args []js.Value) interface{} { closeHandler := js.FuncOf(func(this js.Value, args []js.Value) interface{} {
go func() { go func() {
c.signalClose() c.signalClose(errorEventToError(args[0]))
c.mut.Lock()
// Store the error in c.firstErr. It will be returned by Read later on.
c.firstErr = errorEventToError(args[0])
c.mut.Unlock()
c.releaseHandlers() c.releaseHandlers()
}() }()
return nil return nil

Loading…
Cancel
Save