Skip to content

Tenant is not preserved in threads #255

@serch

Description

@serch

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:

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions