--- xvfb-run.orig 2009-03-17 16:34:38.588489000 +0000 +++ xvfb-run.patched 2009-03-17 16:40:56.682709000 +0000 @@ -109,7 +109,7 @@ while :; do case "$1" in - -a|--auto-servernum) SERVERNUM=$(find_free_servernum) ;; + -a|--auto-servernum) SERVERNUM=$(find_free_servernum); AUTONUM="yes" ;; -e|--error-file) ERRORFILE="$2"; shift ;; -f|--auth-file) AUTHFILE="$2"; shift ;; -h|--help) SHOWHELP="yes" ;; @@ -155,13 +155,32 @@ AUTHFILE=$(tempfile -n "$XVFB_RUN_TMPDIR/Xauthority") fi -# Start Xvfb. -MCOOKIE=$(mcookie) -XAUTHORITY=$AUTHFILE xauth add ":$SERVERNUM" "$XAUTHPROTO" "$MCOOKIE" \ - >"$ERRORFILE" 2>&1 -XAUTHORITY=$AUTHFILE Xvfb ":$SERVERNUM" $XVFBARGS $LISTENTCP >"$ERRORFILE" \ - 2>&1 & -XVFBPID=$! +# Start Xvfb +# Loop until we get a match between lock file contents and Xvfb PID +# (this avoids a race condition when another process starts an Xserver). +while true; do + MCOOKIE=$(mcookie) + XAUTHORITY=$AUTHFILE xauth add ":$SERVERNUM" "$XAUTHPROTO" "$MCOOKIE" \ + >"$ERRORFILE" 2>&1 + XAUTHORITY=$AUTHFILE Xvfb ":$SERVERNUM" $XVFBARGS $LISTENTCP \ + >"$ERRORFILE" 2>&1 & + XVFBPID=$! + # Test PID of Xvfb and contents of the X server lock file + # (use numerical comparison to avoid whitespace issues). + # If the display was in use the Xvfb process will die on its own. + if [ "$XVFBPID" -eq "$(/dev/null; then echo "Xvfb failed to start" >&2