Make Control safe to stop and wait on from any lifecycle state (#1794)

This commit is contained in:
Nate Brown
2026-07-10 10:35:17 -05:00
committed by GitHub
parent 5ecdd4eaa9
commit ab736e4c6b
10 changed files with 441 additions and 58 deletions
+16 -8
View File
@@ -43,12 +43,25 @@ type Service struct {
}
}
func New(control *nebula.Control) (*Service, error) {
wait, err := control.Start()
func New(control *nebula.Control) (_ *Service, reterr error) {
// Check this before Start so a failure doesn't leave a running nebula
device, ok := control.Device().(*overlay.UserDevice)
if !ok {
return nil, errors.New("must be using user device")
}
err := control.Start()
if err != nil {
return nil, err
}
// Anything that fails after a successful Start must tear nebula back down
defer func() {
if reterr != nil {
control.Stop()
}
}()
ctx := control.Context()
eg, ctx := errgroup.WithContext(ctx)
s := Service{
@@ -57,11 +70,6 @@ func New(control *nebula.Control) (*Service, error) {
}
s.mu.listeners = map[uint16]*tcpListener{}
device, ok := control.Device().(*overlay.UserDevice)
if !ok {
return nil, errors.New("must be using user device")
}
s.ipstack = stack.New(stack.Options{
NetworkProtocols: []stack.NetworkProtocolFactory{ipv4.NewProtocol, ipv6.NewProtocol},
TransportProtocols: []stack.TransportProtocolFactory{tcp.NewProtocol, udp.NewProtocol, icmp.NewProtocol4, icmp.NewProtocol6},
@@ -147,7 +155,7 @@ func New(control *nebula.Control) (*Service, error) {
// Add the nebula wait function to the group so a fatal reader error
// propagates out through errgroup.Wait().
eg.Go(func() error {
return wait()
return control.Wait()
})
return &s, nil