liburing: io_uring_prep_cancel Not Returning -ECANCELED for Canceled Operations
Description:
When using io_uring_prep_cancel to cancel an ongoing recvmsg request in a separate thread, the completion queue event (cqe->res) does not always return the expected -ECANCELED code. Instead, it may return a different result, such as the completion result of the operation or another error code, even though the cancellation request was successfully submitted.
This behavior occurs in the following scenario:
- A recvmsg request is submitted to io_uring via io_uring_prep_recvmsg.
- A cancellation request is issued via io_uring_prep_cancel from a separate thread after a brief delay (e.g., 2 seconds).
- The completion event (cqe->res) does not return -ECANCELED, but another code such as a regular result or error, indicating that the request was not properly marked as canceled.
Code Snippet:
io_uring_sqe *sqe = io_uring_get_sqe(&ring);
io_uring_prep_recvmsg(sqe, sock, &msg, 0);
io_uring_submit(&ring);
// After some delay, in a separate thread:
io_uring_sqe *cancel_sqe = io_uring_get_sqe(&ring);
io_uring_prep_cancel(cancel_sqe, sqe, 0);
io_uring_submit(&ring);
//.....
int ret = io_uring_wait_cqe(&ring, &cqe);
if (ret < 0) {
std::cerr << "Error waiting for completion event: " << strerror(-ret) << std::endl;
break;
}
// Check if the request was canceled
if (cqe->res == -ECANCELED) {
std::cout << "Request was successfully canceled" << std::endl;
} else if (cqe->res < 0) {
std::cerr << "I/O operation failed: " << strerror(-cqe->res) << std::endl;
} else {
std::cout << "I/O operation completed successfully with result: " << cqe->res << std::endl;
}
io_uring_cqe_seen(&ring, cqe);
Expected Behavior: When io_uring_wait_cqe receives the completion event for the canceled recvmsg operation, cqe->res should return -ECANCELED, indicating that the request was successfully canceled.
Actual Behavior: In some cases, cqe->res returns a different value, such as the result of the operation or another error code, even though a cancel request was submitted and expected to take effect.
This issue appears to cause confusion when trying to cancel operations, as the result does not correctly reflect that the operation was canceled. Any guidance on why this occurs or whether this is intended behavior would be helpful.
2nd Question/Issue What is the common best-practice approach to cancel ongoing io_uring_wait_cqe blocking-calls if the application needs to be shutdown cleanly.
Do we need to cancel first every single ongoing async SEQ-Request by invoking io_uring_prep_cancel and afterwards call io_uring_queue_exit. I cannot see any other way to to shutdown my application as the im blocked in my thread which blocks on io_uring_wait_cqe. Another thread just would issue io_uring_prep_cancel to make the blocking call exit.
Any suggestion are very appreciated.
Thx,
Rafael
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 18 (7 by maintainers)
Common way to handle EINTR is to retry same operation
@axboe I will deliver a working example as soon as possible. Thx so far!