Skip to content

Race condition can occur in some scenarios #16

@ciprianstoica

Description

@ciprianstoica

I had the following scenario:

  • Gunicorn spawning more than one worker
  • a slow running system because of a momentarily high CPU usage
  • a user that attempts to log in for the first time since this middleware has been added to the project (and thus has no entry in the preventconcurrentlogins_visitor table) impatiently clicks login several times. So all those login attempts would run in parallel.

In such situations a race condition can occur and because of that, I got the following exception:

duplicate key value violates unique constraint "preventconcurrentlogins_visitor_user_id_key"
DETAIL: Key (user_id)=(385) already exists.
...
File "/opt/python3.9/lib/python3.9/site-packages/preventconcurrentlogins/middleware.py", line 39, in process_request
Visitor.objects.create(

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions