diff --git a/main.py b/main.py index 91eec2c..42a8d8c 100644 --- a/main.py +++ b/main.py @@ -16,7 +16,9 @@ cur = con.cursor() # NB! single is enough for now, we might require multiple la async def new_session(): """Start a new session""" # add session to the database - try: + tries = 3 # something is very wrong with our random, if we miss 3 times + for i in range(tries): + try: # generate a cookie cookie = uuid4().hex cur.execute( @@ -27,8 +29,13 @@ async def new_session(): ) con.commit() except sqlite3.IntegrityError as e: - if str(e) == "UNIQUE constraint failed: sessions.cookie": + if i < tries - 1 and str(e) == "UNIQUE constraint failed: sessions.cookie": + continue + else if str(e) == "UNIQUE constraint failed: sessions.cookie": return {"cookie": "error"} + else: + raise + break # return new session cookie return {"cookie": cookie}