Complete Win32 non-blocking implementation#58
Conversation
Fully implement solution for running the libuv default loop without blocking in win32. It is based on Simon Urbanek's 'background' draft package that implements a version of the Rhttpd strategy. Implementation details are in src/httpuv.cpp file.
| TerminateThread(server_thread, 0); | ||
| server_thread = 0; | ||
| if (GetExitCodeThread(thread, &ts) && ts == STILL_ACTIVE) | ||
| TerminateThread(thread, 0); |
There was a problem hiding this comment.
This seems really bad--it's almost never a good idea to forcibly terminate a thread from the outside, with the possible exception of when the entire process is about to shut down. It can leave all sorts of state corrupted. The usual approach is to set a flag that the other thread will check, and if it's important for this thread to not continue until it knows the other thread is dead, to wait/join the other thread (in Windows it looks like WaitForSingleObject will work).
There was a problem hiding this comment.
Thanks. I'll take a look, these lines are straight from 'background' and Rhttpd, so I'll pass along whatever we end up using here to them.
|
Thanks for all the hard work. I know this stuff takes a lot of effort. I don't fully understand the comment in httpuv.cpp; is the libuv loop run on the main thread, or the background thread? |
|
I may also be spending more time on this in the future. It turns out that even in the non-daemonized case, having the libuv loop running on the main thread really hurts I/O performance a lot when R is also busy--it's enough of a problem that some geospatial related features in Leaflet are not worth shipping until we resolve it. |
|
The libuv ends up running in the main thread, through this message passing mechanism. The background thread is only running the 'polling' loop. Again, straight from 'background' and Rhttpd. What do you have in mind? With the existing 'InputHandler' and this win32 message passing mechanisms, the libuv loop still runs in the main R thread. Perhaps use libuv's thread mechanism directly? Ok, thanks, I'll add a better solution to thread stopping in the next day or so. |
|
If the background thread is running a message loop then you should just be On Fri, Feb 12, 2016 at 8:12 AM, hcorrada notifications@github.com wrote:
|
|
@jcheng5 Check this thread exit strategy. Let me know if it can be improved. THanks! |
| int ntimes = 10; | ||
| int i = 0; | ||
|
|
||
| while (i < ntimes && GetExitCodeThread(thread, &ts) && ts == STILL_ACTIVE) { |
There was a problem hiding this comment.
I think you just want WaitForSingleObject(hThread, millis) here.
|
With |
|
Hey @jcheng5, thanks for the pointer! |
|
Hi, @jcheng5, can this be pulled in? |
|
Hi Hector, @hcorrada - I cannot tell if your good work, getting Windows httpuv async communications, R <-> browser, made it into release. Could you let me know? |
|
Don't think so... @jcheng5 maybe take a look after rstudio conference? |
|
Sorry for the long, long delay on this. I just felt really uncomfortable with the Windows implementation using threads and still do. I believe I may have found a much cleaner solution using SetTimer though, this doesn't need a background thread at all: I also need to dive deeply back into httpuv to support asynchronous I/O. I'll try to prioritize Win32 support for daemonized httpuv as well, and getting it to a place where I'm comfortable calling it non-experimental. |
|
Hi Joe,
This is great news! Daemonized httpuv on all platforms would be a real boon.
- Paul
… On Apr 6, 2017, at 10:41 AM, Joe Cheng ***@***.***> wrote:
Sorry for the long, long delay on this. I just felt really uncomfortable with the Windows implementation using threads and still do. I believe I may have found a much cleaner solution using SetTimer though, this doesn't need a background thread at all:
https://github.com/jcheng5/later/blob/master/src/later_win32.cpp
I also need to dive deeply back into httpuv to support asynchronous I/O. I'll try to prioritize Win32 support for daemonized httpuv as well, and getting it to a place where I'm comfortable calling it non-experimental.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub, or mute the thread.
|
|
Thanks Joe! That would be awesome! |
|
I think this is no longer necessary because of #106. |
Using ideas from Simon Urbanek's 'background' draft package that implements the Rhttpd strategy. Implementation description in
src/httpuv.cpp