home · contact · privacy
Ensure DB connections are closed even on erroneous/failing HTTP requests.
authorChristian Heller <c.heller@plomlompom.de>
Tue, 2 Apr 2024 00:36:26 +0000 (02:36 +0200)
committerChristian Heller <c.heller@plomlompom.de>
Tue, 2 Apr 2024 00:36:26 +0000 (02:36 +0200)
plomtask/http.py

index 5b7100c4187d9190e9925ce7bae495fd64df6e94..f368232acd33dcc268ca296e69e0d34748f6fa2a 100644 (file)
@@ -110,10 +110,11 @@ class TaskHandler(BaseHTTPRequestHandler):
                 return
             else:
                 raise NotFoundException(f'Unknown page: /{site}')
-            conn.close()
             self._send_html(html)
         except HandledException as error:
             self._send_msg(error, code=error.http_code)
+        finally:
+            conn.close()
 
     def do_GET_calendar(self, conn: DatabaseConnection,
                         params: ParamsParser) -> str:
@@ -160,10 +161,11 @@ class TaskHandler(BaseHTTPRequestHandler):
             else:
                 msg = f'Page not known as POST target: /{site}'
                 raise NotFoundException(msg)
-            conn.close()
             self._redirect('/')
         except HandledException as error:
             self._send_msg(error, code=error.http_code)
+        finally:
+            conn.close()
 
     def do_POST_day(self, conn: DatabaseConnection, params: ParamsParser,
                     form_data: PostvarsParser) -> None: