diff options
author | Luis Pabon <lpabon@redhat.com> | 2013-11-20 16:31:46 -0500 |
---|---|---|
committer | Luis Pabon <lpabon@redhat.com> | 2013-11-22 09:31:50 -0800 |
commit | 7095f1c6540612be7df25ece8f6f141b6089a844 (patch) | |
tree | db16881dae6d2bf26a40b544876273605287fb28 | |
parent | f4c0ab5b986c8caa912b2dd5037351a5a505428d (diff) |
Fix double close issue in diskfile
The DiskWriter was closing the file descriptor when it finished
writing but initializing it to None. Therefore, the DiskFile
context manager would then try to close it also.
Change-Id: I188ec814d025e28c7b89532f0502ebf1d4a20a09
Signed-off-by: Luis Pabon <lpabon@redhat.com>
Reviewed-on: http://review.gluster.org/6317
Reviewed-by: Peter Portante <pportant@redhat.com>
Reviewed-by: pushpesh sharma <psharma@redhat.com>
Tested-by: pushpesh sharma <psharma@redhat.com>
-rw-r--r-- | gluster/swift/obj/diskfile.py | 16 | ||||
-rw-r--r-- | test/unit/obj/test_diskfile.py | 22 |
2 files changed, 30 insertions, 8 deletions
diff --git a/gluster/swift/obj/diskfile.py b/gluster/swift/obj/diskfile.py index 41fe6c6..fdfabb5 100644 --- a/gluster/swift/obj/diskfile.py +++ b/gluster/swift/obj/diskfile.py @@ -297,6 +297,14 @@ class DiskWriter(object): self.last_sync = 0 self.threadpool = threadpool + def close(self): + """ + Close the file descriptor + """ + if self.fd: + do_close(self.fd) + self.fd = None + def write(self, chunk): """ Write a chunk of data into the temporary file. @@ -407,7 +415,7 @@ class DiskWriter(object): break # Close here so the calling context does not have to perform this # in a thread. - do_close(self.fd) + self.close() def put(self, metadata, extension='.data'): """ @@ -762,11 +770,7 @@ class DiskFile(SwiftDiskFile): dw = DiskWriter(self, fd, tmppath, self.threadpool) yield dw finally: - try: - if dw.fd: - do_close(dw.fd) - except OSError: - pass + dw.close() if dw.tmppath: do_unlink(dw.tmppath) diff --git a/test/unit/obj/test_diskfile.py b/test/unit/obj/test_diskfile.py index dc2c2fa..8869cce 100644 --- a/test/unit/obj/test_diskfile.py +++ b/test/unit/obj/test_diskfile.py @@ -32,7 +32,7 @@ from swift.common.exceptions import DiskFileNotExist, DiskFileError, \ from gluster.swift.common.exceptions import GlusterFileSystemOSError import gluster.swift.common.utils import gluster.swift.obj.diskfile -from gluster.swift.obj.diskfile import DiskFile +from gluster.swift.obj.diskfile import DiskWriter, DiskFile from gluster.swift.common.utils import DEFAULT_UID, DEFAULT_GID, X_TYPE, \ X_OBJECT_TYPE, DIR_OBJECT from gluster.swift.common.fs_utils import Fake_file @@ -98,6 +98,24 @@ class MockRenamerCalled(Exception): def _mock_renamer(a, b): raise MockRenamerCalled() +class TestDiskWriter(unittest.TestCase): + """ Tests for gluster.swift.obj.diskfile.DiskWriter """ + + def test_close(self): + dw = DiskWriter('a', 100, 'a', None) + mock_close = Mock() + with patch("gluster.swift.obj.diskfile.do_close", mock_close): + # It should call do_close + self.assertEqual(100, dw.fd) + dw.close() + self.assertEqual(1, mock_close.call_count) + self.assertEqual(None, dw.fd) + + # It should not call do_close since it should + # have made fd equal to None + dw.close() + self.assertEqual(None, dw.fd) + self.assertEqual(1, mock_close.call_count) class TestDiskFile(unittest.TestCase): """ Tests for gluster.swift.obj.diskfile """ @@ -1029,7 +1047,7 @@ class TestDiskFile(unittest.TestCase): assert os.path.exists(saved_tmppath) dw.write("123") # Closing the fd prematurely should not raise any exceptions. - os.close(dw.fd) + dw.close() assert not os.path.exists(saved_tmppath) def test_create_err_on_unlink(self): |