Skip to content

Commit c189949

Browse files
committed
fix: x11vnc race condition with Xvfb, issue #349
This fix adds a new function that will wait until X11 socket is ready, and then start x11vnc. As this can impact start-up scripts that require X environment, the wait_x_socket is used before `X_SCRIPTS` are started.
1 parent a66a334 commit c189949

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

Dockerfile.template

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@ RUN apt-get update -y && \
9999
groupadd --gid ${USER_GID} ibgateway && \
100100
useradd -ms /bin/bash --uid ${USER_ID} --gid ${USER_GID} ibgateway && \
101101
echo "ibgateway ALL=(ALL) NOPASSWD:ALL" | tee -a /etc/sudoers && \
102-
chmod a+x ${SCRIPT_PATH}/*.sh
102+
chmod a+x ${SCRIPT_PATH}/*.sh && \
103+
mkdir -p /tmp/.X11-unix && chmod 1777 /tmp/.X11-unix
103104

104105
USER ${USER_ID}:${USER_GID}
105106
WORKDIR ${HOME}

image-files/scripts/common.sh

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,3 +263,23 @@ start_socat() {
263263
fi
264264

265265
}
266+
267+
wait_x_socket() {
268+
DISPLAY_NUM=${DISPLAY#:}
269+
# wait for X11 socket
270+
if [ -S "/tmp/.X11-unix/X${DISPLAY_NUM}" ]; then
271+
return 0
272+
fi
273+
echo ".> Waiting for Xvfb socket on $DISPLAY..."
274+
timeout=50 # 50 * .2 = 10 seconds max wait
275+
elapsed=0
276+
while [ ! -S "/tmp/.X11-unix/X${DISPLAY_NUM}" ]; do
277+
sleep 0.2
278+
elapsed=$((elapsed + 1))
279+
if [ $elapsed -ge $timeout ]; then
280+
echo "Error: Timed out waiting for Xvfb socket!"
281+
exit 1
282+
fi
283+
done
284+
echo ".> Xvfb socket ready!"
285+
}

image-files/scripts/run.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ echo "*************************************************************************"
1111
# shellcheck disable=SC1091
1212
source "${SCRIPT_PATH}/common.sh"
1313

14+
# shellcheck disable=SC2329
1415
stop_ibc() {
1516
echo ".> 😘 Received SIGINT or SIGTERM. Shutting down IB Gateway."
1617

@@ -54,6 +55,8 @@ start_xvfb() {
5455
}
5556

5657
start_vnc() {
58+
# wait for X11 socket to be ready
59+
wait_x_socket
5760
# start VNC server
5861
file_env 'VNC_SERVER_PASSWORD'
5962
if [ -n "$VNC_SERVER_PASSWORD" ]; then
@@ -120,6 +123,7 @@ start_vnc
120123

121124
# run scripts once X environment is up
122125
if [ -n "$X_SCRIPTS" ]; then
126+
wait_x_socket
123127
run_scripts "$HOME/$X_SCRIPTS"
124128
fi
125129

0 commit comments

Comments
 (0)