Skip to content

Commit

Permalink
[XLA:Python] Fix a data race between the thread destructor and garbag…
Browse files Browse the repository at this point in the history
…e collection.

We need to ensure accesses to entries_ in the thread-local state's destructor are ordered with respect to Python GC in another thread, but acquiring the mutex around the thread-local state table first is enough to ensure that.

PiperOrigin-RevId: 694461960
  • Loading branch information
hawkinsp authored and tensorflower-gardener committed Nov 8, 2024
1 parent 670685b commit 1dd56fd
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions third_party/xla/xla/python/config.cc
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,12 @@ ThreadLocalConfigState::ThreadLocalConfigState() {
}

ThreadLocalConfigState::~ThreadLocalConfigState() {
// We may not hold the GIL, so we must use deferred destruction.
xla::GlobalPyRefManager()->AddGarbage(absl::MakeSpan(entries_));
// It's important that we remove the thread-local state before we access
// entries_. This ensures that accesses to entries_ are ordered with respect
// any garbage collection.
GlobalConfigState::Instance().RemoveThreadLocalState(this);
// We do not hold the GIL, so we must use deferred destruction.
xla::GlobalPyRefManager()->AddGarbage(absl::MakeSpan(entries_));
}

void ThreadLocalConfigState::Set(int key, nb::object value) {
Expand Down

0 comments on commit 1dd56fd

Please sign in to comment.