diff --git a/ouroboros.go b/ouroboros.go index ab079d4d..b6e836b6 100644 --- a/ouroboros.go +++ b/ouroboros.go @@ -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 } diff --git a/ouroboros_test.go b/ouroboros_test.go new file mode 100644 index 00000000..77207368 --- /dev/null +++ b/ouroboros_test.go @@ -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() +}