Haha. I'm sure they fixed it, just as we're understanding the problem!
I updated the code to take the number of CLOSE_WAIT connections into consideration as well as whether the port is accesible. The default CLOSE_WAITs allowed is five or fewer, but its configurable (at the top of the script are some variables you can tweak, if you feel like it). If you turn on debug, it outputs the connection counts and date every five seconds (or whatever you set check_delay to).
Same drill for updating your script. Launch a terminal (command-space; type terminal; hit enter). Then copy/paste below commands.
download/install new one:
curl http://djw.s3.amazonaws.com/homesharing-keepalive.rb > ~/Documents/homesharing-keepalive.rb && chmod +x ~/Documents/homesharing-keepalive.rb
launch:
~/Documents/homesharing-keepalive.rb
Contents of script:
#! /usr/bin/ruby
debug = false # set to anything but false to get debug output
check_delay = 5 # delay between checks
max_close_waits = 5 # max allowed number of HS connections in a CLOSE_WAIT state
require 'socket'
def is_port_open?(ip, port)
begin
hs = TCPSocket.new(ip, port)
rescue Errno::ECONNREFUSED
return false
end
hs.close
return true
end
def restart_itunes
puts "Home sharing is broken. I am going to restart iTunes."
`osascript -e 'tell application "iTunes" to quit'` # exit iTunes
sleep 10 # give iTunes time to shut down
`open -a iTunes` # start iTunes
sleep 30 # to avoid getting into an iTunes restart loop
puts "iTunes restarted"
end
loop do
num_hs_conns = `netstat -n | grep 3689 | wc -l`.to_i # total HS conns
num_hs_conns_cw = `netstat -n | grep 3689 | grep CLOSE_WAIT | wc -l`.to_i # CLOSE_WAIT HS conns
puts "#{Time.now} :: Number of HS connections: #{num_hs_conns} (CLOSE_WAIT: #{num_hs_conns_cw})" if debug
unless is_port_open?("127.0.0.1", 3689) && num_hs_conns_cw < max_close_waits
puts "#{Time.now} :: Number of HS connections: #{num_hs_conns} (CLOSE_WAIT: #{num_hs_conns_cw})" unless debug
restart_itunes
puts "#{Time.now} :: Number of HS connections: #{num_hs_conns} (CLOSE_WAIT: #{num_hs_conns_cw})" unless debug
end
STDOUT.flush
sleep check_delay
end