util: socketset: Add missing return

If our machine has that pipe busy, don't panic the test. (We do want it
to fail though!)

We're also more careful to check nil object just as a convenience to
help programmers.
This commit is contained in:
James Shubin
2023-09-19 06:49:13 -04:00
parent 6c0775ba59
commit 1b12db92ab
2 changed files with 13 additions and 0 deletions

View File

@@ -83,6 +83,10 @@ func NewSocketSet(groups uint32, name string, proto int) (*SocketSet, error) {
// to the message length. It will block until an event is produced, or shutdown
// is called.
func (obj *SocketSet) ReceiveBytes() ([]byte, error) {
if obj == nil {
return nil, fmt.Errorf("nil object") // caller used this incorrectly
}
// Select will return when any fd in fdSet (fdEvents and fdPipe) is ready
// to read.
_, err := unix.Select(obj.nfd(), obj.fdSet(), nil, nil, nil)
@@ -180,6 +184,10 @@ Loop:
// message to the pipe file descriptor. It must be called before close, and
// should only be called once.
func (obj *SocketSet) Shutdown() error {
if obj == nil {
return fmt.Errorf("nil object") // caller used this incorrectly
}
// close the event socket so no more events are produced
if err := unix.Close(obj.fdEvents); err != nil {
return err
@@ -193,6 +201,10 @@ func (obj *SocketSet) Shutdown() error {
// Close closes the pipe file descriptor. It must only be called after Shutdown
// has closed fdEvents, and unblocked receive. It should only be called once.
func (obj *SocketSet) Close() error {
if obj == nil {
return fmt.Errorf("nil object") // caller used this incorrectly
}
if err := unix.Unlink(obj.pipeFile); err != nil {
return errwrap.Wrapf(err, "could not unbind %s", obj.pipeFile)
}

View File

@@ -114,6 +114,7 @@ func TestShutdown(t *testing.T) {
ss, err := NewSocketSet(0, "pipe.sock", 0)
if err != nil {
t.Errorf("could not create SocketSet: %+v", err)
return
}
// waitgroup for netlink receive goroutine
wg := &sync.WaitGroup{}