[Python-Dev] bytearray and array.array are not thread-safe

"Martin v. Löwis" martin at v.loewis.de
Sun Jul 6 00:28:43 CEST 2008


> Short story: bytearray and array.array by construction allow user code to
> reallocate their internal memory buffer. But a raw pointer to the said buffer
> can also be obtained by another thread, and used after releasing the GIL (for
> CPU-intensive operations like compression). As a consequence, the interpreter
> crashes.
> 
> Was it envisioned? 

I guess this wasn't considered. For t#, there is a comment from
Travis that it really shouldn't release the buffer yet, but it does,
anyway.

I propose that new codes s*, t*, w* are added, and that s#,t#,w# refuses
objects which implement a releasebuffer procedure (alternatively, s# etc
might be removed altogether right away). Users of s* then need to pass
in a Py_Buffer view pointer that gets filled, and need to explicitly
release the buffer. For convenience, it might help if the Py_buffer
structure includes a borrowed PyObject* to the underlying object, along
with a PyBuffer_Release procedure/macro.

Regards,
Martin


More information about the Python-Dev mailing list