From c36103d9dfcca11f75a568d5ab9569e28ba35d1e Mon Sep 17 00:00:00 2001 From: Aaron Ball Date: Sat, 28 May 2016 11:07:44 -0600 Subject: Put in msg timeout waiting for ping response Before, occasionally, the server would not respond (usually due to connection flood when used with a scheduler) with a ping response. This caused the get_ping function to infinitely loop, very quickly, causing the cpu to spike and the process to never exit. This commit fixes that behavior by looping no more than 50 times with an empty response before exiting. Also added a "no ping response received" error message to better explain when exiting with failure in this scenario. --- ircmsg.sh | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/ircmsg.sh b/ircmsg.sh index 754d736..a504466 100755 --- a/ircmsg.sh +++ b/ircmsg.sh @@ -24,12 +24,22 @@ portfd='' # matching "PING". Retrieves value of that line (without the PING prefix), and # returns so the corresponding PONG can be sent. # +# Note: This function will timeout when reading from the server 100 times +# function get_ping { local portfd=${1} + local emptymax=50 + local emptycount=0 # get ping - while [[ ${ping:0:4} != 'PING' ]]; do + while [[ ${emptycount} -lt ${emptymax} ]]; do read ping <&${portfd} + + # If first four match PING, break and process + [[ ${ping:0:4} == 'PING' ]] && break + + # If response is empty, increment empty counter + [[ ${ping} == '' ]] && emptycount=$(($emptycount + 1)) done ping=${ping:5} @@ -87,6 +97,11 @@ function main { # Respond to the server with its ping value (ping pong) ping=$(get_ping ${portfd}) + if [[ ${ping} == '' ]]; then + echo "ERROR: No PING response received. Cannot continue." + exit 1 + fi + echo "PONG ${ping}" >&${portfd} # Send message to specified channel -- cgit v1.2.3