Repository navigation
Description
I have been debugging a hang in my project. I'll try and describe the symptoms and what I think is happen.
Symptoms
- When I run my test suite, it runs my binary hundreds of times. On most test suite runs, one or two tests get stuck
- I have figured out that the binary gets stuck after a
Lwt_unix.writecall to a pipe. I have confirmed that the fd is ready to be written and someone is listening on the other side. - When I
stracea stuck binary, I observe the main thread callingselect, waiting for various fds to be readable. I also observe a single system thread worker, performing a blockingreadcall. - My binary is a server. I can get the binary unstuck by sending it a request.
- Upon receiving the request, the binary creates another system thread worker and finally performs the
writecall.
My guess at a cause
(I'm assuming async_detach since that's the default and I don't ever specify an async method)
I poked around in lwt_unix_stubs.c a bit. I'm guessing I'm hitting a deadlock that looks like this:
- The binary starts with 0 system threads
- The
readsystem call either A) sees 0 system threads and creates a new one, or B) sees 1 free system thread and adds itself to the pool - Before the system thread starts on the
readand can decrementthread_waiting_count, the main thread adds thewriteto the pool too - The system thread starts on the
readjob and blocks indefinitely - The
writejob is stuck in the pool - When I send a request to get it unstuck, the request sends another
writecommand, which creates a second system thread. In my strace, I see twowritesystem calls on the new system thread - one to an fd and one to eventfd - The new second system thread runs the blocked
write, and the binary is now unhung
Things to do
Avoid long blocking system calls
Before lwt, I would use the select & read pattern. With Lwt, I've just been using read. Perhaps I should always use wait_read and wait_write before a read or write, to avoid tying up a system thread for a long time.
If this is a best practice, it might be worth documenting too.
Fix the deadlock
Perhaps the number of jobs in the pool_queue should never exceed thread_waiting_count. If we assume that every job might take a Very Long Time, then putting N+1 jobs in the queue when there are only N free system workers will block that last job for a Very Long Time
So we could do something like
- Keep a
pool_queue_size - If
pool_queue_size >= thread_waiting_count || thread_waiting_count == 0, create a new system thread
Activity
gabelevi commentedon Mar 30, 2018
It's worth mentioning that all the fds I'm using are blocking. Switching to using non-blocking fds would probably also help. Might also explain why I don't see other reports of this behavior.
aantron commentedon Mar 30, 2018
I haven't fully understood the issue yet, but I want to immediately note that:
wait_writebefore a blocking write, so it should not be necessary for the user to do that.gabelevi commentedon Mar 30, 2018
Unfortunately, my project has been incrementally adopting Lwt. So the process which creates the pipe and spawns the lwt-using-server doesn't use Lwt. So we have a blocking pipe. (Which I can fix with
Unix.set_nonblockYep, you're right! Interestingly enough, the
wait_readbefore a blocking read was commented out back in October (c285495). Do you remember why?aantron commentedon Mar 30, 2018
If I understood correctly, at that time when the binary gets stuck, you would expect to see the main thread and two system threads, not one, with one system thread waiting in
read, and the other tasked with performing a potentially-blockingwrite(which we hope doesn't block because Lwt, IIRC, didwait_writefirst). However, thereadandwritetask are "racing" for the same thread from the thread pool, due to a race condition in how the free threads count is managed. As a result, thewriteis never initiated, and if there is nothing to read either, the server is stuck forever. Is that a correct understanding?gabelevi commentedon Mar 30, 2018
Yep! That's exactly what I think is happening!
aantron commentedon Mar 30, 2018
No problem. You may want to set it to non-blocking mode as a quick workaround, but if this is accurate, then this is a classic type of bug that definitely needs to be fixed on the Lwt side as well :)
I don't remember why that was commented off the top of my head. Have to look into it. Given I have a rule never to leave any code commented without commenting why, I suspect that was supposed to be temporary for debugging, and I missed restoring it during self-review.
aantron commentedon Mar 30, 2018
Also, to make it non-blocking from Lwt's point of view, you should use the functions in
Lwt_unix. If you are usingof_unix_file_descr, I think that can be ensured with~blocking:false ~set_flags:true. I also recommend checkingLwt_unix.blockingon the fd, to see if Lwt thinks it is blocking or not. One source of problems is when you have a blocking fd that Lwt thinks is non-blocking, and Lwt happily blocks on it in the main thread. Given your description, though, I don't think that is happening.aantron commentedon Mar 30, 2018
...and this suggests another potential workaround, to try Lwt 3.1.0 for the time being. It should have this bug and/or the even more incorrect thread count management Lwt had last year, but Lwt actually doing
wait_readwill make it very unlikely thatreadwill block in the worker thread, creating this condition. You could also callwait_readmanually for now.I will look into this issue in detail shortly :)
aantron commentedon Mar 30, 2018
...and, confirmed, that call shouldn't be commented out. I remember clearly now, that I did that in order to be able to debug the other issue (linked to the commit).
However, Lwt should work whether
wait_readis commented or not, just not as well when it is commented. So, there is still the underlying issue exposed by commenting it.Lwt_unix: fix race in worker thread count
aantron commentedon Mar 31, 2018
@gabelevi Your analysis was correct. See the attached commit.
Can you please pin Lwt to
https://github.com/ocsigen/lwt.git#569and see if that fixes your tests? That branch is based on Lwt 4.0.0, which isn't merged into opam yet, but it should be backwards-compatible with 3.x.x, except for the previously-announced breaking changes (#453).The short description of the problem is that worker threads were decrementing the idle thread count, after being awakened from the main thread. The main thread should instead itself decrement the idle worker count, to ensure thread wakeup and counting is done atomically.
With the previous code, when the main thread saw one worker idle, it would awaken it. The worker would now be racing with the rest of the main thread, which was going on to submit another job to the thread pool. If this next job submission happened before the worker thread started running, the main thread would still see the count say that one worker thread is idle, even though that would not be the case.
I was able to reproduce this (or a similar) issue on my end with this program:
This sometimes exits after a successful
write, but it usually hangs.It's difficult to make this into a permanent test because I am going to fix the commented-out
wait_readnext, which will make this race condition difficult to reproduce. There are no otherLwt_unixcalls that we can use to reliably block in a worker, so I'll probably need to later write a custom one for that usingLwt_unix's bindings APIs.I didn't fully fix the code for
Async_switch, because that is deprecated.Switch to using nonblocking fds with lwt
Switch to using nonblocking fds with lwt
Lwt_unix.read: restore readability check
Work around non-blocking fd issue on Windows.
Work around non-blocking fd issue on Windows.
Work around non-blocking fd issue on Windows.