Skip to content

Commit

Permalink
Fix a potential race condition when fetching a map tile
Browse files Browse the repository at this point in the history
We clear the missingTiles set and then fill it with a given coord
only after we schedule it to be fetched. In an unfavourable case it may
cause the request to be dropped before we add it to the missingTiles set.
So, instead of clearing it fill it with the necessary tiles, and later
remove some of them if they proof already fetched (in cache).
  • Loading branch information
pwiecz committed Apr 30, 2022
1 parent 39934cc commit 5625d46
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions gui/map_drawer.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,18 @@ func (l *lockedCoordSet) Remove(coord osm.TileCoord) {
defer l.mutex.Unlock()
delete(l.set, coord)
}
func (l *lockedCoordSet) Set(set map[osm.TileCoord]struct{}) {
l.mutex.Lock()
defer l.mutex.Unlock()
for coord, _ := range l.set {
if _, ok := set[coord]; !ok {
delete(l.set, coord)
}
}
for coord, _ := range set {
l.set[coord] = struct{}{}
}
}
func newLockedCoordSet() *lockedCoordSet {
return &lockedCoordSet{
set: make(map[osm.TileCoord]struct{}),
Expand Down Expand Up @@ -630,7 +642,7 @@ func (w *MapDrawer) redrawTiles() {
delete(tileCoords, coord)
}
}
w.missingTiles.Clear()
w.missingTiles.Set(tileCoords)
for coord := range tileCoords {
w.tryShowTile(coord)
}
Expand Down Expand Up @@ -674,7 +686,9 @@ func (w *MapDrawer) tryShowTile(coord osm.TileCoord) {
}
wrappedCoord.X %= maxCoord
tileImage := w.tileCache.Get(wrappedCoord)
if tileImage == nil {
if tileImage != nil {
w.missingTiles.Remove(coord)
} else {
go func() {
w.fetchTile(coord)
}()
Expand Down

0 comments on commit 5625d46

Please sign in to comment.