You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Lwt_unix.read on Windows with non-blocking fd seems to block Lwt #574
As a workaround to #569, I recently switched to using non-blocking fds. This worked on Linux and OSX but broke my Windows build. While I'm not sure the cause of this issue, here's what I know and how I worked around the issue. I have not attempted to put together a small repro case yet, because my Windows computer is a potato.
Compiler: 4.03.0+mingw64c
Lwt: 3.3.0
Lwt_ppx: 1.1.0
What my code does:
I have a server, written with Lwt.
It reads an fd using Lwt_unix.read
After the read, it pushes a message to an Lwt_stream.
Another Lwt thread waits on Lwt_stream.next
Behavior with blocking fd
Server reads from fd and pushes to stream. The thread waiting on the stream wakes up and reads from the stream
Behavior with non-blocking fd
Server reads from fd and pushes to stream.
The thread waiting on the stream never wakes up.
If I use Lwt_engine.set, I can confirm that the select method is never being called, even though there are other fds being read.
My fix
If I run Lwt_unix.wait_read fd before the Lwt_unix.read, it fixes my problem.
My best guess
Lwt_unix.readable fd is often false before the Lwt_unix.read call. So my guess is that Lwt_unix.read is effectively a blocking call when using a non-blocking fd, and blocks all of Lwt until the fd is readable and the read call completes.
@gabelevi, on Windows, only sockets can be non-blocking (in the Unix sense). Among other problems, you may have a fd that Lwt thinks is non-blocking, but is actually blocking. What kind of fd do you have, and how are you creating the Lwt_unix.file_descr for it?
The issue seems to be that Lwt_io.read eventually calls Lwt_unix.openfile with O_NONBLOCK. Lwt_unix.openfile calls Unix.openfile on Windows, which ignores the O_NONBLOCK setting (as you mentioned, files can't be non-blocking on Windows).
but since this is Win32 and the file is not a socket (presumably, it is a HANDLE underneath, not SOCKET), and the caller did not supply ~blocking, is_blocking correctly decides that the file is opened in blocking mode, which should cause Lwt to run I/O requests on it in worker threads (to avoid blocking the main thread).
So, something else must be the problem.
The way I would debug this is by inserting prints into both the "user" code and Lwt (as needed), to see what is actually being triggered and what the various values are. I presume you are doing something like this on your end right now. I can also help out, but to resolve this kind of issue, I need to be able to reproduce it, whether in a small case, or if you can provide instructions on what full projects I should build, in what environment, etc.
Ah ya, thanks for the details @aantron - you're right, given that, the issue we faced with esy/esy#539 does not seem to be the same issue here. It sounds like in this case, too, the entire thread is blocked, which is not what we're experiencing here. We still haven't been able to show conclusively that Lwt is to blame at the moment either - I think what would useful for us to see in our repro is if the close method is being called successfully, and at that point, see if there are still any open handles on it.
If the finaliser is called, at least we know nothing is keeping a reference to the Lwt file descriptor, which is what we would expect.
That doesn't mean the underlying OS fd is closed, or the file isn't open through another fd elsewhere, but it does give some info for relatively low effort.
Activity
Work around non-blocking fd issue on Windows.
Work around non-blocking fd issue on Windows.
aantron commentedon Apr 13, 2018
This may have been fixed by 86a6baf, part of #569, which isn't part of a release yet.
The underlying problem was also spotted by @gabelevi, so thanks :)
Work around non-blocking fd issue on Windows.
gabelevi commentedon Apr 16, 2018
Unfortunately, that fix is for blocking fds. This issue is for non-blocking fds
aantron commentedon Apr 16, 2018
@gabelevi, on Windows, only sockets can be non-blocking (in the Unix sense). Among other problems, you may have a fd that Lwt thinks is non-blocking, but is actually blocking. What kind of fd do you have, and how are you creating the
Lwt_unix.file_descrfor it?bryphe commentedon Nov 5, 2018
@aantron , I think we're hitting a similiar issue in
esyin this code path:https://github.com/esy/esy/blob/571f1c28d15752ec960550abfd25eaa7b4ec8b80/esy-lib/Fs.ml#L14
The issue seems to be that
Lwt_io.readeventually callsLwt_unix.openfilewithO_NONBLOCK.Lwt_unix.openfilecallsUnix.openfileon Windows, which ignores theO_NONBLOCKsetting (as you mentioned, files can't be non-blocking on Windows).The
Unix.openfilemethod is implemeneted here for Windows: https://github.com/ocaml/ocaml/blob/db9671f67b47a582b078f1a92e42edfd9f6a0fd7/otherlibs/win32unix/open.c#L44....which is where
O_NONBLOCKis ignored. But I think this is a problematic case for us.cc @andreypopp
aantron commentedon Nov 6, 2018
@bryphe I don't think that the
O_NONBLOCKis the problem:Lwt_io.with_fileeventually does callLwt_unix.openfilewithO_NONBLOCK, and that flag is indeed meaningless (on any system for a regular file).However, for making the
Lwt_unix.file_descr, Lwt's "view" of the fd, it callsLwt_unix.of_unix_file_descr:lwt/src/unix/lwt_unix.cppo.ml
Lines 593 to 595 in 5960580
which is an alias
lwt/src/unix/lwt_unix.cppo.ml
Line 426 in 5960580
for
mk_ch:lwt/src/unix/lwt_unix.cppo.ml
Lines 335 to 344 in 5960580
which calls
is_blockingto guess the blocking mode:lwt/src/unix/lwt_unix.cppo.ml
Lines 290 to 312 in 5960580
but since this is Win32 and the file is not a socket (presumably, it is a
HANDLEunderneath, notSOCKET), and the caller did not supply~blocking,is_blockingcorrectly decides that the file is opened in blocking mode, which should cause Lwt to run I/O requests on it in worker threads (to avoid blocking the main thread).So, something else must be the problem.
The way I would debug this is by inserting prints into both the "user" code and Lwt (as needed), to see what is actually being triggered and what the various values are. I presume you are doing something like this on your end right now. I can also help out, but to resolve this kind of issue, I need to be able to reproduce it, whether in a small case, or if you can provide instructions on what full projects I should build, in what environment, etc.
bryphe commentedon Nov 7, 2018
Ah ya, thanks for the details @aantron - you're right, given that, the issue we faced with esy/esy#539 does not seem to be the same issue here. It sounds like in this case, too, the entire thread is blocked, which is not what we're experiencing here. We still haven't been able to show conclusively that
Lwtis to blame at the moment either - I think what would useful for us to see in our repro is if theclosemethod is being called successfully, and at that point, see if there are still any open handles on it.aantron commentedon Nov 7, 2018
You may be able to get some information by attaching a finalizer to the
Lwt_iochannel (i.e., the argumentLwt_io.with_filepasses to your function) usingGc.finalise, and triggering a garbage collection withGc.full_majorwhen thewith_filepromise resolves. See https://caml.inria.fr/pub/docs/manual-ocaml/libref/Gc.html.If the finaliser is called, at least we know nothing is keeping a reference to the Lwt file descriptor, which is what we would expect.
That doesn't mean the underlying OS fd is closed, or the file isn't open through another fd elsewhere, but it does give some info for relatively low effort.