diff --git a/tensorflow/python/lib/io/file_io_test.py b/tensorflow/python/lib/io/file_io_test.py index af4b2e9dd60..45de79fd0c7 100644 --- a/tensorflow/python/lib/io/file_io_test.py +++ b/tensorflow/python/lib/io/file_io_test.py @@ -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: diff --git a/tensorflow/python/lib/io/file_io_wrapper.cc b/tensorflow/python/lib/io/file_io_wrapper.cc index 0a2410b69e1..54b06d69559 100644 --- a/tensorflow/python/lib/io/file_io_wrapper.cc +++ b/tensorflow/python/lib/io/file_io_wrapper.cc @@ -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);