Fixed race condition problem on shared variable query