Skip to content

Use contextvars for singletons. Remove forking in tests

Cal Pratt requested to merge cpratt34/context-aware-threading into master

I created two new utilities, ContextThread and ContextThreadPoolExecutor, which wrap tasks in the contextvars.copy_context().run. This allows all jobs which are started by server initialization and requests to share the base context that was present when the server is started, namely, access to the AuthManager and MonitoringBus.

When going through the code, I noticed we have many places where we have threadpools, but never terminate them (one had a nasty del method). So I added in a stop method to the StorageABC which is called recursively by the storage implementations from the instance when grpc shutdown is invoked.

With this change AuthManager and MonitoringBus can be safely stored in contextvars, rather than singleton values on class attributes. This allow us to safely run the application in a thread without bleeding global state everywhere. Because we no longer bleed global state, we no longer need to fork the pytest execution, leading to even faster test runs!

Along with these changes, I fixed up the auth client tests to more thoroughly check that auth is being applied to the request thread, and fixed an issue with the fake redis instance sharing its state globally.

Edited by Cal Pratt

Merge request reports