From 5e47c4ac9b543435bbbcf5f96d2cc944accc87fa Mon Sep 17 00:00:00 2001 From: Andrew Gaffney Date: Wed, 4 May 2022 21:56:41 -0500 Subject: [PATCH] fix: don't panic when calling Close() after Dial() failure Fixes #79 --- ouroboros.go | 10 +++++++--- ouroboros_test.go | 21 +++++++++++++++++++++ 2 files changed, 28 insertions(+), 3 deletions(-) create mode 100644 ouroboros_test.go 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() +}