Title: Review thread safety of client/API/Evaluation context · Issue #96 · open-feature/python-sdk · GitHub
Open Graph Title: Review thread safety of client/API/Evaluation context · Issue #96 · open-feature/python-sdk
X Title: Review thread safety of client/API/Evaluation context · Issue #96 · open-feature/python-sdk
Description: Review the thread safety of the SDK to make sure there's no potential concurrency issues, particularly around state maintained in the global API object, clients, and evaluation context objects. See here for a similar discussion in the Ja...
Open Graph Description: Review the thread safety of the SDK to make sure there's no potential concurrency issues, particularly around state maintained in the global API object, clients, and evaluation context objects. See...
X Description: Review the thread safety of the SDK to make sure there's no potential concurrency issues, particularly around state maintained in the global API object, clients, and evaluation context objects....
Opengraph URL: https://github.com/open-feature/python-sdk/issues/96
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Review thread safety of client/API/Evaluation context","articleBody":"Review the thread safety of the SDK to make sure there's no potential concurrency issues, particularly around state maintained in the global API object, clients, and evaluation context objects.\n\nSee [here](https://github.com/open-feature/java-sdk/issues/64) for a similar discussion in the Java SDK and others.\n\n## Background\n\nPython's GIL is sometimes mistaken for making threading a non-issue (like Node's single-threaded model), but they're not the same. The GIL only makes individual bytecode operations atomic; compound operations like check-then-act or read-modify-write are not safe. Providers commonly run background threads for connection management and emit events from them, so these races aren't just theoretical.\n\n## Audit\n\nHere's what we found. The only locks in the SDK today are in `_event_support.py` (and even those have some gaps). Everything else is unprotected.\n\n- **`ProviderRegistry` singleton** (`provider/_registry.py`) - this is the big one. No locks at all. `set_provider()` and `set_default_provider()` have check-then-act patterns with side effects (initialize/shutdown), so concurrent calls can double-shutdown, lose providers, or initialize providers that never get registered. `clear_providers()` has a window where shut-down providers are still returned to callers.\n- **Global `_hooks` list** (`hook/__init__.py`) - `add_hooks` does `_hooks = _hooks + hooks` which is a read-concat-rebind; two concurrent calls can lose one set of hooks\n- **`api.clear_providers()`** (`api.py`) - calls `provider_registry.clear_providers()` then `_event_support.clear()` as two separate steps; event handlers still exist but providers are gone in between\n- **`_event_support.py`** - has RLock (good), but immediate handler execution in `add_*_handler` runs outside the lock, and `clear()` acquires two separate locks non-atomically\n- **Global `_evaluation_context`** (`evaluation_context/__init__.py`) - no lock; TOCTOU across the evaluation pipeline (context can change between read and use)\n- **Global transaction context propagator** (`transaction_context/__init__.py`) - no lock; read-then-call-method races with propagator swap\n- **Client instance state** (`client.py`) - `self.hooks` has the same lost-update pattern as global hooks; `_assert_provider_status` reads `self.provider` again instead of using the reference captured earlier in the evaluation flow, so the status check can be for a different provider than the one actually used\n- **`AbstractProvider._on_emit`** (`provider/__init__.py`) - `emit()` does `if hasattr(self, \"_on_emit\"): self._on_emit(...)` which is a TOCTOU; `detach()` during shutdown can delete `_on_emit` while a background thread is between the check and the call\n\n## Definition of done\n\n- [ ] issues created for thread safety of global API, client, and evaluation context","author":{"url":"https://github.com/toddbaert","@type":"Person","name":"toddbaert"},"datePublished":"2023-04-18T13:33:27.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":5},"url":"https://github.com/96/python-sdk/issues/96"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:efa45860-c3c9-08f8-5abe-8665f851d4e4 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A1CE:36DB52:1411B55:1CF9024:6A4CDA2B |
| html-safe-nonce | dcf4eb3ad8393d80fe78777137068a5cf8a0c300e95f1d08942fb6360d3f0212 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBMUNFOjM2REI1MjoxNDExQjU1OjFDRjkwMjQ6NkE0Q0RBMkIiLCJ2aXNpdG9yX2lkIjoiNjYwOTE2ODEyNDM3NjU3MDQxMSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | f8923b77c4f00a211f3c48684f1b35a6044c323ca57e33bd11317586e4fdef7c |
| hovercard-subject-tag | issue:1673147756 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/open-feature/python-sdk/96/issue_layout |
| twitter:image | https://opengraph.githubassets.com/c2b62d68a5735eef3c2962143fd34ee3b3cd76030b3b606bcd42a4ff0a2f6c07/open-feature/python-sdk/issues/96 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/c2b62d68a5735eef3c2962143fd34ee3b3cd76030b3b606bcd42a4ff0a2f6c07/open-feature/python-sdk/issues/96 |
| og:image:alt | Review the thread safety of the SDK to make sure there's no potential concurrency issues, particularly around state maintained in the global API object, clients, and evaluation context objects. See... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | toddbaert |
| hostname | github.com |
| expected-hostname | github.com |
| None | 299b43bca6e02ad35197ffeba30d2466846d5fb02ab96fbced5b5e6cec589fb8 |
| turbo-cache-control | no-preview |
| go-import | github.com/open-feature/python-sdk git https://github.com/open-feature/python-sdk.git |
| octolytics-dimension-user_id | 99483952 |
| octolytics-dimension-user_login | open-feature |
| octolytics-dimension-repository_id | 489357827 |
| octolytics-dimension-repository_nwo | open-feature/python-sdk |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 489357827 |
| octolytics-dimension-repository_network_root_nwo | open-feature/python-sdk |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 3eeb74f55ccd8a31bdaec6839577c592bbf7844c |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width