TfRefPtr: the thread that drops the last reference deletes the object without synchronizing with other threads' decrements
Description of Issue
When several threads hold a TfRefPtr to the same object and drop their references concurrently, the thread that drops the last reference deletes the object without acquiring the other threads' releases. Writes those threads made to the object before dropping their reference are therefore not guaranteed to be visible to the destructor. Under the C++ memory model this is a data race, and ThreadSanitizer reports it.
All the decrement paths use release ordering only, and nothing on the delete path acquires:
Tf_RefPtr_Counter::RemoveRef:fetch_sub(1, std::memory_order_release) == 1(refPtr.h:552)Tf_RefPtr_UniqueChangedCounter::RemoveRef:fetch_sub(1, release)andcompare_exchange_weak(..., release)(refPtr.h:511, :518)Tf_RefPtr_UniqueChangedCounter::_RemoveRefMaybeLocked:compare_exchange_weak(..., release)andfetch_add(1, release)(refPtr.cpp:50, :58)TfRefPtr::_RemoveRefthendeletes when the counter reports the last reference, with no acquire fence (refPtr.h:1093).
A release on the decrement orders the releasing thread's earlier writes before its decrement, but only an acquire on the deleting side makes them visible to the thread that runs the destructor. std::shared_ptr implementations and Boost's intrusive reference counting use acq_rel on the decrement, or release plus an acquire fence on the zero path, for this reason.
It shows up in USD itself. Running an application's test suite under ThreadSanitizer with OpenUSD built with -fsanitize=thread, PcpCache::~PcpCache() hits it: it resets _rootLayer and _sessionLayer in parallel WorkDispatcher tasks (cache.cpp:132-133). TSan then reports the decrement in TfRefPtr<SdfLayer>::Reset() on one thread against SdfLayer::~SdfLayer() (reached from PcpLayerStack::_BlowLayers()) on another.
On x86 this is benign in practice. On weakly ordered CPUs (ARM, including Apple Silicon) the destructor could in principle observe stale member values written by another thread just before it dropped its reference.
Suggested fix: use std::memory_order_acq_rel for the decrementing read-modify-writes, so the one that observes the last reference acquires every earlier release. Alternatively, keep release on the decrement and add std::atomic_thread_fence(std::memory_order_acquire) in TfRefPtr::_RemoveRef before delete.
- With
acq_relon the three decrement sites inrefPtr.h, the reproducer below runs clean under TSan (5 of 5 runs; the unmodified header reports the race in every run). - The fence variant is also correct by the memory model, but ThreadSanitizer does not model standalone fences, so it keeps reporting it.
_RemoveRefMaybeLockedinrefPtr.cppneeds the same change asrefPtr.h.
Steps to Reproduce
- Save this as
refptr_race.cpp. Each thread writes only its own slot of a shared object and then drops its reference; the destructor, run by whichever thread drops the last reference, reads every slot:
#include <pxr/base/tf/refBase.h>
#include <pxr/base/tf/refPtr.h>
#include <cstdio>
#include <thread>
#include <vector>
PXR_NAMESPACE_USING_DIRECTIVE
constexpr int kThreads = 4;
template <class Base>
struct Obj : Base {
int slot[kThreads] = {};
~Obj() override {
int sum = 0;
for (int v : slot) sum += v;
if (sum < 0) std::printf("unreachable\n");
}
};
template <class Base>
void run(const char *name) {
for (int iter = 0; iter < 100; ++iter) {
TfRefPtr<Obj<Base>> p = TfCreateRefPtr(new Obj<Base>);
std::vector<std::thread> threads;
for (int t = 0; t < kThreads; ++t) {
threads.emplace_back([q = p, t]() mutable {
q->slot[t] = t + 1; // plain write, owned by this thread
q.Reset(); // release-only decrement; the last one deletes
});
}
p.Reset();
for (auto &th : threads) th.join();
}
std::printf("%s done\n", name);
}
int main() {
run<TfSimpleRefBase>("TfSimpleRefBase (Tf_RefPtr_Counter)");
run<TfRefBase>("TfRefBase (Tf_RefPtr_UniqueChangedCounter)");
}- Build it with ThreadSanitizer against any USD install. The reference-count logic is inline in
refPtr.h, so USD itself does not need to be instrumented:
clang++ -std=c++17 -fsanitize=thread -g -O1 refptr_race.cpp \
-I$USD/include -L$USD/lib -Wl,-rpath,$USD/lib -lusd_tf -o refptr_race
./refptr_race(With a GCC-built USD on Linux, also pass -DARCH_HAS_GNU_STL_EXTENSIONS so that Clang sees the library's TfHashMap layout; it does not affect this code.)
- ThreadSanitizer reports a data race for both counter variants on every run (3 of 3 runs; namespace prefix and module offsets trimmed below), for example:
WARNING: ThreadSanitizer: data race
Read of size 4 at 0x720800000210 by main thread:
#0 Obj<TfSimpleRefBase>::~Obj() refptr_race.cpp:16:20
#1 Obj<TfSimpleRefBase>::~Obj() refptr_race.cpp:14:21
#2 TfRefPtr<Obj<TfSimpleRefBase>>::_RemoveRef(TfRefBase const*) const pxr/base/tf/refPtr.h:1096:13
#3 TfRefPtr<Obj<TfSimpleRefBase>>::operator=(TfRefPtr<Obj<TfSimpleRefBase>>&&) pxr/base/tf/refPtr.h:768:9
#4 TfRefPtr<Obj<TfSimpleRefBase>>::Reset() pxr/base/tf/refPtr.h:968:15
#5 void run<TfSimpleRefBase>(char const*) refptr_race.cpp:32:11
#6 main refptr_race.cpp:39:5
Previous write of size 4 at 0x720800000210 by thread T2:
#0 void run<TfSimpleRefBase>(char const*)::'lambda'()::operator()() refptr_race.cpp:28:28
Location is heap block of size 32 at 0x720800000200 allocated by main thread:
#0 operator new(unsigned long)
#1 void run<TfSimpleRefBase>(char const*) refptr_race.cpp:24:48The same report appears for Obj<TfRefBase> (Tf_RefPtr_UniqueChangedCounter).
System Information (OS, Hardware)
Arch Linux x86_64 (kernel 7.2), AMD Ryzen 9 5950X.
Package Versions
OpenUSD v26.05. The code is unchanged in v26.08 and on dev at 2a9a571d0f9957bad5c4a59ba859ebb8df518c51.
Build Flags
- Reproducer: clang 22.1.8,
-fsanitize=thread -g -O1, against a Release OpenUSD 26.05 built with GCC 16 bybuild_usd.py. - Also reproduced with OpenUSD 26.05 itself built with clang 22.1.8 and
-fsanitize=thread.
Source: PixarAnimationStudios/OpenUSD