[New-bugs-announce] [issue3476] BufferedWriter not thread-safe

Antoine Pitrou report at bugs.python.org
Thu Jul 31 11:03:12 CEST 2008


New submission from Antoine Pitrou <pitrou at free.fr>:

As discovered in #3139, io.BufferedWriter mutates the size of its
internal bytearray object. Consequently, invocations of write() from
multiple threads can produce exceptions when one thread gets a buffer to
the bytearray while the other thread tries to resize it. This especially
affects calling print() from multiple threads.

A solution is to use a fixed-size (preallocated) bytearray object.
Another solution is to get rid of the bytearray approach and replace it
with a growing list of immutable bytes objects.

Here is the test script provided by Amaury:

import sys, io, threading
stdout2 = io.open(sys.stdout.fileno(), mode="w")
def f(i):
    for i in range(10):
        stdout2.write(unicode((x, i)) + '\n')
for x in range(10):
    t = threading.Thread(target=f, args=(x,))
    t.start()

(with py3k, replace "stdout2.write" with a simple "print")

----------
components: Library (Lib)
messages: 70494
nosy: amaury.forgeotdarc, pitrou
priority: high
severity: normal
status: open
title: BufferedWriter not thread-safe
type: behavior
versions: Python 2.6, Python 3.0

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue3476>
_______________________________________


More information about the New-bugs-announce mailing list