This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author exarkun
Recipients exarkun, georg.brandl
Date 2009-04-25.12:58:37
SpamBayes Score 1.4685604e-06
Marked as misclassified No
Message-id <1240664318.91.0.855784600353.issue5840@psf.upfronthosting.co.za>
In-reply-to
Content
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.
History
Date User Action Args
2009-04-25 12:58:38exarkunsetrecipients: + exarkun, georg.brandl
2009-04-25 12:58:38exarkunsetmessageid: <1240664318.91.0.855784600353.issue5840@psf.upfronthosting.co.za>
2009-04-25 12:58:37exarkunlinkissue5840 messages
2009-04-25 12:58:37exarkuncreate