Skip to content

Instantly share code, notes, and snippets.

@gma
Last active August 29, 2015 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gma/3b24e28bde2dfe3c2d02 to your computer and use it in GitHub Desktop.
Save gma/3b24e28bde2dfe3c2d02 to your computer and use it in GitHub Desktop.
Commit message for email delivery race condition fix

When running code in a thread ActiveRecord uses a separate database connection, which can't see the results of any queries run by the main thread until they're committed. But Rails doesn't commit transactions in the test environment, making this difficult to test.

The fix for this was in two parts, either of which should be enough to solve the problem on their own.

  1. Load the ActiveRecord objects before entering the thread, as the database connection that we'll be using inside the thread may not be able to access the (freshly created) database records yet.

  2. Commit the transaction that wraps the creation of the database records before publishing the :message_created message (that triggers the thread to run). This ought to mean that code with a separate database connection will be able to load the subscribers anyway.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment