[New-bugs-announce] [issue3139] print is not thread safe

Amaury Forgeot d'Arc report at bugs.python.org
Thu Jun 19 12:06:54 CEST 2008

New submission from Amaury Forgeot d'Arc <amauryfa at gmail.com>:

I found this problem when adding "print" statements to multi-threaded
code. When applying the attached diff to a py3k installation, the output
on screen always contains some garbage.

The following code is an extract of fileio_write (in Modules/_fileio.c),
but the same behavior appears everywhere:

	if (!PyArg_ParseTuple(args, "s#", &ptr, &n))
		return NULL;

	errno = 0;
	n = write(self->fd, ptr, n);

io.BufferedWriter calls this function with a bytearray.
In this case, the GIL is released when holding a pointer to the
bytearray data.
But another thread may mutate the bytearray in between, the pointer
becomes stale and can lead to segfaults or funny results.

components: Interpreter Core
files: test_threaded_print.diff
keywords: patch
messages: 68397
nosy: amaury.forgeotdarc
severity: normal
status: open
title: print is not thread safe
type: crash
versions: Python 2.6, Python 3.0
Added file: http://bugs.python.org/file10658/test_threaded_print.diff

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list