The International Simutrans Forum

 

Author Topic: Signal boxes still incorrectly sorted in objlist  (Read 354 times)

0 Members and 1 Guest are viewing this topic.

Offline ACarlotti

  • *
  • Posts: 478
Signal boxes still incorrectly sorted in objlist
« on: April 23, 2019, 01:06:04 AM »
Every so often I track down a bug and look into Git history to find out how it was introduced, to discover that it was my fault. Well, I've just had that happen again - and the bug in question caused us to spend 6 months debugging a desync. (To be fair to myself, I didn't actually introduce it; I merely changed it by only partly removing the undefined behaviour.)

The bug is really simple, and is roughly as explained here. However, I was incorrect in claiming it was a difference between 32-bit and 64-bit builds. A negative index was being used into an array (giving the priority of different objects types in the ordering of the object list), which is undefined behaviour. (It also originally wasn't assigned a priority, which was an oversight). Thus the value used for the priority could vary between different compilers and different settings within a compiler. It could even depend on what data happened to be nearby in memory.

I 'fixed' the issue by casting the object type number to uint8 ... in one place. I failed to notice that the type_to_pri lookup array was being used in another location as well - hence the indeterminacy and resulting desync occurring  on the server game, and most nearly pinned down here. I've been meaning to investigate the bug further since then, as I recently discussed here.

Knowing that there was an issue with the ordering of the signalbox and pillar, it was actually quite easy to track down the bug (though I was certainly lucky that my gcc linux builds were producing the 'wrong' ordering). I've now uploaded the fix, along with another commit removing the changes away from using first_obj that were made in an attempt to address desync issues, but also led (or might have led) to segfaults caused by race conditions (as discussed here).

Offline jamespetts gb

  • Simutrans-Extended project coordinator
  • Moderator
  • *
  • Posts: 18721
  • Cake baker
    • Bridgewater-Brunel
  • Languages: EN
Re: Signal boxes still incorrectly sorted in objlist
« Reply #1 on: April 23, 2019, 09:42:44 AM »
Splendid, thank you very much for that. I have now incorporated the fix. Thank you also for the explanation: that is helpful. I had never looked in any detail at the object list system, and so was not aware of how the priority ordering worked; it does make sense that such a system should exist and that the indeterminacy of ordering apropos the signalbox that we detected in the server game would relate to it. Thank you for your work on this.