Docker + Xvfb + SDL-2.0.10: SDL_PollEvent call bocks

I’m writing nodejs wrapped gui application running in Docker(Ubuntu Linux 20) with Xvfb:

RUN apt-get update && apt-get install -y mesa-utils xvfb libglvnd-dev libgl1-mesa-dev libegl1-mesa-dev libgles2-mesa-dev libgbm-dev mesa-utils-extra xorg-dev libx11-dev libgl1-mesa-glx

xvfb-run node test.js

However, i find that SDL_PollEvent may be in blocking wait status which cause nodejs main loop completely locked.

When i tracked source code of SDL-2.0.10, i found this:

/* Ack!  X11_XPending() actually performs a blocking read if no events available */
static int
X11_Pending(Display * display)
{
...

Just who can tell me how to fix this or bypass it?

My way is a little simpler: i wrap a whole while(SDL_PollEvent(&ev)){…} is a nodejs call, and use setInterval to periodically call it.

node-sdl wrappers a single SDL_PollEvent call to a js function, and do while loop in js, but i doubt if it will run in blocking state in Xvfb virtual X server mode…

From running log output, SDL_PollEvent initially can receive SDL_WINDOWEVENT events,

if i don’t do any event loop, then initial window ui will not come out(I use gstreamer to capture the window and use WebRTC streaming to get encoded picture out)

But later SDL_PollEvent will not get any events, for there is no real input device in Xvfb-run mode?

I try to use SDL_PushEvent to unblock the blocking-state SDL_PollEvent:

    std::thread t([](){
        while(1){
            std::this_thread::sleep_for(std::chrono::seconds(1));
            SDL_Event evt;
            evt.type = SDL_KEYDOWN;
            evt.key.keysym.sym = SDLK_1;
            SDL_PushEvent(&evt);
        }
    });
    t.detach();

but not well working…

SDL_PollEvent() really shouldn’t be blocking.

“Standard” event handling in normal SDL2 apps looks like:

SDL_Event ev;
while(SDL_PollEvent(&ev)) {
    switch(ev.type) {
      // handle event
    }
}
// and now render something
// ...

so if SDL_PollEvent() blocked, this wouldn’t work as the while-loop would never be left to render something.

AFAIK SDL_PollEvent() should be called in the main thread (i.e. the one that created the window), is that the case in your app?
And what are you trying to do anyway? This mixture of node.js and SDL looks pretty wild, to be honest.

I’m writing a service for receiving UI view render commands (in json format), and nodejs is used for WebSocket server and also for starting gstreamer to record the app window and push encoded video stream to a webrtc server/

Please try with the latest SDL code. That X11_Pending() code was removed in x11: Use XCheckIfEvent() instead of XNextEvent() for thread-safety · libsdl-org/SDL@9c95c24 · GitHub