Skip to content

Commit 90e9708

Browse files
committed
feat: fail if FIFO does not have a reader
1 parent c70f355 commit 90e9708

1 file changed

Lines changed: 10 additions & 2 deletions

File tree

output/driver-pipe.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@ import (
44
"encoding/binary"
55
"errors"
66
"fmt"
7-
librespot "github.com/devgianlu/go-librespot"
87
"io"
98
"math"
109
"os"
1110
"sync"
11+
"syscall"
12+
13+
librespot "github.com/devgianlu/go-librespot"
1214
)
1315

1416
type pipeOutput struct {
@@ -70,11 +72,17 @@ func newPipeOutput(opts *NewOutputOptions) (out *pipeOutput, err error) {
7072
return nil, fmt.Errorf("unknown output pipe format: %s", opts.OutputPipeFormat)
7173
}
7274

73-
out.file, err = os.OpenFile(opts.OutputPipe, os.O_WRONLY, 0)
75+
// Open the FIFO for writing as non-blocking to cause an error if there is no reader.
76+
out.file, err = os.OpenFile(opts.OutputPipe, os.O_WRONLY|syscall.O_NONBLOCK, 0)
7477
if err != nil {
7578
return nil, fmt.Errorf("failed to open fifo: %w", err)
7679
}
7780

81+
// Restore blocking mode now that we are sure we have a reader.
82+
if err := syscall.SetNonblock(int(out.file.Fd()), false); err != nil {
83+
return nil, fmt.Errorf("failed to set blocking mode on fifo: %w", err)
84+
}
85+
7886
go out.outputLoop()
7987

8088
return out, nil

0 commit comments

Comments
 (0)