Skip to content

Commit

Permalink
Merge pull request #80 from cloudstruct/fix/dial-fail-close-panic
Browse files Browse the repository at this point in the history
fix: don't panic when calling Close() after Dial() failure
  • Loading branch information
agaffney authored May 5, 2022
2 parents 6ab8c69 + 5e47c4a commit c56bf6c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
10 changes: 7 additions & 3 deletions ouroboros.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,14 @@ func (o *Ouroboros) Dial(proto string, address string) error {

func (o *Ouroboros) Close() error {
// Gracefully stop the muxer
o.muxer.Stop()
if o.muxer != nil {
o.muxer.Stop()
}
// Close the underlying connection
if err := o.conn.Close(); err != nil {
return err
if o.conn != nil {
if err := o.conn.Close(); err != nil {
return err
}
}
return nil
}
Expand Down
21 changes: 21 additions & 0 deletions ouroboros_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package ouroboros_test

import (
ouroboros "github.com/cloudstruct/go-ouroboros-network"
"testing"
)

// Ensure that we don't panic when closing the Ouroboros object after a failed Dial() call
func TestDialFailClose(t *testing.T) {
oOpts := &ouroboros.OuroborosOptions{}
oConn, err := ouroboros.New(oOpts)
if err != nil {
t.Fatalf("unexpected error when creating Ouroboros object: %s", err)
}
err = oConn.Dial("unix", "/path/does/not/exist")
if err == nil {
t.Fatalf("did not get expected failure on Dial()")
}
// Close Ouroboros connection
oConn.Close()
}

0 comments on commit c56bf6c

Please sign in to comment.