Skip to content

Dont clean connections on Integration tests#24500

Closed
arthurnn wants to merge 2 commits into
masterfrom
arthurnn/clear_connections_executor_test
Closed

Dont clean connections on Integration tests#24500
arthurnn wants to merge 2 commits into
masterfrom
arthurnn/clear_connections_executor_test

Conversation

@arthurnn

Copy link
Copy Markdown
Member

Problem

Connections are going back to the pool with open transactions. This can cause deadlocks.

[related to c7b7c6a]

Solution

First, the action of clear_active_connections! should not be in the
QueryCache executor. They have different responsibilities.

This brings ConnectionManagement class back, now as an executor
instead of Middleware, however it is only responsible for clearing the
connections on complete. [related to #23807]

Also, this adds a flag, so we can turn off the clear_connection action on
Integration tests.

[fixes #24491]

review @matthewd @jeremy @rafaelfranca @mfazekas

To test the deadlocks, i used a variation of @mfazekas's script, see https://gist.github.com/arthurnn/fbb59a684a75a415a2a8032c0ee99f54
I had to change it, because with this implementation, instead of using rack.test env var, the clear connections action is attached to the IntegrationTest class.

First, the action of `clear_active_connections!` should not be in the
QueryCache executor. They have different responsabilites.

This brings `ConnectionManagement` class back, now as an executor
instead of Middleware, however it is only resposible for clearing the
connections on complete.

Also, this adds a flag, so we can turn off the clear action on
Integration tests, otherwise we can return connections with open
transactions back to the pool. [fixes #24491]
app.executor.register_hook(conn_hook)
end

ActiveSupport.on_load(:action_dispatch_integration_test) do

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'm not sure it's we want to disable it globally when ActionDispatch::IntegrationTest loaded.
For example we want to phantomjs end to end tests (clean_connections desired) as well as integration test (no clean connections) in the same run.

I think we should do the same as action_mailer:

ActiveSupport.on_load(:action_dispatch_integration_test) { include ActionMailer::TestCase::ClearTestDeliveries }

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yeah, make sense. I did some inline changes.

@mfazekas

Copy link
Copy Markdown
Contributor

We'll also have the same issue with jobs, in particular test adapter.
https://github.com/rails/rails/blob/master/activejob/lib/active_job/queue_adapters/test_adapter.rb
And there is also inline(?) adapter we likely don't want to do it there as well.
https://github.com/rails/rails/blob/master/activejob/lib/active_job/queue_adapters/inline_adapter.rb

One solution is not calling them in execution wrap - mfazekas@5e5f89f.
None of the execution hooks (lock, reload, ar connection) are interseting in that case IMO

@sgrif

sgrif commented Apr 18, 2016

Copy link
Copy Markdown
Contributor

@arthurnn Do you think this is attacking the symptom rather than the problem? I think #24608 is more the root cause here.

sgrif added a commit to sgrif/rails that referenced this pull request Apr 18, 2016
When the query cache completes, if Active Record is still inside of a
transaction, it is because the transaction is meant to be left open
above this unit of work (such as transactional fixtures in tests). There
were several tests around the behavior of "tests" that were invalid, as
tests are not run through the executor. They have been changed to
reflect the new behavior, which is closer to what actually occurs in
Rails tests.

Fixes rails#23989
Fixes rails#24491
Close rails#24608
Close rails#24500
@mfazekas

Copy link
Copy Markdown
Contributor

I think #24608 is better

@jeremy jeremy closed this Apr 19, 2016
@jeremy jeremy deleted the arthurnn/clear_connections_executor_test branch April 19, 2016 18:13
@arthurnn

Copy link
Copy Markdown
Member Author

thanks @sgrif . I will take a look at your PR

sgrif added a commit that referenced this pull request Apr 22, 2016
…4610)

When the query cache completes, if Active Record is still inside of a
transaction, it is because the transaction is meant to be left open
above this unit of work (such as transactional fixtures in tests). There
were several tests around the behavior of "tests" that were invalid, as
tests are not run through the executor. They have been changed to
reflect the new behavior, which is closer to what actually occurs in
Rails tests.

Fixes #23989
Fixes #24491
Close #24500
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.

Transaction deadlock with rails 5 beta 3

6 participants