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

Closing port does not unblock Write #150

Open
samherrmann opened this issue Dec 6, 2022 · 1 comment
Open

Closing port does not unblock Write #150

samherrmann opened this issue Dec 6, 2022 · 1 comment
Labels

Comments

@samherrmann
Copy link

samherrmann commented Dec 6, 2022

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
Copy link
Member

cmaglie commented Jan 2, 2023

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

@cmaglie cmaglie added the bug label Jan 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants