Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
6401e567
Commit
6401e567
authored
Nov 10, 2017
by
Serhiy Storchaka
Committed by
GitHub
Nov 10, 2017
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[2.7] bpo-31530: Stop crashes when iterating over a file on multiple threads. (#3672)
parent
1bce4efd
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
52 additions
and
3 deletions
+52
-3
Lib/test/test_file2k.py
Lib/test/test_file2k.py
+32
-0
Misc/NEWS.d/next/Core and Builtins/2017-09-20-18-28-09.bpo-31530.CdLOM7.rst
...ore and Builtins/2017-09-20-18-28-09.bpo-31530.CdLOM7.rst
+4
-0
Objects/fileobject.c
Objects/fileobject.c
+16
-3
No files found.
Lib/test/test_file2k.py
View file @
6401e567
...
...
@@ -652,6 +652,38 @@ class FileThreadingTests(unittest.TestCase):
self
.
f
.
writelines
(
''
)
self
.
_test_close_open_io
(
io_func
)
def
test_iteration_torture
(
self
):
# 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
self
.
_run_workers
(
iterate
,
10
)
def
test_iteration_seek
(
self
):
# 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
self
.
_run_workers
(
iterate
,
10
)
@
unittest
.
skipUnless
(
os
.
name
==
'posix'
,
'test requires a posix system.'
)
class
TestFileSignalEINTR
(
unittest
.
TestCase
):
...
...
Misc/NEWS.d/next/Core and Builtins/2017-09-20-18-28-09.bpo-31530.CdLOM7.rst
0 → 100644
View file @
6401e567
Fixed crashes when iterating over a file on multiple threads.
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.
Objects/fileobject.c
View file @
6401e567
...
...
@@ -430,7 +430,7 @@ 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
...
...
@@ -438,7 +438,7 @@ close_the_file(PyFileObject *f)
* pointer. */
PyErr_SetString
(
PyExc_SystemError
,
"PyFileObject locking error in "
"destructor (refcnt <= 0 at close)
.
"
);
"destructor (refcnt <= 0 at close)"
);
}
return
NULL
;
}
...
...
@@ -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
))
...
...
@@ -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
;
...
...
@@ -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
;
...
...
@@ -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
;
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment