Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recently added schemas don't appear in new connections made in new threads #365

Closed
glennfu opened this issue Sep 16, 2016 · 8 comments
Closed

Comments

@glennfu
Copy link

glennfu commented Sep 16, 2016

I think this might be somewhat related to #186 as my code shares a similar structure to what's described there.

schema = "app"
Apartment::Tenant.create(schema)
Apartment::Tenant.switch!(schema)
puts Apartment::Tenant.current
=> "app"

t = Thread.new do
  ApplicationRecord.connection_pool.with_connection do |conn|
    puts Apartment::Tenant.current
    => "public"
    Apartment::Tenant.switch!(schema)
    => Right here I get "One of the following schema(s) is invalid: "app" "public"
  end
end
t.join

If I replace that lower .switch! call with Apartment::Tenant.create(schema) I get an error saying that schema already exists. I would expect that since it existed and connected before, that opening a new Thread after this the schema would still be accessible. Any idea what I can do to work around this?

@glennfu
Copy link
Author

glennfu commented Sep 23, 2016

A bit more debugging:

ActiveRecord::Base.connection.select_all("SELECT * from pg_namespace").rows.length
=> 7
Apartment::Tenant.create("my_new_schema")
ActiveRecord::Base.connection.select_all("SELECT * from pg_namespace").rows.length
=> 8
t = Thread.new do
  ActiveRecord::Base.connection.select_all("SELECT * from pg_namespace").rows.length
  => 7
end
t.join

I've been trying a million things around in here to see if anything makes a different but the command inside the new thread never sees the existence of the recently created postgres schema.

@glennfu glennfu changed the title Opening a new thread won't let me switch back to the same schema Recently added schemas don't appear in new connections made in new threads Sep 23, 2016
@glennfu
Copy link
Author

glennfu commented Sep 23, 2016

I think a workaround to this situation is to say that if you need to add new schemas, and you need them to appear in new threads, you should restart your stack. Then hopefully it's sticky across everything.

@glennfu
Copy link
Author

glennfu commented Sep 23, 2016

Adding ActiveRecord::Base.clear_active_connections! just before I call Thread.new seems to have fixed my issue.

I'm going to call this one fixed.

@glennfu glennfu closed this as completed Sep 23, 2016
@mikecmpbll
Copy link
Collaborator

this is odd? i assume what's happening with clear_active_connections! is that it's putting the current connection back in to the pool and the new thread is checking out that same connection. (could confirm by looking at ActiveRecord::Base.connection.object_id before and inside thread).

i'm unsure why the new thread connection gets a different result though—i assume it's some kind of query/schema cache? maybe try ActiveRecord::Base.connection.schema_cache.clear! or ActiveRecord::Base.connection.query_cache.clear inside the new thead, instead of clear_active_connections!.

@glennfu
Copy link
Author

glennfu commented Sep 28, 2016

I tried both of these calls just before the thread, just inside the top of the new thread, and anywhere else I could think to throw it, but none of them fixed the issue. Thanks for the ideas though!

@glennfu
Copy link
Author

glennfu commented Sep 28, 2016

Also I checked the object_id of ActiveRecord::Base.connection and also conn inside the with_connection block and confirmed their the same so I think your hunch about clear_active_connections! releasing it back for use in the new Thread is correct.

Now how can I get a new Thread and a new connection to recognize the new schema?

@mikecmpbll
Copy link
Collaborator

no clue, maybe pg is caching it per connection. should be easy enough to test out by connecting via cli or something else, creating a new tenant in rails, then check pg_namespace on cli?

@glennfu
Copy link
Author

glennfu commented Sep 28, 2016

So far I can't seem to reproduce this outside of my specs. Both my rails console and specs are hitting the same postgres install & user. However in my specs I can reproduce this problem 100% of the time. Very strange!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants