@@ -68,6 +68,7 @@ type Connection struct {
68
68
handshakeVersion uint16
69
69
handshakeVersionData protocol.VersionData
70
70
doneChan chan interface {}
71
+ connClosedChan chan struct {}
71
72
waitGroup sync.WaitGroup
72
73
onceClose sync.Once
73
74
sendKeepAlives bool
@@ -101,7 +102,7 @@ func NewConnection(options ...ConnectionOptionFunc) (*Connection, error) {
101
102
c := & Connection {
102
103
protoErrorChan : make (chan error , 10 ),
103
104
handshakeFinishedChan : make (chan interface {}),
104
- doneChan : make (chan interface {}),
105
+ connClosedChan : make (chan struct {}),
105
106
// Create a discard logger to throw away logs. We do this so
106
107
// we don't have to add guards around every log operation if
107
108
// a logger is not configured by the user.
@@ -173,12 +174,16 @@ func (c *Connection) DialTimeout(
173
174
174
175
// Close will shutdown the Ouroboros connection
175
176
func (c * Connection ) Close () error {
176
- var err error
177
177
c .onceClose .Do (func () {
178
+ if c .doneChan == nil {
179
+ return
180
+ }
178
181
// Close doneChan to signify that we're shutting down
179
182
close (c .doneChan )
183
+ // Wait for connection to be closed
184
+ <- c .connClosedChan
180
185
})
181
- return err
186
+ return nil
182
187
}
183
188
184
189
// BlockFetch returns the block-fetch protocol handler
@@ -237,6 +242,8 @@ func (c *Connection) shutdown() {
237
242
if c .muxer != nil {
238
243
c .muxer .Stop ()
239
244
}
245
+ // Close channel to let Close() know that it can return
246
+ close (c .connClosedChan )
240
247
// Wait for other goroutines to finish
241
248
c .waitGroup .Wait ()
242
249
// Close consumer error channel to signify connection shutdown
@@ -254,6 +261,7 @@ func (c *Connection) setupConnection() error {
254
261
)
255
262
}
256
263
// Start Goroutine to shutdown when doneChan is closed
264
+ c .doneChan = make (chan interface {})
257
265
go func () {
258
266
<- c .doneChan
259
267
c .shutdown ()
0 commit comments