yfinance 1.2.1: yf.download is still not thread-safe across concurrent top-level calls (RuntimeError: dictionary changed size during iteration)
Describe bug
yfinance.download() in v1.2.1 still uses shared module-level dictionaries in yfinance.shared (_DFS, _ERRORS, _TRACEBACKS, _ISINS).
When multiple application threads call yf.download(..., threads=False) concurrently, those top-level calls still race with each other because they mutate and iterate the same global dicts.
This can raise:
RuntimeError: dictionary changed size during iteration
### Simple code that reproduces your problem
from concurrent.futures import ThreadPoolExecutor
import yfinance as yf
chunks = [
["AAPL", "MSFT", "GOOG"],
["NVDA", "META", "AMZN"],
]
def fetch(chunk):
return yf.download(chunk, period="5d", interval="1m", threads=False, progress=False)
with ThreadPoolExecutor(max_workers=2) as ex:
list(ex.map(fetch, chunks))
### Debug log with debug logging enabled
.
### Bad data proof
_No response_
### `yfinance` version
1.2.1
### Python version
3.14
### Operating system
Fedora 43
Make download() reentrant and thread-safe by removing shared global state yfinance.download() currently uses module-level global dictionaries in yfinance.shared as scratch buffers:
_DFS, _ERRORS, _TRACEBACKS, _ISINS
These globals are mutated and iterated during every download() call.
This makes the function non-reentrant and not thread-safe, even when threads=False. Just wanted to hear your thoughts.
Should be fixed now.
@
Appears fixed on dev. download() no longer uses the module-level dicts in yfinance.shared. multi.py now builds a per-call _DownloadContext (own dfs, errors, tracebacks, isins, progress_bar, and lock), so concurrent top-level calls each mutate their own state with no shared iteration. The _DFS/_ERRORS/_TRACEBACKS/_ISINS globals are now unreferenced across the package. Appears safe to close.
@