Tracing through the code, the loop is caused by the following sequence:

- The TLS negotiation is initiated by the flush
- Negotiation fails and the remote end closes the underlying TCP socket
- Tls_WaitForConnect detects EOF and returns 0 (lines 916-7 in tlsIO.c)
- The return value bubbles up to FlushChannel which treats this as the number
of bytes written (tclIO.c line 2694), decrements the write count by this number (0) and loops around to write again which gets dispatched to Tls_WaitForConnect. Which gets us back to the previous step.