Skip to content

Commit

Permalink
Fix race condition in create_children. (leela-zero#2573)
Browse files Browse the repository at this point in the history
Node is created in create_children, but visits and eval were
assigned later and not guarded by a lock. If other thread was
in uct_select_child before updating the visits, it would read
0 visits and crash in FPU calculation due to divide by zero.
  • Loading branch information
Ttl authored and TFiFiE committed May 27, 2020
1 parent bfd8219 commit a06a464
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/UCTNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ bool UCTNode::create_children(Network & network,
}

link_nodelist(nodecount, nodelist, min_psa_ratio);
// Increment visit and assign eval.
update(eval);
expand_done();
return true;
}
Expand Down
5 changes: 4 additions & 1 deletion src/UCTSearch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ SearchResult UCTSearch::play_simulation(GameState & currstate,
UCTNode* const node) {
const auto color = currstate.get_to_move();
auto result = SearchResult{};
auto new_node = false;

node->virtual_loss();

Expand All @@ -256,6 +257,7 @@ SearchResult UCTSearch::play_simulation(GameState & currstate,
get_min_psa_ratio());
if (!had_children && success) {
result = SearchResult::from_eval(eval);
new_node = true;
}
}
}
Expand All @@ -272,7 +274,8 @@ SearchResult UCTSearch::play_simulation(GameState & currstate,
}
}

if (result.valid()) {
// New node was updated in create_children.
if (result.valid() && !new_node) {
node->update(result.eval());
}

Expand Down

0 comments on commit a06a464

Please sign in to comment.