Skip to content

Commit c56bf6c

Browse files
authored
Merge pull request #80 from cloudstruct/fix/dial-fail-close-panic
fix: don't panic when calling Close() after Dial() failure
2 parents 6ab8c69 + 5e47c4a commit c56bf6c

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

ouroboros.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,14 @@ func (o *Ouroboros) Dial(proto string, address string) error {
103103

104104
func (o *Ouroboros) Close() error {
105105
// Gracefully stop the muxer
106-
o.muxer.Stop()
106+
if o.muxer != nil {
107+
o.muxer.Stop()
108+
}
107109
// Close the underlying connection
108-
if err := o.conn.Close(); err != nil {
109-
return err
110+
if o.conn != nil {
111+
if err := o.conn.Close(); err != nil {
112+
return err
113+
}
110114
}
111115
return nil
112116
}

ouroboros_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package ouroboros_test
2+
3+
import (
4+
ouroboros "github.com/cloudstruct/go-ouroboros-network"
5+
"testing"
6+
)
7+
8+
// Ensure that we don't panic when closing the Ouroboros object after a failed Dial() call
9+
func TestDialFailClose(t *testing.T) {
10+
oOpts := &ouroboros.OuroborosOptions{}
11+
oConn, err := ouroboros.New(oOpts)
12+
if err != nil {
13+
t.Fatalf("unexpected error when creating Ouroboros object: %s", err)
14+
}
15+
err = oConn.Dial("unix", "/path/does/not/exist")
16+
if err == nil {
17+
t.Fatalf("did not get expected failure on Dial()")
18+
}
19+
// Close Ouroboros connection
20+
oConn.Close()
21+
}

0 commit comments

Comments
 (0)