@@ -68,6 +68,7 @@ type Connection struct {
6868 handshakeVersion uint16
6969 handshakeVersionData protocol.VersionData
7070 doneChan chan interface {}
71+ connClosedChan chan struct {}
7172 waitGroup sync.WaitGroup
7273 onceClose sync.Once
7374 sendKeepAlives bool
@@ -101,7 +102,7 @@ func NewConnection(options ...ConnectionOptionFunc) (*Connection, error) {
101102 c := & Connection {
102103 protoErrorChan : make (chan error , 10 ),
103104 handshakeFinishedChan : make (chan interface {}),
104- doneChan : make (chan interface {}),
105+ connClosedChan : make (chan struct {}),
105106 // Create a discard logger to throw away logs. We do this so
106107 // we don't have to add guards around every log operation if
107108 // a logger is not configured by the user.
@@ -173,12 +174,16 @@ func (c *Connection) DialTimeout(
173174
174175// Close will shutdown the Ouroboros connection
175176func (c * Connection ) Close () error {
176- var err error
177177 c .onceClose .Do (func () {
178+ if c .doneChan == nil {
179+ return
180+ }
178181 // Close doneChan to signify that we're shutting down
179182 close (c .doneChan )
183+ // Wait for connection to be closed
184+ <- c .connClosedChan
180185 })
181- return err
186+ return nil
182187}
183188
184189// BlockFetch returns the block-fetch protocol handler
@@ -237,6 +242,8 @@ func (c *Connection) shutdown() {
237242 if c .muxer != nil {
238243 c .muxer .Stop ()
239244 }
245+ // Close channel to let Close() know that it can return
246+ close (c .connClosedChan )
240247 // Wait for other goroutines to finish
241248 c .waitGroup .Wait ()
242249 // Close consumer error channel to signify connection shutdown
@@ -254,6 +261,7 @@ func (c *Connection) setupConnection() error {
254261 )
255262 }
256263 // Start Goroutine to shutdown when doneChan is closed
264+ c .doneChan = make (chan interface {})
257265 go func () {
258266 <- c .doneChan
259267 c .shutdown ()
0 commit comments