New submission from Jean-Paul Calderone <exar...@divmod.com>:

Python includes several APIs for manipulating TLS:

  PyAPI_FUNC(int) PyThread_create_key(void);
  PyAPI_FUNC(void) PyThread_delete_key(int);
  PyAPI_FUNC(int) PyThread_set_key_value(int, void *);
  PyAPI_FUNC(void *) PyThread_get_key_value(int);
  PyAPI_FUNC(void) PyThread_delete_key_value(int key);

These are not covered in the threading documentation, though.  In fact,
the threading documentation goes so far as to claim that they /don't/ exist:

    While most thread packages have a way to store “per-thread global
data,” Python’s internal platform independent thread abstraction doesn’t
support this yet.

It would be great to have these APIs covered in the documentation.  One
subtlety in particular tripped me up and took a long time to track down
by reading various parts of the CPython source - when a thread exits,
its TLS values are not destroyed or reclaimed in any way.  On top of
this, the keys used by the TLS APIs are thread IDs, and thread IDs can
be re-used.  This means that a newer thread can see values from an older
thread in TLS, which was extremely surprising to me, led to a very
obscure threading bug.

----------
assignee: georg.brandl
components: Documentation
messages: 86501
nosy: exarkun, georg.brandl
severity: normal
status: open
title: "Thread State and the Global Interpreter Lock" section of the docs 
doesn't cover TLS APIs
type: feature request

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue5840>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to