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

Tenant is not preserved in threads #255

Open
serch opened this issue Aug 19, 2024 · 0 comments
Open

Tenant is not preserved in threads #255

serch opened this issue Aug 19, 2024 · 0 comments

Comments

@serch
Copy link
Contributor

serch commented Aug 19, 2024

When using threads, the tenant is not preserved:

irb(main):009:0> MultiTenant.current_tenant = 1
=> 1
irb(main):010:0> puts "tenant_id: #{MultiTenant.current_tenant_id}"
irb(main):011:0> thr = Thread.new { puts "tenant_id: #{MultiTenant.current_tenant_id}" }
tenant_id: 1
=> #<Thread:0x0000000148ff8388 (irb):11 run>
tenant_id:

Hence queries don't have the tenant clasue and may leak data

irb(main):001:0> MultiTenant.current_tenant = 1
=> 1
irb(main):002:0> puts Page.where(id: 999).to_sql
irb(main):003:0> thr = Thread.new { puts Page.where(id: 999).to_sql }
SELECT "pages".* FROM "pages" WHERE "pages"."customer_id" = 1 AND "pages"."id" = 999
=> #<Thread:0x0000000149cfb198 (irb):3 run>
irb(main):004:0> SELECT "pages".* FROM "pages" WHERE "pages"."id" = 999

In case this add extra info, we are using concurrent-ruby and we have the same issue

irb(main):002:0> MultiTenant.current_tenant = 1
=> 1
irb(main):003:0> thread_pool = Concurrent::FixedThreadPool.new(2)
=>
#<Concurrent::FixedThreadPool:0x00000001251130c0
...
irb(main):004:1* thread_pool.post do
irb(main):005:1*   puts "tenant_id: #{MultiTenant.current_tenant_id}"
irb(main):006:0> end
=> true
tenant_id:
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

1 participant