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

Add cancellation #105

Open
kroppt opened this issue Mar 8, 2021 · 0 comments · May be fixed by #121
Open

Add cancellation #105

kroppt opened this issue Mar 8, 2021 · 0 comments · May be fixed by #121

Comments

@kroppt
Copy link

kroppt commented Mar 8, 2021

It appears all the Go implementations of serial drivers are not asynchronous. This is fine, because in Go you can use goroutine to wait on blocked code. However, cancellation would be expected, but it's not available. Currently, the only way to cancel a Read or Write is to close the serial port, which cancels every operation, rather than the specific operation you want cancelled.

Regarding cancellation on Windows:

  • CancelIoEx function: "The CancelIoEx function allows you to cancel requests in threads other than the calling thread. The CancelIo function only cancels requests in the same thread that called the CancelIo function."
    This is important in Go, because goroutines are scheduled across multiple threads usually, and there is no guarantee which thread a piece of code is currently running on.
  • Canceling Pending I/O Operations: "When canceling asynchronous I/O, when no overlapped structure is supplied to CancelIoEx, the function attempts to cancel all outstanding I/O on the file on all threads in the process."
    This implies that providing the overlapped structure cancels only that I/O operation.
  • WriteFile function (fileapi.h): "The WriteFile function returns when one of the following conditions occur: - An asynchronous handle is being used and the write is occurring asynchronously."
  • According to my testing, when cancelling overlapped IO, GetOverlappedResult returns failure with the ERROR_OPERATION_ABORTED error.

The Unix implementation can be added to easily, since it's already "select"ing over the port handle and a cancellation handle used only when closing the port.

Timeouts would also be possible through system calls, but that is a less ideal, because Go can easily do timeouts on its own. So I think adding cancellation satisfies any need for setting timeouts.

@kroppt kroppt linked a pull request Sep 22, 2021 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants