Skip to content

Commit

Permalink
[fix] Avoid resource leaks in case a MountSource constructor throws
Browse files Browse the repository at this point in the history
  • Loading branch information
mxmlnkn committed Sep 16, 2024
1 parent c5db703 commit a9499b1
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
5 changes: 5 additions & 0 deletions core/ratarmountcore/SQLiteIndex.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,11 @@ def openWritable(self):
def __enter__(self):
return self

def __del__(self):
# This is important in case SQLiteIndex is not used with a context manager
# and the constructor raises an exception. There is no clean way to close it in that case!
self.close()

def __exit__(self, exception_type, exception_value, exception_traceback):
self.close()

Expand Down
10 changes: 6 additions & 4 deletions ratarmount.py
Original file line number Diff line number Diff line change
Expand Up @@ -676,13 +676,15 @@ def __del__(self) -> None:
try:
if self.mountPointFd is not None:
os.close(self.mountPointFd)
except Exception:
pass
except Exception as exception:
if self.printDebug >= 1:
print("[Warning] Failed to close mount point folder descriptor because of:", exception)

try:
self.mountSource.__exit__(None, None, None)
except Exception:
pass
except Exception as exception:
if self.printDebug >= 1:
print("[Warning] Failed to tear down root mount source because of:", exception)

def _addNewHandle(self, handle, flags):
# Note that fh in fuse_common.h is 64-bit and Python also supports 64-bit (long integers) out of the box.
Expand Down

0 comments on commit a9499b1

Please sign in to comment.