Skip to content

[2.7] bpo-31530: Stop crashes when iterating over a file on multiple threads. #3672

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions Lib/test/test_file2k.py
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,38 @@ def io_func():
self.f.writelines('')
self._test_close_open_io(io_func)

def test_iteration_torture(self):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a comment with a least "bpo-31530".

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

# bpo-31530: Crash when concurrently iterate over a file.
with open(self.filename, "wb") as fp:
for i in xrange(2**20):
fp.write(b"0"*50 + b"\n")
with open(self.filename, "rb") as f:
def iterate():
try:
for l in f:
pass
except IOError:
pass
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's probably a better stress test to continue looping even when you get an exception.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes the test tooooooo slow. I have killed it after 8 minutes.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Decrease the size of the file then?

Copy link
Member Author

@serhiy-storchaka serhiy-storchaka Oct 4, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2**12 lines is not enough for provoking a crash, 2**13 to 2**14 lines provoke random crashes, and 2**15 lines is needed for stable crashing. After patching the test with 2**13 lines takes about 40 sec, and 2**15 lines need 3 minutes to finish. This is still too long.

self._run_workers(iterate, 10)

def test_iteration_seek(self):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a comment with a least "bpo-31530".

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

# bpo-31530: Crash when concurrently seek and iterate over a file.
with open(self.filename, "wb") as fp:
for i in xrange(10000):
fp.write(b"0"*50 + b"\n")
with open(self.filename, "rb") as f:
it = iter([1] + [0]*10) # one thread reads, others seek
def iterate():
try:
if next(it):
for l in f:
pass
else:
for i in range(100):
f.seek(i*100, 0)
except IOError:
pass
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

similarly here

self._run_workers(iterate, 10)

@unittest.skipUnless(os.name == 'posix', 'test requires a posix system.')
class TestFileSignalEINTR(unittest.TestCase):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Fixed crashes when iterating over a file on multiple threads.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest to complete the description:

seek() and next() methods of file objects now raise an exception during concurrent operation on the same file object. A lock can be used to prevent the error.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

seek() and next() methods of file objects now raise an exception during
concurrent operation on the same file object.
A lock can be used to prevent the error.
19 changes: 16 additions & 3 deletions Objects/fileobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -430,15 +430,15 @@ close_the_file(PyFileObject *f)
if (Py_REFCNT(f) > 0) {
PyErr_SetString(PyExc_IOError,
"close() called during concurrent "
"operation on the same file object.");
"operation on the same file object");
} else {
/* This should not happen unless someone is
* carelessly playing with the PyFileObject
* struct fields and/or its associated FILE
* pointer. */
PyErr_SetString(PyExc_SystemError,
"PyFileObject locking error in "
"destructor (refcnt <= 0 at close).");
"destructor (refcnt <= 0 at close)");
}
return NULL;
}
Expand Down Expand Up @@ -762,6 +762,12 @@ file_seek(PyFileObject *f, PyObject *args)

if (f->f_fp == NULL)
return err_closed();
if (f->unlocked_count > 0) {
PyErr_SetString(PyExc_IOError,
"seek() called during concurrent "
"operation on the same file object");
return NULL;
}
drop_readahead(f);
whence = 0;
if (!PyArg_ParseTuple(args, "O|i:seek", &offobj, &whence))
Expand Down Expand Up @@ -2238,6 +2244,7 @@ readahead(PyFileObject *f, Py_ssize_t bufsize)
{
Py_ssize_t chunksize;

assert(f->unlocked_count == 0);
if (f->f_buf != NULL) {
if( (f->f_bufend - f->f_bufptr) >= 1)
return 0;
Expand Down Expand Up @@ -2279,6 +2286,12 @@ readahead_get_line_skip(PyFileObject *f, Py_ssize_t skip, Py_ssize_t bufsize)
char *buf;
Py_ssize_t len;

if (f->unlocked_count > 0) {
PyErr_SetString(PyExc_IOError,
"next() called during concurrent "
"operation on the same file object");
return NULL;
}
if (f->f_buf == NULL)
if (readahead(f, bufsize) < 0)
return NULL;
Expand Down Expand Up @@ -2692,7 +2705,7 @@ int PyObject_AsFileDescriptor(PyObject *o)
}
else {
PyErr_SetString(PyExc_TypeError,
"argument must be an int, or have a fileno() method.");
"argument must be an int, or have a fileno() method");
return -1;
}

Expand Down