\documentclass[]{article}
\usepackage{amsmath,graphicx,supertabular,hyperref,tabularx,ifthen}
\title{TIP \#361: Releasing Channel Buffers}
\date{February 03, 2010}
\author{Wayne Cuddy}
\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 \#361}&\textbf{Releasing Channel Buffers}\\\hline
Author&

Wayne Cuddy \tipmail{wcuddy}{useunix.net}
\\
Created&Wednesday, \(\text{3}^{\text{rd}}\) February 2010\\
Type&Project\\
Tcl Version&\(8.7\)\\
State&Draft\\
Vote&Pending\\
Version&\tipversion$Revision: 1.3 $\\
Post History&\\
\hline\end{tabularx}\end{center}
\thispagestyle{empty}\pagestyle{empty}
\begin{tipabstract}

Tcl should provide a mechanism by which a channel's output buffer can be released without requiring that Tcl flush any remaining data in the buffer to the operating system. This is of particular interest with output mechanisms which can block indefinitely causing the interpreter to consume unnecessary resources or prevent the interpreter from exiting.
\end{tipabstract}
\tableofcontents\setcounter{page}{0}\clearpage\pagestyle{plain}

\section{Problem}

When working with processes that handle multiplexing/non-blocking I/O it is not uncommon to write, or call \textbf{puts}, with more data than the operating system will accept. Thus Tcl begins to buffer this data using internal buffers at the application level and flushes the data in the background. Problems arise when the consumer of this data, be it the other end of a socket, pipe or FIFO, refuse to read data and do not close the channel.

This has adverse effects on the \textbf{close} function and consequently the interpreter/process when it attempts to flush and close channels during finalization.

\subsection{Consequences}
\begin{enumerate}
\item{}
When \textbf{close} is issued on a non-blocking channel Tcl removes the channel from pool of accessible channels from the current interpreter and attempts to flush any remaining data in the output buffers before closing the channel. Since the other end of the channel is not consuming data Tcl will never be able to flush it's internal buffers nor close the lower level channel driver. In a multiplexing server which opens new channels this will eventually result in resource starvation or denial of service.
\setcounter{enumi}{1}
\item{}
When \textbf{exit} is called Tcl attempts to flush all output buffers, if any open channel blocks the interpreter will be unable exit and must be forcibly terminated by OS specific mechanisms.
\end{enumerate}

\section{Propose API Additions}

Provide a function and/or flags to existing functions which can be used to clear internal buffers without flushing.
\begin{itemize}
\item{}
\textbf{chan clear} \textit{channelId}
\item{}
\textbf{close -noflush} \textit{channelId}
\end{itemize}

\section{Copyright}

This document has been placed in the public domain.

\section{Colophon}

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