FAQ | This is a LIVE service | Changelog

Skip to content
Snippets Groups Projects

authentication: remove race condition in creating users

Merged Dr Rich Wareham requested to merge github/fork/rjw57/user-creation-race into master
  1. Apr 26, 2018
    • Dr Rich Wareham's avatar
      authentication: remove race condition in creating users · 166819f3
      Dr Rich Wareham authored
      Previously we emulated a get_or_create user behaviour by attempting to
      get the user and, if that failed, create it.
      
      This introduced a race where another worker could create a user between
      these checks. (We observed this in production when an initial log in
      causes a flurry of requests to lookupproxy.) This was a result of
      Django's autocommit mode since the user fetch and subsequent user
      creation were separate transactions allowing for database change between
      them.
      
      The race-free approach is to *always* attempt to create the user and
      only if that fails do we fetch the user. These are still run as separate
      transactions but the user object will always be created by the first
      worker to talk to the DB and the rest will see an integrity error
      causing them to fetch the new user object.
      166819f3
Loading