Skip to content

Commit

Permalink
Fix #208
Browse files Browse the repository at this point in the history
One important query was not atomic, leading to a race
  • Loading branch information
jdabtieu committed Nov 23, 2023
1 parent dcfb22e commit b2113b0
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/python-app.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@
/share
/Scripts
/Include
/include
/data
pyvenv.cfg
__pycache__
4 changes: 4 additions & 0 deletions src/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
7 changes: 3 additions & 4 deletions src/views/contest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down

0 comments on commit b2113b0

Please sign in to comment.