question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Replace worker use of `threading.local()` with ContextVar

See original GitHub issue

Context variables are now the recommended way to manage thread/context-local state:

Context managers that have state should use Context Variables instead of threading.local() to prevent their state from bleeding to other code unexpectedly, when used in concurrent code. https://docs.python.org/3/library/contextvars.html

They work for threads, but as a bonus, also work for async code.

In many places in the worker (and even more in distributed in general), we use threading.local() variables currently. However, workers also support being used in an async mode (used in most tests), and increasingly support running async functions (https://github.com/dask/distributed/pull/5151). The fact that thread-local variables can have the wrong value when used in an async context means there are likely lots of bugs around async mode. https://github.com/dask/distributed/issues/4959 is just one example.

If we want to support async mode properly, switching Worker use of thread-local variables to ContextVars would be a good place to start. These sorts of bugs are generally very confusing and tedious to track down. It might be worth getting ahead of these bugs instead of repeatedly hunting them down.

As a bonus, we’re inconsistent about ContextVars vs thread-local currently (for example, get_client checks a ContextVar, but get_worker checks a thread-local var). Consolidating on one interface might make future development smoother.

cc @jcrist @crusaderky @fjetter

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
crusaderkycommented, Nov 15, 2021

+1 for replacing thread-local variables with contetxvars wherever possible.

0reactions
gjoseph92commented, Jun 14, 2022

I’m surprised to hear that. I thought I found the opposite was the case in https://github.com/dask/distributed/pull/5517.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Is there any reason to use Python threading.local() rather than ...
I am working on some code to wrap around ContextVars so they can be a drop-in replacement, but the thing is not production...
Read more >
Thread Context Variables in Python
ContextVar() class. This module provides APIs to manage, store, and access context-local state. The ContextVar class is used to declare and work ......
Read more >
Back-propagation of contextvar changes from worker threads
to_thread.run_sync() ) but any changes made to those variables are not propagated back. This has become a problem in application frameworks, and ...
Read more >
Python contextvars and multithreading | by Koby Bass - Medium
In single threaded code, this can be solved by a global variable or the Singleton pattern. For most applications, concurrency is required to ......
Read more >
Contextvars and Thread local - ValarMorghulis.IO
In the module level, use ContextVar.set or directly setattr for a thread local variable, won't successfully set a default value, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found