No, no loop. My application is network infrastructure based, and does continually try to open, and then close, lots of sockets both to the local machine and any number of remote machines. But it doesn't do this particularly quickly and it's fairly straightforward. It closes everything it opens and normally runs for days on end with no problems. The socket that's hit the error is the "control" socket which stays open permanently.
There is something interesting (in the "unusual" sense) in there though. This control socket reads an XML document. The fileevent handler wants to parse it and then update the application, including the GUI, before dealing with the next one. I found that when a stream of these XML documents come in the GUI blocked up because there was a lot of heavy processing going on without a break. So to fix that I do the reading from the socket, then when I've got the whole document I put the processing of it into an "after 0" call in order to let the event loop get a look in.
Further, during the XML processing and GUI updating I used to have a bunch of [update] calls for various reasons (and not [update idletasks], either). This was before I learned what a terrible idea that is, and yes, the effect was as you'd expect - my XML processing routine allowed the socket handler back in and the XML processing effectively got called re-entrantly. Not knowing any better at the time, my fix for this was to switch off the fileevent handler, do the XML processing, then switch it back on again.
So the flow of the socket's fileevent handler was basically:
gets the incoming line
append to buffer - if the XML isn't yet complete, return
once the XML is complete:
save the fileevent readable script (which I'm in) to a variable
after 0 [list processTheXML $xml]
put the fileevent readable script back
I fully appreciate what a total dog's breakfast of logic this is and it was only yesterday, when I was looking at this issue, that I realised that that mess was still in there. All my [update]s have long gone, and as of yesterday all that fileevent changing has now been removed, as has the "after 0". The XML document is processed inline now and it all seems to work.
I've explained this because that's clearly a use case of the Tcl language which isn't going to be explored too often. :) Hopefully it's that messing about with the fileevent handler which has caused the problem.