diff options
author | Aaron Ball <nullspoon@iohq.net> | 2016-05-28 11:07:44 -0600 |
---|---|---|
committer | Aaron Ball <nullspoon@iohq.net> | 2016-05-28 11:15:22 -0600 |
commit | c36103d9dfcca11f75a568d5ab9569e28ba35d1e (patch) | |
tree | 1966680754c7303115ef4d862cc692827ccaa843 | |
parent | a0efae77e4b573864011edbcbee61cb85c82d343 (diff) | |
download | ircmsg-c36103d9dfcca11f75a568d5ab9569e28ba35d1e.tar.gz ircmsg-c36103d9dfcca11f75a568d5ab9569e28ba35d1e.tar.xz |
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.
-rwxr-xr-x | ircmsg.sh | 17 |
1 files changed, 16 insertions, 1 deletions
@@ -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 |