Skip to content

Commit

Permalink
Merge pull request #5 from mitre/mutex-mount-unmount
Browse files Browse the repository at this point in the history
Fix #3 use mutex to resolve race condition on non-atomic mount/unmoun…
  • Loading branch information
jondricek authored Feb 25, 2021
2 parents b11e861 + 3cea396 commit 3eb2761
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/thumbtack/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging
import os
import threading

from pathlib import Path
from pkg_resources import get_distribution, DistributionNotFound
Expand Down Expand Up @@ -52,6 +53,8 @@ def create_app(mount_dir=None, image_dir=None, database=None, base_url=None):
elif os.environ.get("THUMBTACK_APPLICATION_ROOT"):
app.config.update(APPLICATION_ROOT=os.environ.get("THUMBTACK_APPLICATION_ROOT"))

app.mnt_mutex = threading.Lock()

# configure the rest
configure(app, base_url)
app.before_first_request(before_first_request)
Expand Down
7 changes: 6 additions & 1 deletion src/thumbtack/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,12 @@ def put(self, image_path):
"""
status = None
try:
current_app.mnt_mutex.acquire()
mounted_disk = mount_image(image_path)

if mounted_disk and mounted_disk.mountpoint is not None:
current_app.logger.info(f"Image mounted successfully: {image_path}")
current_app.mnt_mutex.release()
return mounted_disk

# TODO: refactor to not duplicate code in the mount_form in views.py
Expand All @@ -67,6 +69,7 @@ def put(self, image_path):
except ImageNotInDatabaseError:
status = f"Cannot mount {image_path}. Image is not in Thumbtack database."

current_app.mnt_mutex.release()
current_app.logger.error(status)
abort(400, message=str(status))

Expand All @@ -92,15 +95,17 @@ def get(self, image_path=None):
abort(404, message=f"{image_path} not mounted")
return mount_info

def delete(self, image_path):
def delete(self, image_path=None):
"""Unmounts an image file.
Parameters
----------
image_path : str
Relative path to an image file to unmount.
"""
current_app.mnt_mutex.acquire()
unmount_image(image_path)
current_app.mnt_mutex.release()


class SupportedLibraries(Resource):
Expand Down

0 comments on commit 3eb2761

Please sign in to comment.