python-injector / python-injector/injector
ThreadLocalScope doesn't clear manually set Keys
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 1.5k
- Forks
- 94
- PR merge metrics
- No merged PRs in 30d
Description
Manually bound Keys end up caching a value you might expect to be thread local with ThreadLocalScope. The problem is that for manually bound items, a InstanceProvider(to) is created. Then later ThreadLocalScope does
provider = InstanceProvider(provider.get(self.injector))
Where provider.get just returns that to always. I would have expected something fancier to happen such that thread2 would never see the binding.
TEST_KEY = Key('TEST_KEY')
thread1
injector.binder.bind(TEST_KEY, 'test', threadlocal)
assert(injector.get(TEST_KEY) == 'test')
thread2
assert(injector.get(TEST_KEY) == 'test')
https://github.com/alecthomas/injector/blob/master/injector.py#L442
https://github.com/alecthomas/injector/blob/master/injector.py#L608
Not really sure what the best approach would be. I would have thought that the thread local scope sort of owned the binding created when I did
injector.binder.bind(TEST_KEY, 'test', threadlocal)
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in injector.py around lines 442 and 608, focusing on ThreadLocalScope and the InstanceProvider created for manually bound Keys. Reproduce the two-thread example from the issue and inspect how provider.get(self.injector) handles the manually supplied value. Done means a manually thread-local-bound Key is not visible across threads, with regression coverage for that behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100