[New-bugs-announce] [issue27387] Thread hangs on str.encode() when locale is not set

Josh Purvis report at bugs.python.org
Sat Jun 25 18:03:39 EDT 2016

New submission from Josh Purvis:

This bug manifest itself in at least one very specific situation:

    1. No locale is set on the machine
    2. A file (test1.py) imports a second (test2.py)
    3. The second file (test2.py) calls str.encode() from inside a thread
    4. Running Python 2.7

[Environment with no locale set]:

    # both of these are unset:
    $ echo $LC_CTYPE

    $ echo $LANG        



    import test2


    from threading import Thread

    class TestThread(Thread):
        def run(self):
            msg = 'Error from server: code=000a'
            print msg
            msg = msg.encode('utf-8')

    t = TestThread()

    print 'done'

[Expected behavior]:

    $ python test1.py                                                                         
    Error from server: code=000a

[Actual behavior]: 

    $ python test1.py                                                                         
    Error from server: code=000a
    [script hangs here indefinitely]

Much thanks to Alan Boudreault, a developer of the cassandra-driver Python package, for helping me locate this bug and further narrow it down to the threading module. The above code snippet was copied from his comment on my issue over there (https://datastax-oss.atlassian.net/browse/PYTHON-592).

Another curious behavior is that if you modify test1.py to decode any string prior to the import, it implicitly fixes the issue:


    "any string".decode('utf-8')
    import test2

I realize that one should probably always have a locale set, however, this proved to be very difficult to isolate, especially given that it works if no import occurs or a string is decoded prior to the import.

components: Interpreter Core, Unicode
messages: 269262
nosy: ezio.melotti, haypo, joshpurvis
priority: normal
severity: normal
status: open
title: Thread hangs on str.encode() when locale is not set
type: behavior
versions: Python 2.7

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list