Skip to content

Commit

Permalink
handle errors properly when preparing socket
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelli321 committed Dec 1, 2024
1 parent d26455f commit 395854a
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions receiver/statsdreceiver/internal/transport/uds_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ func NewUDSServer(transport Transport, socketPath string) (Server, error) {
return nil, fmt.Errorf("NewUDSServer with %s: %w", transport.String(), ErrUnsupportedPacketTransport)
}

if _, err := os.Stat(socketPath); err == nil {
os.Remove(socketPath)
if err := prepareSocket(socketPath); err != nil {
return nil, err
}

conn, err := net.ListenPacket(transport.String(), socketPath)
Expand All @@ -44,3 +44,17 @@ func (u *udsServer) Close() error {
os.Remove(u.packetConn.LocalAddr().String())
return u.packetConn.Close()
}

func prepareSocket(socketPath string) error {
if _, err := os.Stat(socketPath); err == nil {
// File exists, remove it
if err := os.Remove(socketPath); err != nil {
return fmt.Errorf("failed to remove existing socket file: %w", err)
}
} else if !os.IsNotExist(err) {
// Return any error that's not "file does not exist"
return fmt.Errorf("failed to stat socket file: %w", err)
}

return nil
}

0 comments on commit 395854a

Please sign in to comment.