Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Race Condition #4699

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Conversation

HanetakaChou
Copy link

@HanetakaChou HanetakaChou commented Mar 16, 2025

when another thread resizes the table, it will perform the following two steps:

  • ANOTHER-1. allocate the new meomry block
  • ANOTHER-2. copy the existing data from the old memory block to the new meomry block

there can be such timeline:

  • ANOTHER-1. another thread has allocated the new meomry block
  • CURRENT. current thread is reading the uninitialized data from the new memory block
  • ANOTHER-2. another thread will copy the existing data from the old memory block to the new meomry block, but this will not affect the uninitialized data read by the current thread
		int uniqueId = body.getWorldArrayIndex();
		const int INVALID_SOLVER_BODY_ID = -1;
		m_kinematicBodyUniqueIdToSolverBodyTableMutex.lock();
		if (m_kinematicBodyUniqueIdToSolverBodyTable.size() <= uniqueId)
		{
			// ~~m_kinematicBodyUniqueIdToSolverBodyTableMutex.lock();~~
			// now that we have the lock, check again
			if (m_kinematicBodyUniqueIdToSolverBodyTable.size() <= uniqueId)
			{
				m_kinematicBodyUniqueIdToSolverBodyTable.resize(uniqueId + 1, INVALID_SOLVER_BODY_ID);
			}
			// ~~m_kinematicBodyUniqueIdToSolverBodyTableMutex.unlock();~~
		}
                // ** the following read may have **uninitialized** data without lock ** !!!
		solverBodyId = m_kinematicBodyUniqueIdToSolverBodyTable[uniqueId];
		m_kinematicBodyUniqueIdToSolverBodyTableMutex.unlock();

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.

1 participant