diff --git a/serial_linux_test.go b/serial_linux_test.go index 219efd5..5eaea0f 100644 --- a/serial_linux_test.go +++ b/serial_linux_test.go @@ -39,3 +39,18 @@ func TestSerialReadAndCloseConcurrency(t *testing.T) { time.Sleep(time.Millisecond * 1) port.Close() } + +func TestDoubleCloseIsNoop(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + cmd := exec.CommandContext(ctx, "socat", "STDIO", "pty,link=/tmp/faketty") + require.NoError(t, cmd.Start()) + go cmd.Wait() + // let our fake serial port node to appear + time.Sleep(time.Millisecond * 100) + + port, err := Open("/tmp/faketty", &Mode{}) + require.NoError(t, err) + require.NoError(t, port.Close()) + require.NoError(t, port.Close()) +} diff --git a/serial_unix.go b/serial_unix.go index c33bca8..2e72473 100644 --- a/serial_unix.go +++ b/serial_unix.go @@ -30,8 +30,9 @@ type unixPort struct { func (port *unixPort) Close() error { if !atomic.CompareAndSwapUint32(&port.opened, 1, 0) { - return &PortError{code: PortClosed} + return nil } + // Close port port.releaseExclusiveAccess() if err := unix.Close(port.handle); err != nil {