\documentclass[]{article}
\usepackage{amsmath,graphicx,supertabular,hyperref,tabularx,ifthen}
\title{TIP \#398: Quickly Exit with Non-Blocking Blocked Channels}
\date{February 24, 2012}
\author{Alexandre Ferrieux}
\urlstyle{sf}
\setlength{\parskip}{1ex}
\setlength{\parindent}{0pt}
\def\tipversion$#1${\texttt{\$#1\$}}
\def\tiplangle#1{\ensuremath{<}}
\def\tiprangle#1{\ensuremath{>}}
\def\tipbar#1{\ensuremath{|}}
\def\tipmail#1#2{\(\langle\){\small\expandafter\url{#1@#2}}\(\rangle\)}
\ifx\pdfoutput\undefined
\newcommand{\tipimage}[2]{\typeout{Make sure you download #1.eps}\ifthenelse{\lengthtest{0.8\textwidth>#2}\and\lengthtest{0pt<#2}}{\includegraphics{#1.eps}}{\includegraphics[width=0.8\textwidth]{#1.eps}}}
\newcommand{\tipxref}[1]{}
\newcommand{\tipxrefend}{}
\else
\newcommand{\tipimage}[2]{\typeout{Make sure you create #1.pdf}\ifthenelse{\lengthtest{0.8\textwidth>#2}\and\lengthtest{0pt<#2}}{\includegraphics{#1.pdf}}{\includegraphics[width=0.8\textwidth]{#1.pdf}}}
\pdfcatalog{/PageMode /UseOutlines}
\newcommand{\tipxref}[1]{\pdfannotlink attr {/C [0.5 0.5 1.0] /Border [0 0 1]} goto name {#1}}
\newcommand{\tipxrefend}{\pdfendlink}
\fi
\newenvironment{tipabstract}{\begin{abstract}}{\end{abstract}}
\begin{document}\maketitle
\begin{center}\begin{tabularx}{\linewidth}{|r@{: }X|}\hline
\textbf{TIP \#398}&\textbf{Quickly Exit with Non-Blocking Blocked Channels}\\\hline
Author&

Alexandre Ferrieux \tipmail{alexandre.ferrieux}{gmail.com}
\\
Created&Friday, \(\text{24}^{\text{th}}\) February 2012\\
Type&Project\\
Tcl Version&\(8.6\)\\
State&Final\\
Vote&Done\\
Version&\tipversion$Revision: 1.7 $\\
Keywords&close, exit, flush, blocking, nonblocking\\
Post History&\\
\hline\end{tabularx}\end{center}
\thispagestyle{empty}\pagestyle{empty}
\begin{tipabstract}

This TIP reverts an age-old documented behavior that is useless, and the ill effects of which cannot be circumvented: Tcl's insistence on flushing even non-blocking channels on exit.
\end{tipabstract}
\tableofcontents\setcounter{page}{0}\clearpage\pagestyle{plain}

\section{Rationale}

The close.n manpage says:

\begin{quote}
Channels are automatically closed when an interpreter is destroyed and when the process exits. Channels are switched to blocking mode, to ensure that all output is correctly flushed before the process exits.
\end{quote}

So, assuming the application has been using nonblocking IO all the time to stay responsive even with blocked sockets (network failures) or pipes (stalled consumer), if it decides to give up and exit, it \textit{cannot} do so today: Tcl will switch those channels back to blocking mode, and sit forever trying to flush them, until a kind hand hits \textasciicircum{}C.

No combination of \textbf{close}/\textbf{fconfigure}/\textbf{exit} will do, thanks to the above clarification.

Note that the intent of this behavior is to automagically prevent careless applications from truncating their output. The flaw of this logic is that such careless applications typically don't use nonblocking channels. Hence the generalization is abusive.

Hence, the proposal of this TIP is simply to revert this deleterious choice. This way:
\begin{itemize}
\item{}
simple apps continue to autoflush their blocking channels,
\item{}
smart nonblocking contraptions enjoy a timely exit, and
\item{}
generic code can even guarantee quick death by switching all channels to nonblocking mode, like this:
\begingroup\small\begin{verbatim}
   proc instantdeath {{status 0}} {
       foreach ch [chan names] {fconfigure $ch -blocking 0}
       exit $status
   }
\end{verbatim}
\endgroup
\end{itemize}

\section{Specification}

Nonblocking channels shall no longer be switched to blocking mode when the process calls \textbf{exit} or \textit{Tcl\_Exit()}. Any buffered data for nonblocking channels will be discarded.

\section{Notes}

Blocking channels are unaffected by this TIP; blocking channels shall still be flushed and closed at \textbf{exit}-time

\section{Related Bugs}

Bug 2946474 noted the accidental, non-TIP-warranted suppression of this strange behavior. Fixing this bug simply meant to comply with Day One documentation. This TIP now judges the substance.

\section{Reference Implementation}

Branch tip-398-impl holds the code.

\section{Copyright}

This document has been placed in the public domain.

\section{Colophon}

\textit{TIP AutoGenerator --- written by Donal K. Fellows}
\end{document}
