Skip to content

Commit d94775e

Browse files
jnarebgitster
authored andcommitted
git-instaweb: Wait for server to start before running web browser
Add generic httpd_is_ready subroutine, which busy-waits for web server to be started, by checking if $port is opened on localhost. This is used to avoid situation where web browser is started before web server is ready to accept connection, and fails. It uses IO::Socket::INET module, which is core Perl module since v5.6.0. Alternate solution, possible for those web servers that can run arbitrary code hooks after they bind the listen socket (after they start accepting connections), would be to use some kind of blocking mechanism: FIFO or lockfile, see http://thread.gmane.org/gmane.comp.version-control.git/147337/focus=147566 This can be always added later, as a web server specific branch in httpd_is_ready function. Signed-off-by: Jakub Narebski <[email protected]> Acked-by: Petr Baudis <[email protected]> Acked-by: Eric Wong <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d112762 commit d94775e

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

git-instaweb.sh

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,19 @@ stop_httpd () {
117117
rm -f "$fqgitdir/pid"
118118
}
119119

120+
httpd_is_ready () {
121+
"$PERL" -MIO::Socket::INET -e "
122+
local \$| = 1; # turn on autoflush
123+
exit if (IO::Socket::INET->new('127.0.0.1:$port'));
124+
print 'Waiting for \'$httpd\' to start ..';
125+
do {
126+
print '.';
127+
sleep(1);
128+
} until (IO::Socket::INET->new('127.0.0.1:$port'));
129+
print qq! (done)\n!;
130+
"
131+
}
132+
120133
while test $# != 0
121134
do
122135
case "$1" in
@@ -414,7 +427,7 @@ start_httpd
414427
url=http://127.0.0.1:$port
415428

416429
if test -n "$browser"; then
417-
git web--browse -b "$browser" $url || echo $url
430+
httpd_is_ready && git web--browse -b "$browser" $url || echo $url
418431
else
419-
git web--browse -c "instaweb.browser" $url || echo $url
432+
httpd_is_ready && git web--browse -c "instaweb.browser" $url || echo $url
420433
fi

0 commit comments

Comments
 (0)