This problem is still happening. It's definitely an active bug with tcp connections being opened but never closed but I can't tell if the bug is within the AppleTV code, iTunes code, or the iTunes server operating system code. I've been fighting this for months now with no resolution, trying everything, and my only relief is a crude applescript hack.
Recap:
I run 4 AppleTVs all connected via ethernet to my home LAN which connect to iTunes running on the latest model mac mini for music and videos. The mac mini and all the AppleTVs are all up to date on the latest system updates and patches. As soon as I log into the mac mini and start iTunes, all 4 AppleTVs perform fabulously. However, over time the number of active tcp sessions on the server slowly creeps up (even when the AppleTVs are completely dormant) until the server won't accept new connections anymore. At this point, home sharing stops working completely for any devices trying to connect to the mac mini.
User Experience:
Start iTunes on mac mini at 2pm. 2:10pm play home sharing music on one AppleTV - works great. 2:20pm play a home sharing video on another AppleTV - works great. 3pm browse shared library on 3rd AppleTV - works great. 5pm try to browse shared library on any AppleTV - fails. Try browsing shared library on iPhone - fails. Everything fails until you quit iTunes on server and restart. Quitting home sharing on iTunes and restarting home sharing also works, but I find that even more cumbersome.
Proposed fixes:
- Disable ipv6. You can do this under system -> network or via terminal command line using "networksetup -setv6off Ethernet". I've done this both ways and it doesn't resolve anything for me.
- Configure server to log you out after x minutes of inactivity. This will certainly work to quit iTunes and kill all the zombie tcp connections but it also logs you out so iTunes and home sharing isn't available to AppleTV clients anymore. Not a good user experience.
- Change your AppleTV ip address to manual entry and add a 5 to the end of it. This doesn't make any sense. Changing the ip address on your AppleTV isn't going to fix the number of zombie tcp connections on your iTunes server.
AppleScript hack:
The only workaround that I have found to work is to run an AppleScript hack every 30 minutes (via crontab/launchd) that simply counts the number of active tcp connections and if they exceed an arbitrary threshold, then quit and restart iTunes to clear the tcp connections back to zero. Here is the applescript:
#Capture and log how many tcp connections are active
set LogFile to "/Users/MediaServer/Documents/iTunesRestart.log"
set HowManySessionsA to do shell script "netstat -n | grep 254.6 | wc -l"
set HowManySessionsB to do shell script "netstat -n | grep .3689 | wc -l"
set HowManySessionsC to do shell script "netstat -n | grep .49152 | wc -l"
set HowManySessionsD to do shell script "netstat | grep daap | wc -l"
set HowManySessionsE to do shell script "netstat -n | grep 443 | wc -l"
set theLine to (do shell script "date +'%Y-%m-%d %H:%M:%S'" as string) & " Found " & HowManySessionsA & " " & HowManySessionsB & " " & HowManySessionsC & " " & HowManySessionsD & " " & HowManySessionsE & " AppleTV connections!"
do shell script "echo " & theLine & " >> " & LogFile
#Logic to decide whether or not to restart iTunes
set RestartItunes to 0
if HowManySessionsA > " 60" then set RestartItunes to 1
if HowManySessionsB > " 60" then set RestartItunes to 1
if HowManySessionsC > " 60" then set RestartItunes to 1
if HowManySessionsD > " 60" then set RestartItunes to 1
if HowManySessionsE > " 60" then set RestartItunes to 1
if RestartItunes > 0 then
#Restart iTunes
quit application "iTunes"
delay 20
tell application "iTunes"
activate
end tell
#Email notification
set recipientName to "Me"
set recipientAddress to "me@gmail.com"
set theSubject to "Restarting iTunes"
set theContent to "I am restarting iTunes now. The max is 60 connections and I found " & HowManySessionsA & " " & HowManySessionsB & " " & HowManySessionsC & " " & HowManySessionsD & " " & HowManySessionsE
tell application "Mail"
##Create the message
set theMessage to make new outgoing message with properties {subject:theSubject, content:theContent, visible:true}
##Set a recipient
tell theMessage
make new to recipient with properties {name:recipientName, address:recipientAddress}
##Send the Message
send
end tell
end tell
#Log the iTunes restart
set theLine to (do shell script "date +'%Y-%m-%d %H:%M:%S'" as string) & " Restarting iTunes now"
do shell script "echo " & theLine & " >> " & LogFile
end if
Logging result:
2012-08-14 23:30:35 Found 0 15 0 15 2 AppleTV connections!
2012-08-15 00:00:35 Found 0 25 0 25 2 AppleTV connections!
2012-08-15 00:30:35 Found 0 33 0 33 2 AppleTV connections!
2012-08-15 01:00:35 Found 0 42 0 42 2 AppleTV connections!
2012-08-15 01:30:35 Found 0 50 0 50 2 AppleTV connections!
2012-08-15 02:00:34 Found 0 57 0 57 2 AppleTV connections!
2012-08-15 02:30:34 Found 0 65 0 65 2 AppleTV connections!
2012-08-15 02:31:16 Restarting iTunes now
2012-08-15 03:00:35 Found 0 0 21 0 4 AppleTV connections!
2012-08-15 03:30:35 Found 0 0 33 0 2 AppleTV connections!
2012-08-15 04:00:35 Found 0 0 57 0 2 AppleTV connections!
2012-08-15 04:30:35 Found 0 0 78 0 2 AppleTV connections!
2012-08-15 04:30:56 Restarting iTunes now
2012-08-15 05:00:34 Found 0 27 0 27 2 AppleTV connections!
2012-08-15 05:30:35 Found 0 48 0 48 2 AppleTV connections!
2012-08-15 06:00:34 Found 0 72 0 72 2 AppleTV connections!
2012-08-15 06:00:56 Restarting iTunes now
2012-08-15 06:30:35 Found 0 0 24 0 2 AppleTV connections!
2012-08-15 07:00:35 Found 0 0 51 0 2 AppleTV connections!
2012-08-15 07:30:35 Found 0 0 72 0 2 AppleTV connections!
2012-08-15 07:30:57 Restarting iTunes now
2012-08-15 08:00:34 Found 0 24 0 24 2 AppleTV connections!
2012-08-15 08:30:34 Found 0 42 0 42 2 AppleTV connections!
2012-08-15 09:00:34 Found 0 66 0 66 2 AppleTV connections!
2012-08-15 09:00:56 Restarting iTunes now
2012-08-15 09:30:34 Found 0 0 24 0 2 AppleTV connections!
2012-08-15 10:00:35 Found 0 0 54 0 2 AppleTV connections!
Final Word:
It's not always port 3289/daap that is being zombied. Often it is 49152 so you have to check for both. There is no good reason that between 2:30am and 4:30am when NOBODY in my house is streaming anything that the number of active/zombie tcp connections on iTunes should rise from 21 to 33 to 57 to 78 and then my applescript hack restarts iTunes. This is ridiculous.