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

Most upvoted comments

I’ll spin a patch tomorrow morning.

On Jun 16, 2021, at 5:56 PM, Samuel Williams @.***> wrote:

Oh that makes total sense. Okay, I’m keen to test this when you are ready.

cc @ciconia

— You are receiving this because you commented. Reply to this email directly, view it on GitHub, or unsubscribe.

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.

@isilence You are not providing socket because 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?

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.

At least a binding to a custom C function doing fcntl should do. IIRC it can’t block, so have no clue why it needs timeout. For other fcntl ops, perhaps.

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.c

“ego system”, nice pun!

I meant to say ecosystem 😃

But it sounds weird to me, I’d expect any sane language providing access to such internals and allow easy C binding for low-level libraries. Or is it a library vs lang core thing?

It makes using Python simple and nice!

For example when io_uring_prep_close was added I kept trying to close socket fd that was created in Python and after a week or two of fun times! (new pun) I found out that even though fd was being closed python class would keep that fd open. 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 use recv with MSG_DONTWAIT flag 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.

Do you mean mean why io_uring should honor O_NONBLOCK and other flags? Because they are already supported, no more no less. If compatibility wasn’t a problem, I’d much prefer to leave IOSQE_NONBLOCK and only it controlling the behaviour, but can’t be done.

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”.

What do you mean by “a proper socket”? socket(2)? The whole net stack?

If io_uring implements io_uring_prep_socket+ everyone will start using it is what I mean. Like how there is io_uring_prep_openat* and its such an awesome feature.

What do you mean? who should or going to stick with it?

Look at this for example https://bugs.python.org/issue4761 someone implemented openat* nearly 10 years ago and even today there is no openat* 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:

cqe 2: res=32
cqe 1: res=32

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.

Secondly, I get this weird feeling like if you tell io_uring to ignore O_NONBLOCK it might block your Ruby’s event-manager for a long time/till external IO event happens. It depends how you are handling wait state.

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).

when calling io.read, we simply perform read system call. If it returns EAGAIN, we punt it off to the event loop.

O…, I think I get what you are doing… You simply take the initial socket and try to socket.recv() and only call poll if it blocks, right? I know that’s how it works in old epoll world.

We can certainly have the uring implementation handle EAGAIN too, but this just seems inefficient. We will have to do it anyway for backwards compatibility.

Yes, leave O_NONBLOCK as is and catch EAGAIN and poll it using io_uring_prep_poll_add.

If you are worried about efficiency, don’t! You have already lost efficiency when you call socket.recv() directly and EAGAIN is not triggered as often as you think it does.

Secondly, I get this weird feeling like if you tell io_uring to ignore O_NONBLOCK it 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 socket because 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

It just sounds to me that Ruby did something strange and now we’re trying to patch it up on the kernel side.

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 libmariadb which 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 returns EAGAIN we 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 read to 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 uring resulting in EAGAIN for compatibility.

But I think that the most common use case is going to be expecting uring to perform the read and don’t expect EAGAIN result. 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” read as outlined above via the event loop and IORING_OP_READV.

Why can’t the userspace embrace O_NONBLOCK, not try to workaround it, or require it to not be set. What are particular cases?

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.

Pavel, outside of the patch being a quick’n dirty, the intent is only to ignore O_NONBLOCK if set. We should not be fiddling with RWF_NOWAIT or ditto send/recvmsg flags. Just for per-file state, not per request.

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:

  1. Why can’t the userspace embrace O_NONBLOCK, not try to workaround it, or require it to not be set. What are particular cases?

  2. 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.

Can this be in next Linux 5.13-rc* patch or its more of a 5.14 thing.

@YoSTEALTH, 5.13 is feature closed and will be released pretty soon, so definitely not 5.13

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.

@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 -EAGAIN and polling it to account for both blocking/non-blocking flags. This feature would save many unnecessary round-trips.

Rather then just account for EAGAIN it would be nice if it accounted for “BLOCKING” states like EAGAIN, 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_NONBLOCK flag 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_NONBLOCK

You could add a check like

if original_flags & O_NONBLOCK:
    # raise error saying `O_NONBLOCK` is not supported, invalid or something!

I am not really proud of that solution.

Why not just catch -EGAIN and poll it? io_uring_prep_poll_add(sqe, fd, POLLIN)