mnet: fix error handling during listen, to prevent a panic when a socket is taken

This commit is contained in:
Brian Picciano 2019-07-09 19:54:52 -04:00
parent 398d887514
commit b2aa2f73bd

View File

@ -93,21 +93,21 @@ func InstListener(cmp *mcmp.Component, opts ...ListenerOpt) *Listener {
), ),
) )
mrun.InitHook(cmp, func(context.Context) error { mrun.InitHook(cmp, func(ctx context.Context) error {
var err error
cmp.Annotate("proto", lOpts.proto, "addr", *addr) cmp.Annotate("proto", lOpts.proto, "addr", *addr)
var err error
if lOpts.isPacketConn() { 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()) cmp.Annotate("addr", l.PacketConn.LocalAddr().String())
} else { } 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()) cmp.Annotate("addr", l.Listener.Addr().String())
} }
if err != nil {
return merr.Wrap(err, cmp.Context())
}
mlog.From(cmp).Info("listening") mlog.From(cmp).Info("listening")
return nil return nil