From 31d4d1e062986dfa774fd044c25fcab45818b361 Mon Sep 17 00:00:00 2001 From: Nikolay Edigaryev Date: Mon, 5 Aug 2024 17:21:06 +0400 Subject: [PATCH] vetu run: do not panic on context cancellation (#55) When context is canceled dhcp.server.Serve() might return io.EOF, which causes the "vetu run" to panic. Ignore any errors that occured after the context cancellation. --- internal/network/software/dhcp/dhcp.go | 2 +- internal/network/software/software.go | 10 +++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/internal/network/software/dhcp/dhcp.go b/internal/network/software/dhcp/dhcp.go index 92d4248..26498b7 100644 --- a/internal/network/software/dhcp/dhcp.go +++ b/internal/network/software/dhcp/dhcp.go @@ -62,7 +62,7 @@ func New(st *stack.Stack, gatewayIP net.IP, vmIP net.IP) (*DHCP, error) { func (dhcp *DHCP) Run(ctx context.Context) error { go func() { <-ctx.Done() - dhcp.server.Close() + _ = dhcp.server.Close() }() return dhcp.server.Serve() diff --git a/internal/network/software/software.go b/internal/network/software/software.go index 0e1f5bf..41c55e6 100644 --- a/internal/network/software/software.go +++ b/internal/network/software/software.go @@ -99,12 +99,20 @@ func New(vmHardwareAddr net.HardwareAddr) (*Network, error) { go func() { if err := gvisor.Run(ctx); err != nil { + if errors.Is(ctx.Err(), context.Canceled) { + return + } + panic(err) } }() go func() { - if err := dhcp.Run(context.Background()); err != nil { + if err := dhcp.Run(ctx); err != nil { + if errors.Is(ctx.Err(), context.Canceled) { + return + } + panic(err) } }()