5
5
"fmt"
6
6
"math/rand"
7
7
"net"
8
+ "os"
9
+ "runtime/debug"
8
10
"strings"
9
11
"time"
10
12
@@ -108,7 +110,14 @@ func applyOpts(options ...ClientOption) clientOpts {
108
110
return conf
109
111
}
110
112
111
- func (c * client ) run (ctx context.Context , params * lookupParams ) error {
113
+ func (c * client ) run (ctx context.Context , params * lookupParams ) (err error ) {
114
+ defer func () {
115
+ if rerr := recover (); rerr != nil {
116
+ fmt .Fprintf (os .Stderr , "caught panic: %s\n %s\n " , rerr , debug .Stack ())
117
+ err = fmt .Errorf ("panic in zeroconf run loop: %s" , rerr )
118
+ }
119
+ }()
120
+
112
121
ctx , cancel := context .WithCancel (ctx )
113
122
done := make (chan struct {})
114
123
go func () {
@@ -118,7 +127,7 @@ func (c *client) run(ctx context.Context, params *lookupParams) error {
118
127
119
128
// If previous probe was ok, it should be fine now. In case of an error later on,
120
129
// the entries' queue is closed.
121
- err : = c .periodicQuery (ctx , params )
130
+ err = c .periodicQuery (ctx , params )
122
131
cancel ()
123
132
<- done
124
133
return err
@@ -165,6 +174,12 @@ var cleanupFreq = 10 * time.Second
165
174
166
175
// Start listeners and waits for the shutdown signal from exit channel
167
176
func (c * client ) mainloop (ctx context.Context , params * lookupParams ) {
177
+ defer func () {
178
+ if rerr := recover (); rerr != nil {
179
+ fmt .Fprintf (os .Stderr , "caught panic: %s\n %s\n " , rerr , debug .Stack ())
180
+ }
181
+ }()
182
+
168
183
// start listening for responses
169
184
msgCh := make (chan * dns.Msg , 32 )
170
185
if c .ipv4conn != nil {
@@ -313,6 +328,12 @@ func (c *client) shutdown() {
313
328
// Data receiving routine reads from connection, unpacks packets into dns.Msg
314
329
// structures and sends them to a given msgCh channel
315
330
func (c * client ) recv (ctx context.Context , l interface {}, msgCh chan * dns.Msg ) {
331
+ defer func () {
332
+ if rerr := recover (); rerr != nil {
333
+ fmt .Fprintf (os .Stderr , "caught panic: %s\n %s\n " , rerr , debug .Stack ())
334
+ }
335
+ }()
336
+
316
337
var readFrom func ([]byte ) (n int , src net.Addr , err error )
317
338
318
339
switch pConn := l .(type ) {
0 commit comments