Transfer Planner crashes the game when a vehicle is destroyed while it is open (ArgumentOutOfRangeException in DrawPlanWindow)

Laurens

Member
Messages
11
Reaction score
12
Title: Crash (ArgumentOutOfRangeException in TransferPlanner.DrawPlanWindow) when any vehicle is destroyed while the Transfer Planner is open

Game Version​

2026.11.25.5949

Operating System​

Windows 11 Home (build 26300)

What Happened​

With the Transfer Planner open, destroying a different vehicle crashes the game on the next frame. KSA.exe was launched directly, with no mod loader and only Core enabled:
Code
Unhandled exception System.ArgumentOutOfRangeException: index ('5') must be less than '5'. (Parameter 'index')
   at KSA.CelestialSystem.GetIndex(Int32 index)
   at KSA.TransferPlanner.DrawPlanWindow(IGameViewport inViewport)
   at KSA.Program.OnDrawUiThreadSafe(Double dt)
   at KSA.Program.OnFrame(Double currentPlayerTime, Double dtPlayer)
   at KSA.App.Run()
   at KSA.Program.Main(String[] inArgs).
The window freezes on that frame and the process then exits ("abnormal-exit" log).

What Was Expected​

Destroying a vehicle that isn't the planner's source or destination should leave the planner showing the same source. If the destroyed vehicle was the source or destination, the planner should clear that selection, as it does when the window is first opened. In neither case should the game crash.

Reproduction Steps​

  1. Launch KSA with no mods. In the configuration window, keep the defaults (System: Earth and Moon, Vehicle: Rocket, Location: Orbit around Earth) and press START KSA.
  2. Open the terminal with \ and enter: duplicate Rocket Copy
  3. Enter: control Copy
  4. Close the terminal (\) and open the Transfer Planner with F4. It shows SOURCE = Copy.
  5. Open the terminal again and enter: destroy Rocket
  6. The game crashes on the next frame with the exception above.

Reproduction Rate​

Always (deterministic).

Additional Info​

  • Video/Screenshots: none needed. The steps reproduce it every time, and the full log of the crashing run is attached.
  • Save File: not needed. The steps start from a fresh game with the default settings.
  • Other Notes:

    Cause (from reading the code):
    TransferObject stores Astronomical.LookupIndex, a slot number in CelestialSystem._all, rather than the object. LookupCollection.Deregister removes an item by moving the last item into the freed slot and shrinking the list. As a result, TransferPlanner._sourceBody / _destinationBodygo stale whenever any vehicle is removed:
    • If the planner's vehicle was last in the list and an earlier vehicle is removed, the stored index equals the new Count, so GetIndex throws (this report). In the steps above, Copy was created last (index 5), destroy Rocket moved Copy into Rocket's slot, and the planner still asked for index 5 of 5.
    • If the planner's vehicle itself is removed, the stored index now refers to whichever vehicle was moved into the slot. The planner silently switches to an unrelated vehicle, and code that needs a Vehicle can hit a NullReferenceException in DrawPlanWindow.
    • CelestialSystem.Rename (deregister + register) moves indices around in the same way.
    The same happens through every removal path: crashes, Universe Manifest → Clear Debris, docking, recovery and the destroy command. A natural repro is a save that already has debris, a newly launched craft planned from, and then Clear Debris. Multiplayer mods make it frequent, because remote players' craft are added and removed all the time; one player crashed this way five times in a day.

    Suggested fix: have TransferObject hold the Astronomical (or its Id/hash) instead of the slot index, and treat a disposed or missing body as "N/A". Or reset the planner's selections from CelestialSystem.Deregister.

    Related, same window (found by reading the code; not reproduced on demand because it depends on timing): the porkchop calculation (TransferTask.WorkerTask.CalculateAutomaticTransfer → OrbitalTransfers.BuildFlightPlan → FlightPlan.CalculateConicPatch) enumerates patch.PrimaryBody.Children on thread-pool threads. Meanwhile the main thread adds to or removes from those lists, through staging (AdoptSeparatedVehicle), SOI changes (ApplyPendingParentChildrenFix), new vehicles and CelestialSystem.Deregister. If that happens during CALCULATE, the worker throws an unhandled "Collection was modified; enumeration operation may not execute" and the process exits. Seen in the field:
    Code
    Unhandled exception System.InvalidOperationException: Collection was modified; enumeration operation may not execute.
       at System.Collections.Generic.List`1.Enumerator.MoveNext()
       at KSA.FlightPlan.CalculateConicPatch(...)
       at KSA.FlightPlan.ComputeCompleteTrajectory(...)
       at KSA.OrbitalTransfers.BuildFlightPlan(...)
       at KSA.TransferTask.WorkerTask.CalculateAutomaticTransfer(...)
       at KSA.TransferTask.WorkerTask.ThreadPoolCallback(Object threadContext)
    Workers should iterate a snapshot of the children, and an exception in a worker should not take down the process.
 

Attachments

Upvote 1
Back
Top