\documentclass[]{article}
\usepackage{amsmath,graphicx,supertabular,hyperref,tabularx,ifthen}
\title{TIP \#355: Stop Fast Recycling of Channel Names on Unix}
\date{September 01, 2009}
\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 \#355}&\textbf{Stop Fast Recycling of Channel Names on Unix}\\\hline
Author&

Alexandre Ferrieux \tipmail{alexandre.ferrieux}{gmail.com}
\\
Created&Tuesday, \(\text{1}^{\text{st}}\) September 2009\\
Type&Project\\
Tcl Version&\(8.7\)\\
State&Draft\\
Vote&Pending\\
Version&\tipversion$Revision: 1.1 $\\
Post History&\\
\hline\end{tabularx}\end{center}
\thispagestyle{empty}\pagestyle{empty}
\begin{tipabstract}

This TIP proposes to put an end to the unix-specific habit of naming channels after the underlying file descriptor, by using a much longer-lived incremented counter instead.
\end{tipabstract}
\tableofcontents\setcounter{page}{0}\clearpage\pagestyle{plain}

\section{Background}

Tcl (non-reflected) channel names are of the general form \$\textit{KIND}\$\textit{HANDLE} on all OSes, \$\textit{KIND} being something like ``\textbf{file}'', ``\textbf{pipe}'', etc, and \$\textit{HANDLE} being the OS file descriptor or handle. This is clearly a cost-effective way of guaranteeing process-wide unicity at any given time.

However, on unix, file descriptors are in a ``compact table'', i.e. they are small integers that are reused as quickly as possible, to keep the range small for efficiency reasons (and also constraints like the select() API). And as witnessed by [Bug 2826430][\url{https://sourceforge.net/support/tracker.php?aid=2826430}], channel name recycling is dangerous. Quite possibly a bunch of applications running today get occasionally hit, with very hard to decipher symptoms, and an even harder to reproduce setup.

\section{Proposed Change}

This TIP proposes to replace the file descriptor in unix channel names by an ever-increasing, process-unique counter.

\section{Rationale}

This change would bring unix channels in line with the rest of the crowd, since Windows handles seem to have a very long cycle, and reflected channels already use a counter. It could even be more ambitious in that (1) even Windows channels use the counter instead of relying on the OS's lifetime guarantee, and (2) reflected channels use the same counter instead of their own. Choice left open for discussion.

The implementation is trivial: a new public function, \textbf{Tcl\_GetProcessUniqueNum}, returns a global integer counter which is incremented under mutex. (It will wrap at MAXINT and I am listening to whoever thinks it is still a problem...).

\section{Reference Implementation}

The patch attached to the aforementioned bug [\url{https://sourceforge.net/support/tracker.php?aid=2826430}] adds this stub entry, and updates all unix channel naming schemes to use it.

\section{Copyright}

This document has been placed in the public domain.

\section{Colophon}

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