Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

attempt to remove dead socket on startup #14

Merged
merged 6 commits into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
1.1.1 (TBD):
- Try to remove dead sockets on startup instead of failing immediately if the socket file exists

1.1.0 (Sep 19, 2023):
- Add support for Client/GetTreatment(s)WithConfig operations.
- Add support for Manager operations.
Expand Down
16 changes: 16 additions & 0 deletions cmd/splitcli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/splitio/splitd/splitio/conf"
"github.com/splitio/splitd/splitio/link"
"github.com/splitio/splitd/splitio/link/client/types"
"github.com/splitio/splitd/splitio/link/transfer"
"github.com/splitio/splitd/splitio/util"
)

Expand Down Expand Up @@ -38,6 +39,21 @@ func main() {
VerboseWriter: os.Stderr,
})

if args.Method == "ping" {
// no consumer is created (to avoid registering)
c, err := transfer.NewClientConn(logger, &linkOpts.Transfer)
if err != nil {
logger.Error("error connecting to socket: ", err.Error())
os.Exit(2)
}
if err = c.Shutdown(); err != nil {
logger.Error("error closing connection: ", err.Error())
os.Exit(2)
}
logger.Info("socket is accepting connections properly")
os.Exit(0)
}

c, err := link.Consumer(logger, linkOpts)
if err != nil {
logger.Error("error creating client wrapper: ", err)
Expand Down
2 changes: 1 addition & 1 deletion splitio/commitsha.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package splitio

const CommitSHA = "29ff22d"
const CommitSHA = "1472745"
14 changes: 11 additions & 3 deletions splitio/link/service/v1/clientmgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ func (m *ClientManager) handleClientInteractions() error {
rpc, err := m.fetchRPC()
if err != nil {
if errors.Is(err, io.EOF) { // connection ended, no error
m.logger.Debug(fmt.Sprintf("connection remotely closed for metadata=%+v", m.clientConfig.Metadata))
m.logger.Debug(fmt.Sprintf("connection remotely closed for metadata=%s", formatClientConfig(m.clientConfig)))
return nil
} else if errors.Is(err, os.ErrDeadlineExceeded) { // we waited for an RPC, got none, try again.
m.logger.Debug(fmt.Sprintf("read timeout/no RPC fetched. restarting loop for metadata=%+v", m.clientConfig))
m.logger.Debug(fmt.Sprintf("read timeout/no RPC fetched. restarting loop for metadata=%s", formatClientConfig(m.clientConfig)))
continue
} else {
m.logger.Error(fmt.Sprintf("unexpected error reading RPC: %s. Closing conn for metadata=%+v", err, m.clientConfig))
m.logger.Error(fmt.Sprintf("unexpected error reading RPC: %s. Closing conn for metadata=%s", err, formatClientConfig(m.clientConfig)))
return err
}
}
Expand Down Expand Up @@ -313,3 +313,11 @@ func (m *ClientManager) handleSplits(rpc *protov1.RPC) (interface{}, error) {

return response, nil
}

func formatClientConfig(c *types.ClientConfig) string {
if c == nil {
return "<nil>"
}

return fmt.Sprintf("%+v", c)
}
41 changes: 40 additions & 1 deletion splitio/link/transfer/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"errors"
"fmt"
"net"
"os"
"syscall"
"time"

"github.com/splitio/go-toolkit/v5/logging"
Expand All @@ -29,7 +31,8 @@ const (
)

var (
ErrInvalidConnType = errors.New("invalid conn type")
ErrInvalidConnType = errors.New("invalid conn type")
ErrServiceAddressInUse = errors.New("provided socket file / address is already in use")
)

func NewAcceptor(logger logging.LoggerInterface, o *Options, listenerConfig *AcceptorConfig) (*Acceptor, error) {
Expand All @@ -46,6 +49,10 @@ func NewAcceptor(logger logging.LoggerInterface, o *Options, listenerConfig *Acc
return nil, ErrInvalidConnType
}

if err := ensureAddressUsable(logger, address); err != nil {
return nil, err
}

cf := func(c net.Conn) RawConn { return newConnWrapper(c, ff, o) }
return newAcceptor(address, cf, logger, listenerConfig), nil
}
Expand Down Expand Up @@ -93,3 +100,35 @@ func DefaultOpts() Options {
// helpers

func lpFramerFromConn(c net.Conn) framing.Interface { return framing.NewLengthPrefix(c) }

func ensureAddressUsable(logger logging.LoggerInterface, address net.Addr) error {
switch address.Network() {
case "unix", "unixpacket":
if _, err := os.Stat(address.String()); errors.Is(err, os.ErrNotExist) {
return nil // file doesn't exist, we're ok
}

logger.Warning("The socket file exists. Testing if it's currently accepting connections")
c, err := net.Dial(address.Network(), address.String())
if err == nil {
c.Close()
return ErrServiceAddressInUse
}

logger.Warning("The socket appears to be from a previous (dead) execution. Will try to remove it")

if !errors.Is(err, syscall.ECONNREFUSED) {
return fmt.Errorf("unknown error when testing for a dead socket: %w", err)
}

// the socket seems to be bound to a dead process, will try removing it
// so that a listener can be created
if err := os.Remove(address.String()); err != nil {
return fmt.Errorf("error removing dead-socket file from a previous execution: %w", err)
}

logger.Warning("Dead socket file removed successfuly")

}
return nil
}
54 changes: 53 additions & 1 deletion splitio/link/transfer/setup_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,64 @@
package transfer

import (
"github.com/stretchr/testify/assert"
"net"
"os"
"path/filepath"
"testing"
"time"

"github.com/splitio/go-toolkit/v5/logging"
"github.com/stretchr/testify/assert"
)

func TestConnType(t *testing.T) {
assert.Equal(t, "unix-seqpacket", ConnTypeUnixSeqPacket.String())
assert.Equal(t, "unix-stream", ConnTypeUnixStream.String())
assert.Equal(t, "invalid-socket-type", ConnType(123).String())
}

func TestEnsureAddressIsUsable(t *testing.T) {

logger := logging.NewLogger(nil)
assert.Nil(t, ensureAddressUsable(logger, &net.UDPAddr{}))
assert.Nil(t, ensureAddressUsable(logger, &net.TCPAddr{}))
assert.Nil(t, ensureAddressUsable(logger, &net.UnixAddr{Name: "/some/nonexistent/file"}))

// test unknown error (in this case trying to connect to a different socket type)
ready := make(chan struct{})
path := filepath.Join(os.TempDir(), "splitd_test_ensure_address_usable.sock")
os.Remove(path) // por las dudas
go func() {
l, err := net.ListenUnix("unix", &net.UnixAddr{Name: path, Net: "unix"})
assert.Nil(t, err)
defer l.Close()

l.SetDeadline(time.Now().Add(1 * time.Second))
go func() {
time.Sleep(100 * time.Millisecond)
ready <- struct{}{}
}()
l.Accept()
}()
<-ready
assert.ErrorContains(t, ensureAddressUsable(logger, &net.UnixAddr{Name: path, Net: "unixpacket"}), "unknown error when testing for a dead socket")

// test socket in use error
ready = make(chan struct{})
path = filepath.Join(os.TempDir(), "splitd_test_ensure_address_usable2.sock")
os.Remove(path) // por las dudas
go func() {
l, err := net.ListenUnix("unix", &net.UnixAddr{Name: path, Net: "unix"})
assert.Nil(t, err)
defer l.Close()

l.SetDeadline(time.Now().Add(1 * time.Second))
go func() {
time.Sleep(100 * time.Millisecond)
ready <- struct{}{}
}()
l.Accept()
}()
<-ready
assert.ErrorIs(t, ErrServiceAddressInUse, ensureAddressUsable(logger, &net.UnixAddr{Name: path, Net: "unix"}))
}
2 changes: 1 addition & 1 deletion splitio/version.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package splitio

const Version = "1.1.0"
const Version = "1.1.1"