[Python-Dev] Segfault
Neal Norwitz
nnorwitz at gmail.com
Tue Aug 21 18:14:37 CEST 2007
On 8/21/07, Hrvoje Nikšić <hrvoje.niksic at avl.com> wrote:
> On Mon, 2007-08-20 at 20:27 +0200, Maciej Fijalkowski wrote:
> > import thread
> >
> > while 1:
> > f = open("/tmp/dupa", "w")
> > thread.start_new_thread(f.close, ())
> > f.close()
>
> file_close inadvertently allows fclose to be called twice on the same
> stdio file. This patch should fix the problem:
The patch is insufficient to prevent all types of crashes that occur
when accessing a file from 2 threads (closing in one and doing
whatever in another). Almost every place that accesses f_fp is a
problem. For example, try changing one of the f.close to f.tell. Or
in the main thread do a write. With all of these changes to the test
above, I was able to crash python with the patch (until I fixed the
uses of f_fp).
For example:
import thread
while 1:
f = open("/tmp/dupa", "w")
thread.start_new_thread(f.close, ())
try:
f.write('a')
f.close()
except: pass
I've attached a patch (against 2.5) that fixes most of the problems,
but there are still a few more. (Search for Py_BEGIN_ALLOW_THREADS
and check for usage of f_fp within the block.) I'm not convinced the
attached patch is good enough though.
I think there might already be a bug report about file not being
thread-safe. (It could have also been socket, but I think Martin
fixed a problem in socket a while ago.)
n
More information about the Python-Dev
mailing list