Skip to content

Conversation

@jstone-lucasfilm
Copy link
Member

@jstone-lucasfilm jstone-lucasfilm commented Dec 27, 2025

  • Fix a race condition in the Document cache, where the valid flag could be simultaneously written and read from different threads.
  • Use a std::shared_mutex pattern in the Document cache for greater robustness and clearer guarantees.
  • Add an initial Thread Safety section to the Developer Guidelines.

@jstone-lucasfilm jstone-lucasfilm changed the title Clarify and improve thread safety Improvements to thread safety Dec 27, 2025
@jstone-lucasfilm jstone-lucasfilm force-pushed the dev_thread_safety branch 2 times, most recently from 2844810 to 71e3c67 Compare December 28, 2025 05:18
- Fix a race condition in the Document cache, where the `valid` flag could be simultaneously written and read from different threads.
- Add an initial Thread Safety section to the Developer Guidelines.
void setDocument(weak_ptr<Document> document)
{
_doc = document;
invalidate();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if another thread tries to fetch cached data in the miniscule moment after _doc is set, but before invalidate() has had the time to set _valid to false?
I would recommend using a C++17 std::shared_mutex instead for robustness. It allows multiple-reader/single-writer semantics.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a really good suggestion, @JGamache-autodesk, and I've updated the PR to use this approach.

Following up on review notes from Jerry Gamache, this change switches over to a std::shared_mutex pattern, which should be simpler and more robust.
const string& nodeGraphName = elem->getAttribute(PortElement::NODE_GRAPH_ATTRIBUTE);
const string& nodeString = elem->getAttribute(NodeDef::NODE_ATTRIBUTE);
const string& nodeDefString = elem->getAttribute(InterfaceElement::NODE_DEF_ATTRIBUTE);
lock.unlock();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! That is the best currently possible with C++ (up to 23). Rechecking the _valid condition after re-acquiring the lock makes sure we handle the case where another thread pre-emptively rebuilds in the small window after the shared lock is released, especially since this thread now has to wait for all shared locks to be released before proceeding.

LGTM!

@jstone-lucasfilm jstone-lucasfilm merged commit 5961041 into AcademySoftwareFoundation:main Jan 5, 2026
33 checks passed
@jstone-lucasfilm jstone-lucasfilm deleted the dev_thread_safety branch January 5, 2026 18:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants