Skip to content

Commit e968607

Browse files
committed
[Patch] Fix streaming region load slot race
1 parent 6e3ae8a commit e968607

1 file changed

Lines changed: 17 additions & 4 deletions

File tree

Sources/UntoldEngine/Systems/StreamingRegionManager.swift

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,13 +109,27 @@ public extension StreamingRegionManager {
109109
}
110110
}
111111

112-
// Load new regions (respect concurrent limit)
112+
// Load new regions (respect concurrent limit). Reserve each slot before
113+
// the task can run so a fast empty-region load cannot complete and remove
114+
// its active entry before this method records the task.
113115
for regionId in toLoad.prefix(snapshot.availableSlots) {
116+
let reserved = withStateLock { () -> Bool in
117+
guard var region = regions[regionId], region.state == .unloaded else {
118+
return false
119+
}
120+
region.state = .loading
121+
regions[regionId] = region
122+
return true
123+
}
124+
guard reserved else { continue }
125+
114126
let task = Task {
115127
await loadRegion(id: regionId)
116128
}
117129
withStateLock {
118-
activeLoadTasks[regionId] = task
130+
if regions[regionId]?.state == .loading {
131+
activeLoadTasks[regionId] = task
132+
}
119133
}
120134
}
121135
}
@@ -172,9 +186,8 @@ extension StreamingRegionManager {
172186
}
173187
}
174188

175-
// Mark as loading
176189
guard var region = withStateLock({ () -> StreamingRegion? in
177-
guard var region = regions[id], region.state == .unloaded else {
190+
guard var region = regions[id], region.state == .unloaded || region.state == .loading else {
178191
return nil
179192
}
180193
region.state = .loading

0 commit comments

Comments
 (0)