TIP 355: Stop Fast Recycling of Channel Names on Unix

EuroTcl/OpenACS 11 - 12 JULY 2024, VIENNA
Author:         Alexandre Ferrieux <[email protected]>
State:          Draft
Type:           Project
Vote:           Pending
Created:        01-Sep-2009
Tcl-Version:    9.1
Tcl-Ticket:     2826430


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.


Tcl (non-reflected) channel names are of the general form $KIND$HANDLE on all OSes, $KIND being something like "file", "pipe", etc, and $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]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.

Proposed Change

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


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, 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...).

Reference Implementation

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


This document has been placed in the public domain.