Repository navigation
Description
What
ThreadSanitizer reports a data race between a free inside _ext and a read
by Nix's own curl file-transfer worker thread, in nix::Activity::~Activity.
The soak had already finished at that point:
SOAK 10.929 lane=3 END tests/nanopynix/inproc/test_inproc.py::test_inproc_eval_value_navigation ok
SOAK END ok=68 fail=0
==================
WARNING: ThreadSanitizer: data race (pid=2)
So this is a teardown defect, and not something the schedule drives.
Write of size 8 at 0x72040001b7d0 by thread T7 'nix-store_3':
#0 operator delete(void*, unsigned long)
nixidae/nixidae#30..#5 (_ext.cpython-314-x86_64-linux-gnu.so)
nixidae/nixidae#35 _PyEval_EvalFrameDefault
Previous read of size 8 at 0x72040001b7d0 by thread T27:
#0 nix::Activity::~Activity() ../logging.cc:480
nixidae/nixidae#32 std::unique_ptr<nix::Activity>::~unique_ptr()
nixidae/nixidae#33 nix::curlFileTransfer::TransferItem::~TransferItem() ../filetransfer.cc:216
...
nixidae/nixidae#51 nix::curlFileTransfer::workerThreadMain() ../filetransfer.cc:911
nixidae/nixidae#52 nix::curlFileTransfer::workerThreadEntry() ../filetransfer.cc:998
Why it is real
Activity::~Activity calls logger.stopActivity(id). It holds a Logger &,
so it reads the logger object at destruction.
_ext frees on a Store thread while that read is in flight. This is a read of
memory that another thread is freeing, which is a use-after-free and not only
an ordering question.
Thread T27 is Nix's own thread. curlFileTransfer starts it in its
constructor (filetransfer.cc:842), which getDefaultSubstituters() reaches
through HttpBinaryCacheStoreConfig::openStore. Nothing in this repository
starts it, joins it, or knows when it stops.
Why nanopynix meets it
install_logger puts a PyLogger in nix::logger, and remove_logger
takes it out again when a session closes. The curl worker thread outlives the
session's own threads, so it can still be destroying a TransferItem, and
with it an Activity, after the session decides the logger is finished with.
Upstream Nix does not meet this, because the process ends rather than swapping
its logger out.
How it was found
The concurrency soak, on ci-develop, run 30913072524, seed 1, TSAN. The
schedule that preceded it is in the artifact
soak-nix_2_34-run1-inproc.json.
What to check
- Whether
remove_loggeris the free. The stack shows_extand nothing
more, because the extension is built without frame pointers in that path.
A build with symbols would name it. - Whether a
Storethat reaches a substituter is required to reproduce it.
getDefaultSubstitutersis what starts the curl thread, so a soak with no
substituter configured may never see this. - What a correct teardown looks like. Nix gives no way to join the curl
worker, so leaving the logger installed for the life of the process is one
answer, and it costs aPyLoggerthat never goes away.
Acceptance criteria
- The TSAN soak reports no race in
nix::Activity::~Activityover its five
seeds. - The reason the chosen teardown is correct is written where the next reader
ofinstall_loggerandremove_loggerwill find it.
Activity
fix(bindings): install one logger at import, and never free it
fix(bindings): install one logger at import, and never free it