Skip to content

Commit

Permalink
fix: check for port use (#3190)
Browse files Browse the repository at this point in the history
Actually done by goose but hand holding for now

fix for: #3169

Prompt was something like "can you take a look at
#3169 and come up with a fix"
  • Loading branch information
michaelneale authored Oct 25, 2024
1 parent dee85c5 commit bbfeab0
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions frontend/cli/cmd_serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"net"
"net/url"
"os"
osExec "os/exec" //nolint:depguard
Expand Down Expand Up @@ -64,8 +65,18 @@ func (s *serveCmd) Run(ctx context.Context, projConfig projectconfig.Config) err
}

//nolint:maintidx
func isPortAvailable(host string, port string) bool {
ln, err := net.Listen("tcp", net.JoinHostPort(host, port))
if err != nil {
return false
}
ln.Close()
return true
}

func (s *serveCmd) run(ctx context.Context, projConfig projectconfig.Config, initialised optional.Option[chan bool], devMode bool, bindAllocator *bind.BindAllocator) error {
logger := log.FromContext(ctx)

controllerClient := rpc.ClientFromContext[ftlv1connect.ControllerServiceClient](ctx)
provisionerClient := rpc.ClientFromContext[provisionerconnect.ProvisionerServiceClient](ctx)

Expand All @@ -75,6 +86,13 @@ func (s *serveCmd) run(ctx context.Context, projConfig projectconfig.Config, ini
_ = KillBackgroundServe(logger) //nolint:errcheck // ignore error here if the process is not running
}

// Check port availability before starting in background
host := s.Bind.Hostname()
port := s.Bind.Port()
if !isPortAvailable(host, port) {
return fmt.Errorf("port %s is already in use on %s", port, host)
}

if err := runInBackground(logger); err != nil {
return err
}
Expand Down

0 comments on commit bbfeab0

Please sign in to comment.