From b2aa2f73bd27d2784856cfd6cb9b6b2280502528 Mon Sep 17 00:00:00 2001 From: Brian Picciano Date: Tue, 9 Jul 2019 19:54:52 -0400 Subject: [PATCH] mnet: fix error handling during listen, to prevent a panic when a socket is taken --- mnet/mnet.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/mnet/mnet.go b/mnet/mnet.go index 59f1be4..b0ffd66 100644 --- a/mnet/mnet.go +++ b/mnet/mnet.go @@ -93,21 +93,21 @@ func InstListener(cmp *mcmp.Component, opts ...ListenerOpt) *Listener { ), ) - mrun.InitHook(cmp, func(context.Context) error { - var err error - + mrun.InitHook(cmp, func(ctx context.Context) error { cmp.Annotate("proto", lOpts.proto, "addr", *addr) + var err error if lOpts.isPacketConn() { - l.PacketConn, err = net.ListenPacket(lOpts.proto, *addr) + if l.PacketConn, err = net.ListenPacket(lOpts.proto, *addr); err != nil { + return merr.Wrap(err, cmp.Context(), ctx) + } cmp.Annotate("addr", l.PacketConn.LocalAddr().String()) } else { - l.Listener, err = net.Listen(lOpts.proto, *addr) + if l.Listener, err = net.Listen(lOpts.proto, *addr); err != nil { + return merr.Wrap(err, cmp.Context(), ctx) + } cmp.Annotate("addr", l.Listener.Addr().String()) } - if err != nil { - return merr.Wrap(err, cmp.Context()) - } mlog.From(cmp).Info("listening") return nil