[Python-Dev] Segfault

Hrvoje Nikšić hrvoje.niksic at avl.com
Thu Aug 23 09:38:16 CEST 2007


On Wed, 2007-08-22 at 21:32 -0700, Neal Norwitz wrote:
>         Py_BEGIN_ALLOW_THREADS
>         errno = 0;
> -       ret = _portable_fseek(f->f_fp, offset, whence);
> +       if (f->f_fp != NULL)
> +               ret = _portable_fseek(f->f_fp, offset, whence); 

Doesn't this kind of code retain a race condition?  Since this is an
"allow threads" section that runs with the GIL released, file_close
might acquire the GIL and be running in parallel to this code.  If
file_close sets f_fp to NULL after the "if" condition evaluates, but
before the call to _portable_fseek completes, we still get a segfault.

If I understand the Python threading model, the only reliable way to
resolve this is to protect the PyFileObject operations with a mutex.




More information about the Python-Dev mailing list