liburing: Handling non-blocking file descriptors consistently.
This is more of a discussion than an actual issue at the moment, but I hope to provide more concrete details as we explore the use cases.
We have an interesting problem regarding IORING_OP_READV/IORING_OP_WRITEV. In a general programming language environment, we don’t always know if the file descriptor is in non-blocking mode or not.
But the behaviour of IORING_OP_READV/IORING_OP_WRITEV depends on it. In non-blocking mode, we can get a result of EAGAIN.
However, this is not very useful in the case of even driven concurrency where we want operations like this to be “asynchronous” “blocking” - i.e. the CQE should not be generated until the read/write could make progress.
In general, my understanding is, we’d need 3-4 system calls to work around this, something like this:
int current_state = get_nonblocking_state(fd);
if (current_state != nonblocking) set_nonblocking_state(fd);
io_uring_read(..., fd, ...)
if (nonblocking_state_was_changed) restore_nonblocking_state(fd, current_state);
We can cache this to reduce the round trips, but it feels like this would be something better solved by a flag on IORING_OP_READV/IORING_OP_WRITEV. In fact, I’m having a hard time imagining any valid use case for non-blocking operation in the uring.
I’ll try to test out specific cases where this is a problem and report back.
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Comments: 107 (75 by maintainers)
Commits related to this issue
- Merge 'reactor: open fd without O_NONBLOCK when using io_uring backend' from Kefu Chai after reading through the issues of liburing project on GitHub, i realized that i misinterpreted the retry behav... — committed to scylladb/seastar by avikivity 4 years ago
- io_uring/poll: allow some retries for poll triggering spuriously If we get woken spuriously when polling and fail the operation with -EAGAIN again, then we generally only allow polling again if data ... — committed to ammarfaizi2/linux-block by axboe 4 years ago
- io_uring/poll: allow some retries for poll triggering spuriously If we get woken spuriously when polling and fail the operation with -EAGAIN again, then we generally only allow polling again if data ... — committed to jessfraz/linux by axboe 4 years ago
- io_uring/poll: allow some retries for poll triggering spuriously commit c16bda37594f83147b167d381d54c010024efecf upstream. If we get woken spuriously when polling and fail the operation with -EAGAIN... — committed to ammarfaizi2/linux-block by axboe 4 years ago
- io_uring/poll: allow some retries for poll triggering spuriously commit c16bda37594f83147b167d381d54c010024efecf upstream. If we get woken spuriously when polling and fail the operation with -EAGAIN... — committed to ammarfaizi2/linux-block by axboe 4 years ago
- io_uring/poll: allow some retries for poll triggering spuriously commit c16bda37594f83147b167d381d54c010024efecf upstream. If we get woken spuriously when polling and fail the operation with -EAGAIN... — committed to ammarfaizi2/linux-block by axboe 4 years ago
- io_uring/poll: allow some retries for poll triggering spuriously commit c16bda37594f83147b167d381d54c010024efecf upstream. If we get woken spuriously when polling and fail the operation with -EAGAIN... — committed to ammarfaizi2/linux-block by axboe 4 years ago
- io_uring/poll: allow some retries for poll triggering spuriously commit c16bda37594f83147b167d381d54c010024efecf upstream. If we get woken spuriously when polling and fail the operation with -EAGAIN... — committed to ammarfaizi2/linux-block by axboe 4 years ago
- io_uring/poll: allow some retries for poll triggering spuriously commit c16bda37594f83147b167d381d54c010024efecf upstream. If we get woken spuriously when polling and fail the operation with -EAGAIN... — committed to ammarfaizi2/linux-block by axboe 4 years ago
- io_uring/poll: allow some retries for poll triggering spuriously commit c16bda37594f83147b167d381d54c010024efecf upstream. If we get woken spuriously when polling and fail the operation with -EAGAIN... — committed to ammarfaizi2/linux-block by axboe 4 years ago
- io_uring/poll: allow some retries for poll triggering spuriously commit c16bda37594f83147b167d381d54c010024efecf upstream. If we get woken spuriously when polling and fail the operation with -EAGAIN... — committed to ammarfaizi2/linux-block by axboe 4 years ago
- io_uring/poll: allow some retries for poll triggering spuriously commit c16bda37594f83147b167d381d54c010024efecf upstream. If we get woken spuriously when polling and fail the operation with -EAGAIN... — committed to ammarfaizi2/linux-block by axboe 4 years ago
- io_uring/poll: allow some retries for poll triggering spuriously commit c16bda37594f83147b167d381d54c010024efecf upstream. If we get woken spuriously when polling and fail the operation with -EAGAIN... — committed to ammarfaizi2/linux-block by axboe 4 years ago
- io_uring/poll: allow some retries for poll triggering spuriously commit c16bda37594f83147b167d381d54c010024efecf upstream. If we get woken spuriously when polling and fail the operation with -EAGAIN... — committed to ammarfaizi2/linux-block by axboe 4 years ago
- io_uring/poll: allow some retries for poll triggering spuriously commit c16bda37594f83147b167d381d54c010024efecf upstream. If we get woken spuriously when polling and fail the operation with -EAGAIN... — committed to ammarfaizi2/linux-block by axboe 4 years ago
- io_uring/poll: allow some retries for poll triggering spuriously commit c16bda37594f83147b167d381d54c010024efecf upstream. If we get woken spuriously when polling and fail the operation with -EAGAIN... — committed to ammarfaizi2/linux-block by axboe 4 years ago
- io_uring/poll: allow some retries for poll triggering spuriously commit c16bda37594f83147b167d381d54c010024efecf upstream. If we get woken spuriously when polling and fail the operation with -EAGAIN... — committed to ammarfaizi2/linux-block by axboe 4 years ago
- io_uring/poll: allow some retries for poll triggering spuriously commit c16bda37594f83147b167d381d54c010024efecf upstream. If we get woken spuriously when polling and fail the operation with -EAGAIN... — committed to ammarfaizi2/linux-block by axboe 4 years ago
- io_uring/poll: allow some retries for poll triggering spuriously commit c16bda37594f83147b167d381d54c010024efecf upstream. If we get woken spuriously when polling and fail the operation with -EAGAIN... — committed to ammarfaizi2/linux-block by axboe 4 years ago
- io_uring/poll: allow some retries for poll triggering spuriously commit c16bda37594f83147b167d381d54c010024efecf upstream. If we get woken spuriously when polling and fail the operation with -EAGAIN... — committed to ammarfaizi2/linux-block by axboe 4 years ago
I’ll spin a patch tomorrow morning.
It would be a pretty trivial change to arm poll rather than return -EAGAIN to the application, if O_NONBLOCK is set on the file. The main issue here is how to inform io_uring of that desired behavior. OP_READV/OP_WRITEV flags are really readv2/writev2 flags, so it’d then be a generic change to allow this. Which arguably would make sense, as the justification would be that you can do blocking IO without having the query/set new file flags.
This applies to more than OP_READV/OP_WRITEV, though. It also applies to SEND/RECV and friends, so it’s actually more of a generic change. Which means that an IOSQE_ flag would make more sense. The only potential issue there is that the flag space is pretty small, we have 8 bits and 6 are already used. Not a huge issue, we have two bits left, and the last bit could always be turned into a “look at extended bits” or something like that.
In any case, let me know if you want to test a patch for this. Should be easy to cook up.
And together with a car you additionally get a nice fruit blender 3000 with a book of ice cream recipes. That’s the problem with such metaphors…
We can also argue why there is no yet another systemd hidden in io_uring, just because. The bottom line, we’re always open to suggestions and requests, but I’d expect some justification.
I just want to chime in that this is a nice addition to io_uring. I got confronted with this problem as well. By definition, as soon as you start using an event polling loop framework, all fds are going to be nonblocking.
In my application, I was, fortunately, able to identify where to remove the O_NONBLOCK flag to respect the existing nonblocking connection semantic while transitioning to blocking io_uring async i/o. It was where the openssl context object is created!
First of all I been up for way too long, so I am running on fumes here.
They do use fcntl and such, I am sure its more to do with backwards compatibility and or bugs. Maybe they are mixing
O_NONBLOCK+ timeout into one. https://github.com/python/cpython/blob/main/Modules/socketmodule.cI meant to say ecosystem 😃
It makes using Python simple and nice!
For example when
io_uring_prep_closewas added I kept trying to close socketfdthat was created in Python and after a week or two of fun times! (new pun) I found out that even thoughfdwas being closed python class would keep thatfdopen. So the solution is to detach it from the class then close. Which someone did implemented and most likely hardly anyone used it. So a “solution” does exist!Who knows what else is being attached to that socket
fd. Like I have to userecvwithMSG_DONTWAITflag or else socket goes into stuck mode. I haven’t had the time to figure out whats actually happening with it. Monkey patch works for now.It makes no sense for liburing to support
O_NONBLOCK(maybe io_uring can use it for internal use). Its like someone attaching a piece of paper to your back saying “Kick Me”.If io_uring implements
io_uring_prep_socket+ everyone will start using it is what I mean. Like how there isio_uring_prep_openat*and its such an awesome feature.Look at this for example https://bugs.python.org/issue4761 someone implemented
openat*nearly 10 years ago and even today there is noopenat*in Python stdlib. Its what bad call mixed with compatibility mess looks like. Everyone lost out on great features cause of it. I am saying lets (io_uring) not follow that path of trying to provide backwards compatibility and look at next 1-20+ years mark where io_uring is actually going to be used heavily.Once you have basic IO features implemented then you can worry about keeping it compatible, not when its still in early development and trying to support feature that isn’t even being used.
I will try this out over the weekend.
I agree, the vast majority of user space code doesn’t care about
EAGAIN, it’s like the half way house of concurrency.I’ve done a basic test, and I’m not seeing EAGAIN any more. However, it’s difficult to compare since I don’t really have a system to reproduce the old behaviour. Thanks everyone, this looks pretty good to me now.
Regular files aren’t pollable and hence not susceptible to this kind of thing. But apart from that, doesn’t matter if it’s a pipe/socket/eventfd etc, it’ll work the same on either one.
How will this work for other types of files: real files, pipes, eventfd etc?
Wrote a quick test for this, and on the current kernels, I do see poll getting armed just fine for a O_NONBLOCK socket. Now I’m curious if we inadvertently “fixed” this, or…? Works regardless of whether or not read or recv is used, here’s my output:
I’ll attach the test app in the next comment.
I don’t have any updates to share. Rather than make this a per-sqe kind of flag, may make more sense to have it as a ring setup (or register enable/disable) flag instead - if we get -EAGAIN and the file is nonblocking, treat it like it wasn’t and still go through the poll mechanism. If the file isn’t pollable, we’d still end the sqe with -EAGAIN as there’s nothing sane we can do at that point.
As long as nothing else is happening, that’s fine. It’s the heart of the event loop. If some other work is scheduled, we set a timeout (which could be 0).
O…, I think I get what you are doing… You simply take the initial
socketand try tosocket.recv()and only call poll if it blocks, right? I know that’s how it works in old epoll world.Yes, leave
O_NONBLOCKas is and catchEAGAINand poll it usingio_uring_prep_poll_add.If you are worried about efficiency, don’t! You have already lost efficiency when you call
socket.recv()directly andEAGAINis not triggered as often as you think it does.Secondly, I get this weird feeling like if you tell io_uring to ignore
O_NONBLOCKit might block your Ruby’s event-manager for a long time/till external IO event happens. It depends how you are handling*wait*state.@isilence You are not providing
socketbecause of highly technical reason but more as a supporting feature (prevent people from making bad choices).Think of io_uring as if you are buying a car. You get the best engine, suspension, frame, but you don’t get a seat with io_uring. And you are telling me its just a seat! you can buy that anywhere, use a stool. Get what I mean? Do you really want to drive around in a best car you can get with a stool as a seat? With no seat belt, not fashioned to the frame?
We can talk about technical reasons at https://github.com/axboe/liburing/issues/234
Adding this flag, whether it’s per-request or per-ring, would help tremendously in integrating io_uring in a whole bunch of “ecosystems” that have established APIs and need to maintain backward-compatibility. And it’s opt-in as @axboe said.
Thanks for all the great discussion.
There are several places where we cannot create our IO. Opaque libraries like
libmariadbwhich internally create sockets and have some (crappy) interfaces for getting the file descriptor out for event driven I/O is one example.Therefore, trying to funnel people into a specific way to construct file descriptors won’t solve this problem generally, although I understand why that might be a nice approach.
For Ruby, it’s my decision to implement non-blocking I/O generally. In Ruby, when we perform I/O operation, like
read, and it returnsEAGAINwe automatically enter event loop. When I/O is blocking, this won’t happen and the thread will block. Both are okay, but we wanted a ubiquitous way to implement event driven concurrency, i.e.read->EAGAIN->wait_readable-> scheduler ->select/epoll/kqueue/uring.But we recently introduced a hook which allows some operations like
readto go directly to the event loop. This is where the problem happens, because our I/O is non-blocking, it can go like this:read-> scheduler ->uring.Within the same process, or even the same file descriptor, in theory it can go through both paths, and even now, the current implementation should handle
uringresulting inEAGAINfor compatibility.But I think that the most common use case is going to be expecting
uringto perform the read and don’t expectEAGAINresult. As discussed, there is a user space cost to this.Personally, the global per-uring flag makes sense to me from the place I’m coming from. I believe it means we can effectively implement “blocking”
readas outlined above via the event loop andIORING_OP_READV.Dealing with EAGAIN in userspace will have a performance cost. The specific use case here which lead to @ioquatix opening this issue is the fact that in Ruby 3.0 sockets are non-blocking: https://bugs.ruby-lang.org/issues/17664.
I get all the WIP thing, it just seems to me that instead of good understandable model, we solve problems by piling up more complexity on top. Even thinking how to describe the whole situation… It will be a normal async execution, unless there is O_NONBLOCK/RWF_NOWAIT/etc, which would make them nonblocking… Well unless it’s specifically O_NONBLOCK and we set IOSQE_IGNORE_NONBLOCK, which would be back normally async. It’s kind of a messy model IMHO.
So, there are questions I have:
Why can’t the userspace embrace O_NONBLOCK, not try to workaround it, or require it to not be set. What are particular cases?
Does io_uring even need to honor O_NONBLOCK? Is there any user caring about it? Because there were always cases where it could punt it somewhere and do not return back even if there is no data. There is IOSQE_ASYNC, that would do exactly that. And remembering unifying RWF_NOWAIT and O_NONBLOCK io_uring specific handling 5.12 or 5.13, so again there should had been some inconsistency.
Funny you mentioned pipe I was just thinking if it would work on pipe as well.
Seems to work for me, just tested it with a pipe. Even if O_NONBLOCK is set, as long as IOSQE_IGNORE_NONBLOCK is set, we don’t return -EAGAIN but rather arm poll and trigger the read once data is posted to the pipe.
@YoSTEALTH, 5.13 is feature closed and will be released pretty soon, so definitely not 5.13
@axboe O.o something like
IOSQE_IO_BLOCKING_POLL*? If you can make this into an feature I would totally welcome it as well. Currently I have lines of code just checking-EAGAINand polling it to account for both blocking/non-blocking flags. This feature would save many unnecessary round-trips.Rather then just account for
EAGAINit would be nice if it accounted for “BLOCKING” states likeEAGAIN, EALREADY, EINPROGRESS, EWOULDBLOCK, ...Each opcode has a specific set of flags, like the RWF_* for read/write, but there’s also a generic set of IOSQE_* flags that apply to any opcode. That handles things like links, etc. Things that sit above the specific request type.
As mentioned, that only then works for the read/write variants, not for any other opcode. Hence I’d be leaning towards making it an sqe flag instead. If it was just applicable to OP_READV and friends, then the RWF approach would be preferable.
Hmm that’s a tricky situation. Monkey patching
O_NONBLOCKflag to not be non-blocking might lead to other problems/bugs.If you have access to when the original flag is being set
original_flags = O_CREAT | O_RDWR | O_NONBLOCKYou could add a check like
I am not really proud of that solution.
Why not just catch
-EGAINand poll it?io_uring_prep_poll_add(sqe, fd, POLLIN)