Skip to content

Commit

Permalink
fix(Query): Fix wrong path response for k-shortest paths (#6437)
Browse files Browse the repository at this point in the history
The k-shortest path query sometimes returns a wrong response. The cause
of this issue is inappropriate use of sync.pools. Whenever a valid path
is found a new variable is assigned with this route and that variable is
appended in the kroutes (which is then parsed to produce response).
But the route is also put in the sync.pool. Since the route is a
structure containing a pointer to slice along with other fields, when
it is fetched back from the pool in future then any mutation on this fetched
route causes modification in the kroutes, resulting in garbage response.

This issue is fixed by making a copy of the route struct rather
than just assigning it to a variable, which causes a shallow copy.
  • Loading branch information
ahsanbarkati authored Sep 14, 2020
1 parent 5c33428 commit 4792d8b
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion query/shortest.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,8 +340,13 @@ func runKShortestPaths(ctx context.Context, sg *SubGraph) ([]*SubGraph, error) {
continue
}

// Add path to list.
// Add path to list after making a copy of the path in itemRoute. A copy of
// *item.path.route is required because it has to be put back in the sync pool and a
// future reuse can alter the item already present in kroute because it is a pointer.
itemRoute := make([]pathInfo, len(*item.path.route))
copy(itemRoute, *item.path.route)
newRoute := item.path
newRoute.route = &itemRoute
newRoute.totalWeight = item.cost
kroutes = append(kroutes, newRoute)
if len(kroutes) == numPaths {
Expand Down

0 comments on commit 4792d8b

Please sign in to comment.