From 342a2a5568f82725de038a13ed26d51f897db32e Mon Sep 17 00:00:00 2001 From: Xavier Michelon Date: Tue, 31 Jan 2023 15:35:03 +0100 Subject: [PATCH] fix(GODT-2272): use shorter filename for gRPC file socket. --- README.md | 2 +- internal/frontend/grpc/service.go | 10 +++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index c6903c55..466f514e 100644 --- a/README.md +++ b/README.md @@ -89,6 +89,6 @@ There are now three types of system folders which Bridge recognises: | gluon messages | sata | gluon/backend/store | | Update files | data | updates | | sentry cache | data | sentry_cache | -| Mac/Linux File Socket | temp | bridge_{RANDOM_UUID}.sock | +| Mac/Linux File Socket | temp | bridge{4_DIGITS} | diff --git a/internal/frontend/grpc/service.go b/internal/frontend/grpc/service.go index 5857d348..f65ee84e 100644 --- a/internal/frontend/grpc/service.go +++ b/internal/frontend/grpc/service.go @@ -25,6 +25,7 @@ import ( "errors" "fmt" "io/fs" + "math/rand" "net" "os" "path/filepath" @@ -580,10 +581,17 @@ func (s *Service) monitorParentPID() { func computeFileSocketPath() (string, error) { tempPath := os.TempDir() for i := 0; i < 1000; i++ { - path := filepath.Join(tempPath, fmt.Sprintf("bridge_%v.sock", uuid.NewString())) + path := filepath.Join(tempPath, fmt.Sprintf("bridge%04d", rand.Intn(10000))) // nolint:gosec if _, err := os.Stat(path); errors.Is(err, fs.ErrNotExist) { return path, nil } + + if err := os.Remove(path); err != nil { + logrus.WithField("path", path).WithError(err).Warning("Could not remove existing socket file") + continue + } + + return path, nil } return "", errors.New("unable to find a suitable file socket in user config folder")