bugst / go-serial

A cross-platform serial library for go-lang.
BSD 3-Clause "New" or "Revised" License
617 stars 188 forks source link

Closing port does not unblock Write #150

Open samherrmann opened 1 year ago

samherrmann commented 1 year ago

Consider the following pseudo code for reading from a serial port:

// 1. Open port.
port, _ := serial.Open("/dev/pts/1", mode)

// 2. Listen to the "done" channel that emits when user presses Ctrl+C.
go func() {
  <-done
  port.Close()
}()

// 3. Read from port.
for {
  buff := make([]byte, 1024)
  // If Close is called while Read is blocked (because it's waiting on data) 
  // then Read returns and we can exit the application.
  if _, err = port.Read(buff); err != nil {
    if isPortClosed(err) {
      return nil
    }
    return err
  }
}

In the above example, all works as expected. Calling Close on the port unblocks Read and the application can proceed as desired.

Now consider a similar setup for writing to the serial port:

port, _ := serial.Open("/dev/pts/2", mode)

go func() {
  <-done
  port.Close()
}()

i =: 0
for {
  // If Close is called while Write is blocked 
  // (because no reader exists on the other side) 
  // then Write does *not* return.
  if _, err = port.Write([]byte(fmt.Sprintf("%v", i))); err != nil {
    if isPortClosed(err) {
      return nil
    }
    return err
  }
  i++
  time.Sleep(time.Microsecond)
}

In this example, if Write is blocked because no reader exists on the other side, then calling Close on the port does not force Write to return and the application cannot exit cleanly.

cmaglie commented 1 year ago

We should probably create a pipe to signal the port close and unlock the write (something similar to the Read call).