From b2113b04d6ee5dbc9f9aa3b9a8babd3a43008105 Mon Sep 17 00:00:00 2001 From: Jonathan Wu Date: Wed, 22 Nov 2023 23:09:53 -0500 Subject: [PATCH] Fix #208 One important query was not atomic, leading to a race --- .github/workflows/python-app.yml | 2 +- .gitignore | 2 ++ src/helpers.py | 4 ++++ src/views/contest.py | 7 +++---- 4 files changed, 10 insertions(+), 5 deletions(-) diff --git a/.github/workflows/python-app.yml b/.github/workflows/python-app.yml index 06d40a8..02ccd70 100644 --- a/.github/workflows/python-app.yml +++ b/.github/workflows/python-app.yml @@ -35,7 +35,7 @@ jobs: # stop the build if there are Python syntax errors or undefined names flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics # exit-zero treats all errors as warnings. CTFOJ uses 90 characters per line as the limit for Python code. - flake8 . --count --exit-zero --ignore=F405,W503 --max-complexity=10 --max-line-length=90 --statistics + flake8 . --count --exit-zero --ignore=F405,W503 --max-complexity=11 --max-line-length=90 --statistics - name: Test and generate coverage report with pytest run: | git rm src/migrate.py diff --git a/.gitignore b/.gitignore index 82f02a5..2c27351 100644 --- a/.gitignore +++ b/.gitignore @@ -4,5 +4,7 @@ /share /Scripts /Include +/include /data pyvenv.cfg +__pycache__ diff --git a/src/helpers.py b/src/helpers.py index 613049d..157240f 100644 --- a/src/helpers.py +++ b/src/helpers.py @@ -242,6 +242,10 @@ def update_dyn_score(contest_id, problem_id, update_curr_user=True): Prereqs for using this function: user solve entry must already be in contest_solved """ db.execute("BEGIN") + if update_curr_user: + db.execute(("INSERT INTO contest_solved(contest_id, user_id, problem_id) " + "VALUES(:cid, :uid, :pid)"), + cid=contest_id, pid=problem_id, uid=session["user_id"]) check = db.execute(("SELECT * FROM contest_problems WHERE contest_id=:cid AND " "problem_id=:pid"), cid=contest_id, pid=problem_id) solves = len(db.execute( diff --git a/src/views/contest.py b/src/views/contest.py index 8017786..119bcc4 100644 --- a/src/views/contest.py +++ b/src/views/contest.py @@ -268,13 +268,12 @@ def contest_problem(contest_id, problem_id): "AND user_id=:uid AND problem_id=:pid"), cid=contest_id, uid=session["user_id"], pid=problem_id) if len(check1) == 0: - db.execute(("INSERT INTO contest_solved(contest_id, user_id, problem_id) " - "VALUES(:cid, :uid, :pid)"), - cid=contest_id, pid=problem_id, uid=session["user_id"]) - if check[0]["score_users"] != -1: # Dynamic scoring update_dyn_score(contest_id, problem_id) else: # Static scoring + db.execute(("INSERT INTO contest_solved(contest_id, user_id, problem_id) " + "VALUES(:cid, :uid, :pid)"), + cid=contest_id, pid=problem_id, uid=session["user_id"]) points = check[0]["point_value"] db.execute(("UPDATE contest_users SET lastAC=datetime('now'), " "points=points+:points WHERE contest_id=:cid AND user_id=:uid"),