Cause
It's a race on shared state between threads. current_user is a class attribute, so every thread in a gunicorn worker process reads and writes the same slot. With --threads 4, two requests can interleave like this:
| Thread A (user Alice) | Thread B (user Bob) |
|---|---|
CartView.current_user = Alice |
|
_cart_cache.get(alice.id) → miss |
|
CartView.current_user = Bob |
|
load_cart(CartView.current_user) → loads Bob's cart |
|
_cart_cache[alice.id] = Bob's cart |
Alice is shown Bob's cart. Two details make it worse:
- The bad entry sticks. The wrong cart is stored in
_cart_cacheunder Alice's id. Her later requests to that worker hit the cache and keep showing Bob's cart until the process restarts. So one short race turns into a lasting leak for that user on that worker. - The key and the value come from different sources. The cache key uses the thread-local
request.user.id, but the value is loaded from the sharedCartView.current_user. Nothing checks that the two refer to the same user.
Why only production, and why rarely
-
Staging (
--threads 1): each worker process handles one request at a time, so nothing else can overwritecurrent_userbetween the write and the read. The bug can't happen there. Separate gunicorn workers are separate processes, so they don't share the class attribute. -
Production (
--threads 4): requests share the attribute. It only goes wrong when all three of these line up:- a cache miss (first visit to that worker, or after a restart)
- a second request writes
current_userin the short gap beforeload_cartreads it - the GIL switches threads at exactly that point
That combination fits "once in a few hundred requests."
How to confirm it before changing anything
These checks are cheap. I haven't run any of them; this diagnosis comes from reading the code.
- Log the mismatch. Temporarily log a warning in production or in a load test whenever
CartView.current_user.id != request.user.idjust beforeload_cart. Any hit proves this mechanism. - Reproduce in staging. Run staging with
--threads 4and send concurrent requests from many different users to the cart page. A fresh worker gives the most cache misses. Then check which user's items each response contains. Running the same load with--threads 1should show zero leaks. - Force it in a test. Make
load_cartsleep briefly or wait on athreading.Barrier, then callCartView().get()from two threads with different users. This reproduces it every time. - Check that it persists. Once a user has seen a wrong cart, their repeat requests to the same worker should keep showing that same wrong cart. Restarting the worker should clear it. If that's what happens, the cache is what turns the race into a lasting leak.
If check 1 or 2 fires, the cause is confirmed. Note that any fix will also need to clear the poisoned entries in _cart_cache, either by restarting the workers or by discarding the cache. Holding off on the fix as you asked.
Real run recorded with claude-code / claude-opus-5-5. Output is shown verbatim, unmodified.
它做什麼
一條針對原因不明故障的簡短工作規則:先蒐集證據、確定原因,再修改產品程式碼。
運作方式
- 將觀察到的現象與推測的原因分開。
- 追蹤輸入、狀態變化與模組邊界,依證據強弱與排除成本為各個假設排序。
- 在找到能解釋所有證據的機制之前不修改程式碼;找到後回報原因與證據。除非任務明確要求修正,否則不動手修。
適合情境
偶發 bug、效能退化,以及「測試環境正常、正式環境出錯」這類問題。
需要了解
出自開源專案 Caveman 的一組通用工作方式。指令本身不帶 Caveman 品牌,放到任何專案都能使用。
純指令檔:沒有腳本、不連網、不寫入檔案。 壓縮檔中另附 LICENSE、NOTICE 與 agents/openai.yaml(供 Codex 使用的顯示名稱與預設提示詞)。