ElMassimo / ElMassimo/request_store_rails

Why does `current_store_id` default to `nil`?

Open
#11 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Ruby
Stars
85
Forks
6
PR merge metrics
No merged PRs in 30d

Description

Hi,

I was bit recently by a bug because a thread (created by a library) did not properly initialize `current_store_id`. I added a monkey patch to add that, and all is working well now.

However, this got me thinking: why is it possible for `current_store_id` to ever return `nil`? I believe the safer default is to always set to `SecureRandom.uuid` if uninitialized:

```ruby
def current_store_id
Thread.current.fetch(REQUEST_STORE_ID) { SecureRandom.uuid }
end
```

The current default basically makes all the spawned threads share a store between them, but separate from the main thread.

It may be an even safer default to raise an error when the request store is unset. It probably means a thread was spawned without proper setup/teardown

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.