Skip to content

activerecord: No warning for return out of transaction block without writes#39453

Merged
eileencodes merged 2 commits intorails:masterfrom
dylanahsmith:transaction-return-no-raise
May 28, 2020
Merged

activerecord: No warning for return out of transaction block without writes#39453
eileencodes merged 2 commits intorails:masterfrom
dylanahsmith:transaction-return-no-raise

Conversation

@dylanahsmith
Copy link
Copy Markdown
Contributor

Summary

#29333 introduced a deprecation warning for transaction blocks that are exited with break, return or throw which has a couple of problems which @eileencodes brought to my attention (#29333 (comment)).

That deprecation warning was introduced for code like

Timeout.timeout(1) do
  Example.transaction do
    example.update_attributes(value: 1)
    sleep 3 # simulate something slow
  end
end

because it wasn't expected that the timeout would commit the transaction. However, the warning says the next version of rails will raise an exception and it would also be unexpected for that code to not result in a Timeout::Error exception. So the first commit in this PR changes the deprecation warning message to remove the mention of raising.

The other problem is with deprecation warnings from returns out of a transaction block that hasn't made any writes to the transaction, where it doesn't matter if the transaction is rolled back or committed. E.g.

with_lock do
  return if some_crtieria_met?

  # do work
end

The second commit in this PR avoids that deprecation warning by marking open transactions as having written on the first write query, then making the warning conditional on the current transaction having written.

Other Information

Since the #29333 hasn't been released, I've just edited the existing CHANGELOG entry for it.

Copy link
Copy Markdown
Member

@eileencodes eileencodes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left a comment for a minor change, otherwise this looks good. Thanks for the quick turn around.

@dylanahsmith dylanahsmith force-pushed the transaction-return-no-raise branch from 0090de4 to 4332613 Compare May 27, 2020 20:48
If the transaction block is exited due to a timeout, we don't want to
change what exception is raised.  Also, not raising will allow the
transaction to be conveniently aborted by an `return` or `break`
statement.
…writes

It doesn't matter if the transaction is rolled back or committed if it
wasn't written to, so we can avoid warning about a breaking change.
@eileencodes eileencodes merged commit 55f519f into rails:master May 28, 2020
@eileencodes
Copy link
Copy Markdown
Member

Thanks Dylan!

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants