Browse Source

타임아웃 도입

https://github.com/openNAMU/openNAMU/issues/1584
잉여개발기 (SPDV) 2 years ago
parent
commit
f2e8f46811
5 changed files with 32 additions and 9 deletions
  1. 1 0
      lang/en-US.json
  2. 4 3
      lang/ko-KR.json
  3. 23 5
      route/edit.py
  4. 3 0
      route/tool/func.py
  5. 1 1
      version.json

+ 1 - 0
lang/en-US.json

@@ -577,6 +577,7 @@
             "error_title_length_too_long" : "Documents title or Discussion topic length is too long. Maximum number of characters : ",
             "error_password_length_too_short" : "Password length is too short. Minimum number of characters : ",
             "error_password_require_for_wiki_access" : "A password is required to access the wiki.",
+            "timeout_error" : "Running is taking too long. Maximum running time (Second(s)) : ",
         "_comment_3.2_" : "Warning",
             "http_warning" : "Warning: If you are not on HTTPS connection, your information can be leaked. The users themselves have responsibility to any problems that happen because of this.",
             "user_head_warning" : "User data will be deleted if you close the browser or when you sign in.",

+ 4 - 3
lang/ko-KR.json

@@ -84,7 +84,7 @@
     "file_name_error": "파일명에는 알파벳, 한글, 공백, 밑줄 및 빼기 기호만 사용할 수 있습니다.",
     "pass": "넘기기",
     "recaptcha_error": "'로봇이 아닙니다'를 통해 reCAPTCHA를 통과하세요.",
-    "file_capacity_error": "최대 파일 크기 (MB): ",
+    "file_capacity_error": "최대 파일 크기 (MB) : ",
     "setting": "설정",
     "end": "끝",
     "error": "오류",
@@ -307,7 +307,7 @@
     "requires_approval": "가입시 승인 필요",
     "approval_question": "회원가입 질문",
     "public_key": "공개 키",
-    "fast_edit_error": "편집 속도가 너무 빠릅니다. 제한 (초): ",
+    "fast_edit_error": "편집 속도가 너무 빠릅니다. 제한 (초) : ",
     "main_acl_setting": "기본 ACL 설정",
     "edit_req_acl": "편집 요청 ACL",
     "application_list": "가입신청 목록",
@@ -547,5 +547,6 @@
     "link_case_insensitive": "링크 대소문자 구분 안함",
     "hide_user_name" : "가입자 이름 숨기기",
     "comment" : "댓글",
-    "reply" : "대댓글"
+    "reply" : "대댓글",
+    "timeout_error" : "실행 시간이 너무 오래 걸립니다. 최대 실행 시간 (초) : "
 }

+ 23 - 5
route/edit.py

@@ -1,5 +1,26 @@
 from .tool.func import *
 
+def edit_render_set(name, content):
+    render_set(
+        doc_name = name,
+        doc_data = content,
+        data_in = ''
+    )
+
+# https://stackoverflow.com/questions/13821156/timeout-function-using-threading-in-python-does-not-work
+def edit_timeout(func, args = (), timeout = 3):
+    pool = multiprocessing.Pool(processes = 1)
+    result = pool.apply_async(func, args = args)
+    try:
+        result.get(timeout = timeout)
+    except multiprocessing.TimeoutError:
+        pool.terminate()
+        return 1
+    else:
+        pool.close()
+        pool.join()
+        return 0
+
 def edit(name = 'Test', section = 0, do_type = ''):
     with get_db_connect() as conn:
         curs = conn.cursor()
@@ -74,11 +95,8 @@ def edit(name = 'Test', section = 0, do_type = ''):
             else:
                 leng = '+' + str(len(content))
 
-            render_set(
-                doc_name = name,
-                doc_data = content,
-                data_in = ''
-            )
+            if edit_timeout(edit_render_set, (name, content), timeout = 5) == 1:
+                return re_error('/error/41')
                 
             if db_data:
                 curs.execute(db_change("update data set data = ? where title = ?"), [content, name])

+ 3 - 0
route/tool/func.py

@@ -10,6 +10,7 @@ import logging
 import random
 import typing
 import ipaddress
+import multiprocessing
 
 import email.mime.text
 import email.utils
@@ -2637,6 +2638,8 @@ def re_error(data):
                 password_min_length = ''
                 
             data = load_lang('error_password_length_too_short') + password_min_length
+        elif num == 41:
+            data = load_lang('timeout_error') + '5'
         else:
             data = '???'
 

+ 1 - 1
version.json

@@ -1,6 +1,6 @@
 {
     "beta" : {
-        "r_ver" : "v3.4.6-RC3-dev191",
+        "r_ver" : "v3.4.6-RC3-dev192",
         "c_ver" : "3500361",
         "s_ver" : "3500111"
     }