From d51a89a40274b8451ffc12ce0845808fcc412a86 Mon Sep 17 00:00:00 2001
From: Patrick Uiterwijk <puiterwijk@redhat.com>
Date: May 17 2017 13:54:10 +0000
Subject: [PATCH 1/3] Don't grab project-wide lock for issue view API calls


Signed-off-by: Patrick Uiterwijk <puiterwijk@redhat.com>

---

diff --git a/pagure/api/issue.py b/pagure/api/issue.py
index 923525c..31dcfe6 100644
--- a/pagure/api/issue.py
+++ b/pagure/api/issue.py
@@ -27,7 +27,7 @@ from pagure.api import (
 )
 
 
-def _get_repo(repo_name, username=None, namespace=None):
+def _get_repo(repo_name, username=None, namespace=None, with_lock=False):
     """Check if repository exists and get repository name
     :param repo_name: name of repository
     :param username:
@@ -37,7 +37,8 @@ def _get_repo(repo_name, username=None, namespace=None):
     :return: repository name
     """
     repo = get_authorized_api_project(
-        SESSION, repo_name, user=username, namespace=namespace, with_lock=True)
+        SESSION, repo_name, user=username, namespace=namespace,
+        with_lock=with_lock)
 
     if repo is None:
         raise pagure.exceptions.APIError(
@@ -193,7 +194,7 @@ def api_new_issue(repo, username=None, namespace=None):
 
     """
     output = {}
-    repo = _get_repo(repo, username, namespace)
+    repo = _get_repo(repo, username, namespace, with_lock=True)
     _check_issue_tracker(repo)
 
     if flask.g.token.project and repo != flask.g.token.project:
@@ -677,7 +678,7 @@ def api_change_status_issue(repo, issueid, username=None, namespace=None):
     """
     output = {}
 
-    repo = _get_repo(repo, username, namespace)
+    repo = _get_repo(repo, username, namespace, with_lock=True)
     _check_issue_tracker(repo)
     _check_token(repo, project_token=False)
 
@@ -795,7 +796,7 @@ def api_change_milestone_issue(repo, issueid, username=None, namespace=None):
 
     """  # noqa
     output = {}
-    repo = _get_repo(repo, username, namespace)
+    repo = _get_repo(repo, username, namespace, with_lock=True)
     _check_issue_tracker(repo)
     _check_token(repo)
 
@@ -894,7 +895,7 @@ def api_comment_issue(repo, issueid, username=None, namespace=None):
 
     """
     output = {}
-    repo = _get_repo(repo, username, namespace)
+    repo = _get_repo(repo, username, namespace, with_lock=True)
     _check_issue_tracker(repo)
     _check_token(repo, project_token=False)
 
@@ -974,7 +975,7 @@ def api_assign_issue(repo, issueid, username=None, namespace=None):
 
     """
     output = {}
-    repo = _get_repo(repo, username, namespace)
+    repo = _get_repo(repo, username, namespace, with_lock=True)
     _check_issue_tracker(repo)
     _check_token(repo)
 
@@ -1072,7 +1073,7 @@ def api_subscribe_issue(repo, issueid, username=None, namespace=None):
 
     """  # noqa
     output = {}
-    repo = _get_repo(repo, username, namespace)
+    repo = _get_repo(repo, username, namespace, with_lock=True)
     _check_issue_tracker(repo)
     _check_token(repo)
 
@@ -1155,7 +1156,7 @@ def api_update_custom_field(
 
     """  # noqa
     output = {}
-    repo = _get_repo(repo, username, namespace)
+    repo = _get_repo(repo, username, namespace, with_lock=True)
     _check_issue_tracker(repo)
     _check_token(repo)
 

From 352feee1710fbf984bb37697b4fd61cf7b8272bc Mon Sep 17 00:00:00 2001
From: Patrick Uiterwijk <puiterwijk@redhat.com>
Date: May 17 2017 13:54:10 +0000
Subject: [PATCH 2/3] Request locking for the default hook


Signed-off-by: Patrick Uiterwijk <puiterwijk@redhat.com>

---

diff --git a/pagure/hooks/files/default_hook.py b/pagure/hooks/files/default_hook.py
index ef000b2..6d563f1 100755
--- a/pagure/hooks/files/default_hook.py
+++ b/pagure/hooks/files/default_hook.py
@@ -39,7 +39,8 @@ def run_as_post_receive_hook():
         print('namespace:', namespace)
 
     project = pagure.lib._get_project(
-        pagure.SESSION, repo, user=username, namespace=namespace)
+        pagure.SESSION, repo, user=username, namespace=namespace,
+        with_lock=True)
 
     for line in sys.stdin:
         if pagure.APP.config.get('HOOK_DEBUG', False):
@@ -102,6 +103,8 @@ def run_as_post_receive_hook():
         print('An error occured while running the default hook, please '
               'report it to an admin.')
 
+    pagure.SESSION.remove()
+
 
 def main(args):
     run_as_post_receive_hook()

From a04e952e5b81136b38ebb07e0e5cbfd4fa4fc8ba Mon Sep 17 00:00:00 2001
From: Patrick Uiterwijk <puiterwijk@redhat.com>
Date: May 17 2017 13:54:10 +0000
Subject: [PATCH 3/3] Never lock for unauthenticated or non-POST requests


Signed-off-by: Patrick Uiterwijk <puiterwijk@redhat.com>

---

diff --git a/pagure/__init__.py b/pagure/__init__.py
index e79228a..21fdcab 100644
--- a/pagure/__init__.py
+++ b/pagure/__init__.py
@@ -59,6 +59,7 @@ if 'PAGURE_CONFIG' in os.environ:
     APP.config.from_envvar('PAGURE_CONFIG')
 
 logging.config.dictConfig(APP.config.get('LOGGING') or {'version': 1})
+logger = logging.getLogger(__name__)
 
 
 if APP.config.get('THEME_TEMPLATE_FOLDER', False):
@@ -370,6 +371,15 @@ def get_authorized_project(session, project_name, user=None, namespace=None,
     :rtype: Project
 
     '''
+    if with_lock:
+        if not authenticated():
+            logger.info('Unauthenticated request requested lock')
+            with_lock = False
+
+        if not flask.request.method == 'POST':
+            logger.info('non-POST request requested lock')
+            with_lock = False
+
     repo = pagure.lib._get_project(session, project_name, user, namespace,
                                    with_lock)
 

