Reacquire the GIL when handing errors in the read method of file_io's BufferedInputStream.

PiperOrigin-RevId: 316120310
Change-Id: I905e2c8652b977c154618c2e4d870dd2925a7234
This commit is contained in:
A. Unique TensorFlower 2020-06-12 09:43:33 -07:00 committed by TensorFlower Gardener
parent 588854df78
commit 0bf070c54d
2 changed files with 13 additions and 1 deletions

View File

@ -389,6 +389,18 @@ class FileIoTest(test.TestCase):
self.assertEqual("t", f.read(1))
self.assertEqual("esting3\n\ntesting5", f.read())
def testReadErrorReacquiresGil(self):
file_path = os.path.join(self._base_dir, "temp_file")
with file_io.FileIO(file_path, mode="r+") as f:
f.write("testing1\ntesting2\ntesting3\n\ntesting5")
with self.assertRaises(errors.InvalidArgumentError):
# At present, this is sufficient to convince ourselves that the change
# fixes the problem. That is, this test will seg fault without the change,
# and pass with it. Unfortunately, this is brittle, as it relies on the
# Python layer to pass the argument along to the wrapped C++ without
# checking the argument itself.
f.read(-2)
def testTell(self):
file_path = os.path.join(self._base_dir, "temp_file")
with file_io.FileIO(file_path, mode="r+") as f:

View File

@ -229,7 +229,7 @@ PYBIND11_MODULE(_pywrap_file_io, m) {
const auto status = self->ReadNBytes(bytes_to_read, &result);
if (!status.ok() && !tensorflow::errors::IsOutOfRange(status)) {
result.clear();
tensorflow::MaybeRaiseRegisteredFromStatus(status);
tensorflow::MaybeRaiseRegisteredFromStatusWithGIL(status);
}
py::gil_scoped_acquire acquire;
return py::bytes(result);