Aurora 4x

C# Aurora => C# Bug Reports => Topic started by: Steve Walmsley on April 26, 2020, 05:13:13 AM

Title: Disappearing Ground Units
Post by: Steve Walmsley on April 26, 2020, 05:13:13 AM
The 'Disappearing Ground Units' bug is probably the most significant remaining at this point. Ground units vanish post-save with no warning or error popups. Therefore, I am going to dedicate this thread to that specific issue.

The bug is relatively rare but has affected several different people and it hard to track because any db is always post-bug. If you have suffered this bug, please post in this thread with as much detail as possible and any theories you may have as to the cause.
Title: Re: Disappearing Ground Units
Post by: RagnarVaren on April 26, 2020, 08:26:20 AM
I've looked at a DB that someone else gave me that had the bug which I've since then deleted but I can't remember if the bug also effected NPR ground units. Might help narrow it down if it's a general issue with saving the ground units into the DB that probably only happens in specific circumstances or if it's some weird player only bug.
Title: Re: Disappearing Ground Units
Post by: Ametsala on April 26, 2020, 08:51:08 AM
I've had this in 1. 6. 3.

At some point I noticed that the first set of 10 infantry units had vanished, but the second set of 10, that was trained later, was still there.

The ground commanders still had in their history the assignment to lead those units, but that was the only trace I could find.

I ran some tests on a new game (v1. 6. 3) recruiting ground units and saving and loading, but couldn't reproduce this.  I'll put the tests in an off-topic, in case they are of any use at least in finding out what isn't the cause:
Off-Topic: tests • show

New game
Instant infantry
Assign commanders
Saved and restarted Aurora.
Start recruiting second set of infantry.
Save & restart
Advance time 5 days.
Save & restart
Advance time until second set ready
Save & restart
Assign Commanders to second set
Save & restart
Start third set of infantry and advance time until almost ready.  (Within one 5 day increment from finishing. )
Save & restart
Advance time until ready
Save
Advance time couple times
Start 4th set
Advance time until date of ready is same as current date, but not ready because 5 day pulse didn't happen.
Save & restart
Advance time until it is 23:59:55 on the day before next 5 day pulse.
Save & restart

Test2:
New game
Initial ground units not instanted (except research)
First set recruited ok
Started recruiting 2nd set
Advanced time
Save & restart
2nd set recruited ok
Save & restart
Title: Re: Disappearing Ground Units
Post by: Desdinova on April 26, 2020, 10:31:45 AM
This also happened to me, I think in 1.5. I no longer have the db but I posted it in the bug thread.
Title: Re: Disappearing Ground Units
Post by: Kristover on April 26, 2020, 10:36:00 AM
The one time I had it was after leaving the game open for about three hours after saving it while i did other things and coming back and finding the game locked in some process.  I had to force close and when I reloaded the save, the units were gone.
Title: Re: Disappearing Ground Units
Post by: Nori on April 26, 2020, 12:15:11 PM
It's possible it is related to not saving for a extended period of time. The three times I have had it happen, the game had been open and unsaved for many hours. I had a theory that maybe GU complexity was to blame, but I tested that and no go. I did notice once when it happened that the only units that disappeared were new units. All the ones created and organized during that play period were the ones to disappear.
Title: Re: Disappearing Ground Units
Post by: Kristover on April 26, 2020, 12:27:18 PM
It's possible it is related to not saving for a extended period of time. The three times I have had it happen, the game had been open and unsaved for many hours. I had a theory that maybe GU complexity was to blame, but I tested that and no go. I did notice once when it happened that the only units that disappeared were new units. All the ones created and organized during that play period were the ones to disappear.

It’s funny you’ve noticed this because I’ve arrived at the same conclusion.  I’ve only had the bug happen once but I’ve left the game open and attended for an hour plus a couple of times and EVERY time I’ve come back to it, I’ve had to force close it because it was locked up.  I’ve started saving every year and if I plan on being up for more than a few moments, just saving and closing out the game.
Title: Re: Disappearing Ground Units
Post by: Geebee on April 26, 2020, 03:21:45 PM
I had it happen to seemingly every GU that was built past a certain date but I only noticed when I loaded the game up today.
I did save multiple times through yesterday and I did use spacemaster to redo a survey craft around the time it popped my GU's to.
But my planetary defence groups were still being made, my game was open for a long time though.
Title: Re: Disappearing Ground Units
Post by: Froggiest1982 on April 26, 2020, 03:58:56 PM
Hi Steve, the only thing that comes to mind is that now we do save manually while aurora VB6 saved the database "on the go" at every click. This means less information to be stored but more frequently. The question now is: why ground units though? What is the save sequence? Do they come last? I don't know.

Personally I haven't play long campaigns yet but since reading the bug post and seeing many bugs related to after save load I have came to the conclusion of a double save. I started in 1.6.3 with the below method:

Usually I do save then a run another cycle and save again so if anything is wrong I reload from previous backup. So far so good, but I guess with the complexity of save increasing there also could be an increase in the chance of an error. As said didnt arrive that far yet.

Another question: Is it possible when you save even if looks like all is good to go somehow there is still a process running in the background therefore if you close Aurora in the meantime you'll end up with a corrupted database? That was the reason of my double save, I thought Aurora wouldn't start a save process until the previous is completed.

EDIT: I also close all windows prior saving.
Title: Re: Disappearing Ground Units
Post by: joshuawood on April 26, 2020, 05:27:49 PM
I had automated assignments off for 60 years, turned them on and the very next incrament where they all got assigned 90% of my 100's of ktons dissapeared, could be related? i have the DB from WAAAY after it so i doubt that will be much help :/
Title: Re: Disappearing Ground Units
Post by: Shuggana on April 27, 2020, 05:52:59 AM
I have experienced this.   Latest release as of this date (1.  8?)

All civilian garrisons have disappeared, my Lunar, Martian and Europa garrisons are all gone.   Every single unit I had on Earth remains, however they have lost their OOB and are all completely detached now. 

Normal game, nothing weird.   Saved a few days ago and took a break.   Came back and only noticed because I was suddenly getting events about unrest on colonies with no military presence. 

Only thing I can think is that, at one point, I did hit the "Clear hierarchy" button when I had made a mess of the OOB but I never checked what the button does. 

Additionally, I had a handful of Marine companies in boarding capable transport bays on ships I had sent to battle.   Ships were decimated but I got no Event for the destruction of the ground unit aboard.   Unsure if that's intentional. 

EDIT:

Also, just checked previous saves (the automatic ones in the root folder) and the problem exists at the earliest point but that was only 20 mins before the most recent save.
Title: Re: Disappearing Ground Units
Post by: Froggiest1982 on April 27, 2020, 07:54:03 AM

Only thing I can think is that, at one point, I did hit the "Clear hierarchy" button when I had made a mess of the OOB but I never checked what the button does. 


Maybe a quick SM test on a new save?
Title: Re: Disappearing Ground Units
Post by: Omnivore on April 27, 2020, 09:54:56 AM
I haven't had this happen to me but I just saw something occur when I saved a game that had been running all night and thought it might be pertinent.

After I clicked save there was a point in time where I could close the game window yet the save hadn't completed yet.  This in and of itself may not be a problem with the way SQLite works in C#, but I have to ask, are you using multiple transactions?  If so, is the ground unit data being saved last? 

If the answer to both of those questions is true, then the following might be relevant:

"In SQLite, only one transaction is allowed to have changes pending in the database at a time. Because of this, calls to BeginTransaction and the Execute methods on SqliteCommand may time out if another transaction takes too long to complete. (Microsoft C# docs)"

Hope this helps,

PS: If this reply is unwelcome, please accept my apologies.
Title: Re: Disappearing Ground Units
Post by: Steve Walmsley on April 27, 2020, 10:27:10 AM
I haven't had this happen to me but I just saw something occur when I saved a game that had been running all night and thought it might be pertinent.

After I clicked save there was a point in time where I could close the game window yet the save hadn't completed yet.  This in and of itself may not be a problem with the way SQLite works in C#, but I have to ask, are you using multiple transactions?  If so, is the ground unit data being saved last? 

If the answer to both of those questions is true, then the following might be relevant:

"In SQLite, only one transaction is allowed to have changes pending in the database at a time. Because of this, calls to BeginTransaction and the Execute methods on SqliteCommand may time out if another transaction takes too long to complete. (Microsoft C# docs)"

Hope this helps,

PS: If this reply is unwelcome, please accept my apologies.

I am using one transaction for each of seventy-eight different table inserts. The ground formations aren't last but they are in the last ten so you might be on to something.

I open a connection and then close for each table as well. Am I better using a single open/close and a single transaction for all inserts?
Title: Re: Disappearing Ground Units
Post by: Omnivore on April 27, 2020, 10:44:14 AM
I haven't had this happen to me but I just saw something occur when I saved a game that had been running all night and thought it might be pertinent.

After I clicked save there was a point in time where I could close the game window yet the save hadn't completed yet.  This in and of itself may not be a problem with the way SQLite works in C#, but I have to ask, are you using multiple transactions?  If so, is the ground unit data being saved last? 

If the answer to both of those questions is true, then the following might be relevant:

"In SQLite, only one transaction is allowed to have changes pending in the database at a time. Because of this, calls to BeginTransaction and the Execute methods on SqliteCommand may time out if another transaction takes too long to complete. (Microsoft C# docs)"

Hope this helps,

PS: If this reply is unwelcome, please accept my apologies.

I am using one transaction for each of seventy-eight different table inserts. The ground formations aren't last but they are in the last ten so you might be on to something.

I open a connection and then close for each table as well. Am I better using a single open/close and a single transaction for all inserts?

I'm not up on the latest but, according to: https://stackoverflow.com/questions/7199305/how-many-inserts-can-be-done-within-one-transaction-in-sqlite, you should probably be safe doing it all in one.  That's an old post but I can't easily imagine why SQLite would be less capable today than 8 years ago. 

If a timeout is indeed the cause then that should solve it.

Hope this helps
Title: Re: Disappearing Ground Units
Post by: Eretzu on April 27, 2020, 10:54:43 AM
I am a software developer, but not any kind of expert (and still quite junior).

Opening only one connection that is then reused is should be faster, but not sure if it is that much faster that it would be useful change.

If saving is only time when data is stored to db, are transactions actually needed?
As far as I understand, the point of transactions is to make make a sequence of edits to database atomic and then be reversible if something goes wrong at any point of sequence. But single insert should be atomic by itself so it should not need transaction as there is no worry of other actors.

Of course as I might be wrong, I would be interested on why transactions are needed.
Title: Re: Disappearing Ground Units
Post by: skoormit on April 27, 2020, 11:01:50 AM
I haven't had this happen to me but I just saw something occur when I saved a game that had been running all night and thought it might be pertinent.

After I clicked save there was a point in time where I could close the game window yet the save hadn't completed yet.  This in and of itself may not be a problem with the way SQLite works in C#, but I have to ask, are you using multiple transactions?  If so, is the ground unit data being saved last? 

If the answer to both of those questions is true, then the following might be relevant:

"In SQLite, only one transaction is allowed to have changes pending in the database at a time. Because of this, calls to BeginTransaction and the Execute methods on SqliteCommand may time out if another transaction takes too long to complete. (Microsoft C# docs)"

Hope this helps,

PS: If this reply is unwelcome, please accept my apologies.

I am using one transaction for each of seventy-eight different table inserts. The ground formations aren't last but they are in the last ten so you might be on to something.

I open a connection and then close for each table as well. Am I better using a single open/close and a single transaction for all inserts?

If these 78 inserts are occurring serially (not in parallel), you aren't facing the problem described here, which occurs when two different transactions want to modify the database at the same time (one transaction has to wait on the other to finish, and it will only wait so long before giving up).

Using a single open/close for a single transaction comprising all 78 inserts would reduce connection overhead (possible significantly), and would make the 78 inserts succeed or fail as a unit (which may not be desired--do you want to rollback all of the changes whenever one of them fails?).
Title: Re: Disappearing Ground Units
Post by: Steve Walmsley on April 27, 2020, 11:19:57 AM
I haven't had this happen to me but I just saw something occur when I saved a game that had been running all night and thought it might be pertinent.

After I clicked save there was a point in time where I could close the game window yet the save hadn't completed yet.  This in and of itself may not be a problem with the way SQLite works in C#, but I have to ask, are you using multiple transactions?  If so, is the ground unit data being saved last? 

If the answer to both of those questions is true, then the following might be relevant:

"In SQLite, only one transaction is allowed to have changes pending in the database at a time. Because of this, calls to BeginTransaction and the Execute methods on SqliteCommand may time out if another transaction takes too long to complete. (Microsoft C# docs)"

Hope this helps,

PS: If this reply is unwelcome, please accept my apologies.

I am using one transaction for each of seventy-eight different table inserts. The ground formations aren't last but they are in the last ten so you might be on to something.

I open a connection and then close for each table as well. Am I better using a single open/close and a single transaction for all inserts?

If these 78 inserts are occurring serially (not in parallel), you aren't facing the problem described here, which occurs when two different transactions want to modify the database at the same time (one transaction has to wait on the other to finish, and it will only wait so long before giving up).

Using a single open/close for a single transaction comprising all 78 inserts would reduce connection overhead (possible significantly), and would make the 78 inserts succeed or fail as a unit (which may not be desired--do you want to rollback all of the changes whenever one of them fails?).

Each insert is a separate function and the functions are called in sequence. Unless there is some asynchronous function in SQLite that continues to execute the transaction after I close the DB connection, they will be happening in sequence.

However, the lack of an error is concerning me. I have had database insert failure errors in the past, but never one without an error popup. Also, given there are so many inserts, why is only this one affected? Plus, formations will not be a particularly large table compared to some others. It is very odd, but the only consistent factor seems to be saves after leaving the game open a long time.
Title: Re: Disappearing Ground Units
Post by: skoormit on April 27, 2020, 11:35:06 AM
It is very odd, but the only consistent factor seems to be saves after leaving the game open a long time.

Some thoughts:
Does SQLLite have a connection pool? If so, maybe something is timing out and/or limiting the number of connections.

Can you change the order of the inserts? If so, does the error still occur for the same insert (or is it linked to the particular spot in line, like, say, the 53rd insert)?
Title: Re: Disappearing Ground Units
Post by: Hastermain on April 27, 2020, 11:36:49 AM
I haven't had this happen to me but I just saw something occur when I saved a game that had been running all night and thought it might be pertinent.

After I clicked save there was a point in time where I could close the game window yet the save hadn't completed yet.  This in and of itself may not be a problem with the way SQLite works in C#, but I have to ask, are you using multiple transactions?  If so, is the ground unit data being saved last? 

If the answer to both of those questions is true, then the following might be relevant:

"In SQLite, only one transaction is allowed to have changes pending in the database at a time. Because of this, calls to BeginTransaction and the Execute methods on SqliteCommand may time out if another transaction takes too long to complete. (Microsoft C# docs)"

Hope this helps,

PS: If this reply is unwelcome, please accept my apologies.

I am using one transaction for each of seventy-eight different table inserts. The ground formations aren't last but they are in the last ten so you might be on to something.

I open a connection and then close for each table as well. Am I better using a single open/close and a single transaction for all inserts?

If these 78 inserts are occurring serially (not in parallel), you aren't facing the problem described here, which occurs when two different transactions want to modify the database at the same time (one transaction has to wait on the other to finish, and it will only wait so long before giving up).

Using a single open/close for a single transaction comprising all 78 inserts would reduce connection overhead (possible significantly), and would make the 78 inserts succeed or fail as a unit (which may not be desired--do you want to rollback all of the changes whenever one of them fails?).

Each insert is a separate function and the functions are called in sequence. Unless there is some asynchronous function in SQLite that continues to execute the transaction after I close the DB connection, they will be happening in sequence.

However, the lack of an error is concerning me. I have had database insert failure errors in the past, but never one without an error popup. Also, given there are so many inserts, why is only this one affected? Plus, formations will not be a particularly large table compared to some others. It is very odd, but the only consistent factor seems to be saves after leaving the game open a long time.

Not a programmer, but given that there been a few reports of planets or even entire systems (as in their bodies, but not the fleets in the system, in my case) disappearing, maybe it's more than one that's being affected?
Title: Re: Disappearing Ground Units
Post by: Kristover on April 27, 2020, 11:41:30 AM
Ships have disappeared as well.  It happened to me after leaving a game untended for a  couple of hours.  I remember seeing it happen to somebody else in one of the bug threads.
Title: Re: Disappearing Ground Units
Post by: skoormit on April 27, 2020, 11:48:48 AM
...the lack of an error is concerning me.

The lack of an error makes me think that the database doesn't think there is an error.
Which makes me think that in these cases you are sending an empty data set to insert.
I'm assuming the program deletes the data already in the table and then inserts all the current data (from the program's internal representation).

If so, what could cause this particular data set (ground units) to be cleared? (Or other data sets, if others sometimes disappear in this manner.)
Inadvertently re-initializing the collection?
Loop iteration off by one?

...the only consistent factor seems to be saves after leaving the game open a long time.

Does that include long-running, active play sessions, or do you mean just a long span of idle time?
I frequently have sessions that span multiple days, with (obviously) lots of very long idle time (10+ hours), and I have not encountered this error.
(That said, I also haven't done any GU stuff in C#, other than making militia to keep commanders busy, and a geo survey deployment.)
Title: Re: Disappearing Ground Units
Post by: skoormit on April 27, 2020, 12:03:00 PM
Which makes me think that in these cases you are sending an empty data set to insert.

Or maybe you aren't sending the insert at all?
At a former place of employment, there was a tale of a legendary bug that for quite a long time had flummoxed a dev team using a dynamic SQL tool to build and execute very long SQL statements.
Very occasionally, the SQL statements would only partially complete.
Eventually they discovered that, for some obscure reason, a piece of string concatenation logic would, in rare circumstances, fail to insert a space between something aliased as "s" and the keyword "top". As a result, the word "stop" would be inserted instead, and the parsing engine, naturally enough, concluded that it should stop parsing this particular statement.

Title: Re: Disappearing Ground Units
Post by: Black on April 27, 2020, 12:15:32 PM
My longest gamy is over 70 years old (1.7.3). I keep this game open on my computer in the background unattended for longer perionds of time (up to several hours). I did most basic things with ground units (over 700000 tons over several planets in several systems + civilian units on 30+ CMCs), but i did no STO weapons or combat.  And I never encountered this bug.
Title: Re: Disappearing Ground Units
Post by: Steve Walmsley on April 27, 2020, 12:22:03 PM
Please can everyone who has encountered this bug tell me how long their campaign had been running (in game years) when the bug happened.
Title: Re: Disappearing Ground Units
Post by: Kristover on April 27, 2020, 12:26:18 PM
In my game where the ships disappeared, I was about 55 years in and was playing pretty loose with ships all over the place.  In the game where my ground units disappeared, I was about 21 years in BUT I was also taking a lot of time to build up a massive ground force and had about 4 divisions of troops with about 12-15 subordinate battalions (my smallest unit) attached. I had JUST created my first set of STO units.
Title: Re: Disappearing Ground Units
Post by: MarcAFK on April 27, 2020, 12:31:03 PM
Which makes me think that in these cases you are sending an empty data set to insert.

Or maybe you aren't sending the insert at all?
At a former place of employment, there was a tale of a legendary bug that for quite a long time had flummoxed a dev team using a dynamic SQL tool to build and execute very long SQL statements.
Very occasionally, the SQL statements would only partially complete.
Eventually they discovered that, for some obscure reason, a piece of string concatenation logic would, in rare circumstances, fail to insert a space between something aliased as "s" and the keyword "top". As a result, the word "stop" would be inserted instead, and the parsing engine, naturally enough, concluded that it should stop parsing this particular statement.
Er. Just a shot in the dark but what about STO and P?
Title: Re: Disappearing Ground Units
Post by: Ametsala on April 27, 2020, 12:35:36 PM
Quote from: Steve Walmsley link=topic=11094. msg128594#msg128594 date=1588008123
Please can everyone who has encountered this bug tell me how long their campaign had been running (in game years) when the bug happened. 

Within the first two years.  It's hard to say exactly, but one of the commanders of the disappeared units was first assigned in January of the first year and promoted in September of the second year without being relieved, and then he was assigned in February of the third year, again without being relieved.

I'd been using a lot of 5 second increments due to fighting my unfriendly neighborhood extra terrestrials, though.
Title: Re: Disappearing Ground Units
Post by: RagnarVaren on April 27, 2020, 02:01:40 PM
In my game where the ships disappeared, I was about 55 years in and was playing pretty loose with ships all over the place.  In the game where my ground units disappeared, I was about 21 years in BUT I was also taking a lot of time to build up a massive ground force and had about 4 divisions of troops with about 12-15 subordinate battalions (my smallest unit) attached. I had JUST created my first set of STO units.

Now that you mention it, the DB I looked at also had the bug occur with STO units, I can't remember the exact context though. Could be the bug has some connection with STOs where it fails to save any of the new units to the DB?
How exactly does the game save, does it copy the existing save game and then edit the existing entries and add new ones or does it dump the entire memory into a new DB file? If it's the former then it could be that it fails to do add the new entries for some reason perhaps in connection with STOs where after failing to add those entries it just refuses to add any new ones which would also be difficult to spot in practice because you'd have to reload the game to see that the units are missing.

Just brainstorming since I don't know how it's coded.
Title: Re: Disappearing Ground Units
Post by: Steve Walmsley on April 27, 2020, 02:05:26 PM
Apart from system bodies, the game deletes the existing table and inserts the data from the program.

Otherwise, I would have to keep track of every object in the program that was changed at some point. I do that for system bodies, which has already led to several hard to find bugs, so I don't want to replicate that for every table.
Title: Re: Disappearing Ground Units
Post by: Argoniur on April 27, 2020, 02:27:33 PM
I was 78 years into the campaign, and it did happen after not saving for about 3 hours and leaving the game running without playing for one hour and a half

Edit: also idk if it's the same for everyone but it happened on two different places, in my second colony and in a planet in a distant system where I was excavating some alien installations, only thing I can think of that they have in common is that they were the latest ground units I had trained, about 2-5 years before the bug happened
Title: Re: Disappearing Ground Units
Post by: Bughunter on April 27, 2020, 02:32:10 PM
I don't know anything about your data structures, but sounds like most logical explanation would be the save loop sometimes don't go over all objects rather than a problem with the inserts. Maybe a problem going through a hierarchical structure like command chain? An early return somewhere? A loop variable not going all the way like i++ not reaching last item in list. Objects moved to some separate/temporary list not included in the save operation.
Title: Re: Disappearing Ground Units
Post by: Steve Walmsley on April 27, 2020, 02:51:19 PM
I don't know anything about your data structures, but sounds like most logical explanation would be the save loop sometimes don't go over all objects rather than a problem with the inserts. Maybe a problem going through a hierarchical structure like command chain? An early return somewhere? A loop variable not going all the way like i++ not reaching last item in list. Objects moved to some separate/temporary list not included in the save operation.

I'm not using loops that way for saving. I use LINQ to retrieve a collection of objects then cycle through using foreach. If that was a problem, it would always be a problem.

BTW do ALL ground units vanish or only some? If only some, are they different in some ways to the survivors?
Title: Re: Disappearing Ground Units
Post by: Kristover on April 27, 2020, 02:57:29 PM
I don't know if this helps or not but I did had another recurrence of the 'disappearing' bug and have the db attached - in this particular case I lost all my ships and strangely enough, the most recent CMC militia unit (it was the only new ground unit constructed since the previous save cycle a year earlier).  This case, I saved the game and watched the hourglass disappear indicating save complete (I think I even opened up a window and adjusted one ships error right before leaving it untended so the save was complete) and then left it unattended for an hour while I did a work Zoom conference.  Came back and saw the hourglass had come back and after waiting near an hour for the hourglass to go away while I did other things I forced closed it.  When I reloaded the game and took inventory, all the ships I had constructed since the beginning were gone - you can still see their fleets they were part of in the ship tab and their designs but no ship entries - and although I saw all of my ground troops were there, the civilian CMC unit was gone.  Just as an additional oddity, all the commanders of the ships are still there in the commander tabbed but registering as unassigned - BUT I had assigned one of my spare officers, Captain Keith Derouen, to command the civilian force unit (Broker Mineral Group - Cybele Attachment) and he is there but still registered as assigned to the unit as if it still exists which is different than the ship commanders who register as unassigned.  If I hit any increment of time to progress, I get an unending amount of Function #787 Sequence Contains no Elements errors.  The attached DB is immediately after reloading the first time so it is RIGHT after the error occurred.

The game is version 1.7.3 (I haven't updated to 1.8 yet) and it is 18 years in. 
Title: Re: Disappearing Ground Units
Post by: skoormit on April 27, 2020, 03:09:11 PM
Apart from system bodies, the game deletes the existing table and inserts the data from the program.

Does the delete for each table happen immediately prior to the insert, or do you do all the deletes at once and then all of the inserts?

I know you are opening and closing a connection for each insert. Are you reusing the same connection, or creating a new connection each time?

What would happen if you tried to insert a duplicate primary key? Would that possibly result in a silent failure that either ignores the duplicate row or overwrites the original with the duplicate?
Title: Re: Disappearing Ground Units
Post by: Argoniur on April 27, 2020, 03:10:44 PM
Quote from: Argoniur link=topic=11094. msg128629#msg128629 date=1588015653
I was 78 years into the campaign, and it did happen after not saving for about 3 hours and leaving the game running without playing for one hour and a half

Edit: also idk if it's the same for everyone but it happened on two different places, in my second colony and in a planet in a distant system where I was excavating some alien installations, only thing I can think of that they have in common is that they were the latest ground units I had trained, about 2-5 years before the bug happened

Quote from: Steve Walmsley link=topic=11094. msg128635#msg128635 date=1588017079
Quote from: Bughunter link=topic=11094. msg128631#msg128631 date=1588015930
I don't know anything about your data structures, but sounds like most logical explanation would be the save loop sometimes don't go over all objects rather than a problem with the inserts.  Maybe a problem going through a hierarchical structure like command chain? An early return somewhere? A loop variable not going all the way like i++ not reaching last item in list.  Objects moved to some separate/temporary list not included in the save operation.

I'm not using loops that way for saving.  I use LINQ to retrieve a collection of objects then cycle through using foreach.  If that was a problem, it would always be a problem.

BTW do ALL ground units vanish or only some? If only some, are they different in some ways to the survivors?

In my case all the units in both locations vanished
Title: Re: Disappearing Ground Units
Post by: Steve Walmsley on April 27, 2020, 03:12:06 PM
Apart from system bodies, the game deletes the existing table and inserts the data from the program.

Does the delete for each table happen immediately prior to the insert, or do you do all the deletes at once and then all of the inserts?

I know you are opening and closing a connection for each insert. Are you reusing the same connection, or creating a new connection each time?

What would happen if you tried to insert a duplicate primary key? Would that possibly result in a silent failure that either ignores the duplicate row or overwrites the original with the duplicate?

Each table is a completely separate function. Open connection, delete table, open transaction, create all inserts, commit transaction, close connection. Move to next function.

Here is some example code

        public void SaveSystems()
        {
            try
            {
                // establish a database connection
                SQLiteConnection AuroraDB = new SQLiteConnection(GlobalValues.AuroraConnectionString);
                AuroraDB.Open();

                // delete the existing records
                SQLiteCommand del = new SQLiteCommand("DELETE FROM FCT_System WHERE GameID = " + GameID, AuroraDB);
                del.ExecuteNonQuery();

                using (var cmd = new SQLiteCommand(AuroraDB))
                {
                    using (SQLiteTransaction transaction = AuroraDB.BeginTransaction())
                    {
                        // insert new records
                        foreach (StarSystem obj in SystemList.Values)
                        {
                            cmd.CommandText =
                                @"INSERT INTO FCT_System (SystemID, SystemNumber, Age, AbundanceModifier, Stars, GameID, JumpPointSurveyPoints, SystemTypeID, DustDensity, SolSystem, NoSensorChecks )
                                VALUES ( @SystemID, @SystemNumber, @Age, @AbundanceModifier, @Stars, @GameID, @JumpPointSurveyPoints, @SystemTypeID, @DustDensity, @SolSystem, @NoSensorChecks)";

                            cmd.Parameters.AddWithValue("@SystemID", obj.SystemID);
                            cmd.Parameters.AddWithValue("@SystemNumber", obj.SystemNumber);
                            cmd.Parameters.AddWithValue("@Age", obj.Age);
                            cmd.Parameters.AddWithValue("@AbundanceModifier", obj.AbundanceModifier);
                            cmd.Parameters.AddWithValue("@Stars", obj.Stars);
                            cmd.Parameters.AddWithValue("@GameID", GameID);
                            cmd.Parameters.AddWithValue("@JumpPointSurveyPoints", obj.JumpPointSurveyPoints);
                            cmd.Parameters.AddWithValue("@SystemTypeID", obj.SystemTypeID);
                            cmd.Parameters.AddWithValue("@DustDensity", obj.DustDensity);
                            cmd.Parameters.AddWithValue("@SolSystem", obj.SolSystem);
                            cmd.Parameters.AddWithValue("@NoSensorChecks", obj.NoSensorChecks);
                            cmd.ExecuteNonQuery();
                        }

                        transaction.Commit();
                    }
                }

                AuroraDB.Close();
            }
            catch (Exception error) { GlobalValues.ErrorHandler(error, 1430); return; }
        }
Title: Re: Disappearing Ground Units
Post by: Bughunter on April 27, 2020, 03:14:26 PM
What Kristover describes points towards an endless loop somewhere in the save function, that is technically possible to achieve with LINQ expressions, example: https://stackoverflow.com/questions/31993443/why-does-this-method-result-in-an-infinite-loop (https://stackoverflow.com/questions/31993443/why-does-this-method-result-in-an-infinite-loop)
Title: Re: Disappearing Ground Units
Post by: Steve Walmsley on April 27, 2020, 03:17:05 PM
What Kristover describes points towards an endless loop somewhere in the save function, that is technically possible to achieve with LINQ expressions, example: https://stackoverflow.com/questions/31993443/why-does-this-method-result-in-an-infinite-loop (https://stackoverflow.com/questions/31993443/why-does-this-method-result-in-an-infinite-loop)

I've never had an infinite loop error in a foreach - its is usually while loops that are the issue. The above just looks like a recursive function.
Title: Re: Disappearing Ground Units
Post by: Bughunter on April 27, 2020, 03:20:15 PM
No, with the above code I cannot see how that could happen. Just assumed you might be using some more complex LINQ selection somewhere to pull data into your save function.
Title: Re: Disappearing Ground Units
Post by: JuJo on April 27, 2020, 03:21:25 PM
It happened to me about 12 years into a TN start.  All my ground units were affected.  I normally save every time Im traveling through an unexplored JP.  I think at the time I saved and closed the game maybe an hour(at most) passed before the previous save.  I didn’t had any STO units, just infantry, armor and GEO units.  Some units were in my 2 other colonies.  They all disappeared.
Title: Re: Disappearing Ground Units
Post by: Steve Walmsley on April 27, 2020, 03:23:12 PM
No, with the above code I cannot see how that could happen. Just assumed you might be using some more complex LINQ selection somewhere to pull data into your save function.

The collection there already exists so I just iterate. Occasionally I will use some simple LINQ prior to the loop to get collections from within a collection, such as:

List<MoveOrder> Orders = FleetsList.Values.SelectMany(x => x.MoveOrderList.Values).ToList();

and then foreach that to save.

Most major things like fleets, ships, systems, ground formations, are held in collections at game level. I use LINQ in functions to pull out the ones I need for a particular purpose, but the saves are almost all collections that exists for the whole game.
Title: Re: Disappearing Ground Units
Post by: skoormit on April 27, 2020, 03:24:38 PM
Here is some example code
...

First thing that stands out is that the connection itself is not wrapped in a using statement.
So, you are closing the connection, but not disposing it.
That can lead to funky problems that don't show up in basic unit testing.

The other thing: if an insert throws an exception, the catch block is after the connection close statement.
Which means that the connection will be left open.
But, if the exception were being caught, we'd be seeing an error message, right?
Well, how sure are you about that? If you just put a "throw new Exception();" line before the insert, do you in fact get the expected error messge?
Title: Re: Disappearing Ground Units
Post by: Steve Walmsley on April 27, 2020, 03:27:01 PM
Here is some example code
...

First thing that stands out is that the connection itself is not wrapped in a using statement.
So, you are closing the connection, but not disposing it.
That can lead to funky problems that don't show up in basic unit testing.

The other thing: if an insert throws an exception, the catch block is after the connection close statement.
Which means that the connection will be left open.
But, if the exception were being caught, we'd be seeing an error message, right?
Well, how sure are you about that? If you just put a "throw new Exception();" line before the insert, do you in fact get the expected error messge?

I've never seen a save problem in my own games without an error message.

I have started to change some save functions to have a second try/catch for each record rather than the whole insert, so maybe I need to complete that for every save function.

So you suggest changing to:
using (SQLiteConnection AuroraDB = new SQLiteConnection(GlobalValues.AuroraConnectionString))
{
}
Title: Re: Disappearing Ground Units
Post by: Steve Walmsley on April 27, 2020, 03:30:53 PM
How about updating the code to...

        public void SaveSystems()
        {
            try
            {
                // establish a database connection
                using (SQLiteConnection AuroraDB = new SQLiteConnection(GlobalValues.AuroraConnectionString))
                {
                    AuroraDB.Open();

                    // delete the existing records
                    SQLiteCommand del = new SQLiteCommand("DELETE FROM FCT_System WHERE GameID = " + GameID, AuroraDB);
                    del.ExecuteNonQuery();

                    using (var cmd = new SQLiteCommand(AuroraDB))
                    {
                        using (SQLiteTransaction transaction = AuroraDB.BeginTransaction())
                        {
                            // insert new records
                            foreach (StarSystem obj in SystemList.Values)
                            {
                                try
                                {
                                    cmd.CommandText =
                                        @"INSERT INTO FCT_System (SystemID, SystemNumber, Age, AbundanceModifier, Stars, GameID, JumpPointSurveyPoints, SystemTypeID, DustDensity, SolSystem, NoSensorChecks )
                                VALUES ( @SystemID, @SystemNumber, @Age, @AbundanceModifier, @Stars, @GameID, @JumpPointSurveyPoints, @SystemTypeID, @DustDensity, @SolSystem, @NoSensorChecks)";

                                    cmd.Parameters.AddWithValue("@SystemID", obj.SystemID);
                                    cmd.Parameters.AddWithValue("@SystemNumber", obj.SystemNumber);
                                    cmd.Parameters.AddWithValue("@Age", obj.Age);
                                    cmd.Parameters.AddWithValue("@AbundanceModifier", obj.AbundanceModifier);
                                    cmd.Parameters.AddWithValue("@Stars", obj.Stars);
                                    cmd.Parameters.AddWithValue("@GameID", GameID);
                                    cmd.Parameters.AddWithValue("@JumpPointSurveyPoints", obj.JumpPointSurveyPoints);
                                    cmd.Parameters.AddWithValue("@SystemTypeID", obj.SystemTypeID);
                                    cmd.Parameters.AddWithValue("@DustDensity", obj.DustDensity);
                                    cmd.Parameters.AddWithValue("@SolSystem", obj.SolSystem);
                                    cmd.Parameters.AddWithValue("@NoSensorChecks", obj.NoSensorChecks);
                                    cmd.ExecuteNonQuery();
                                }
                                catch (Exception error) { GlobalValues.ErrorHandler(error, 3249); return; }
                            }

                            transaction.Commit();
                        }
                    }

                    AuroraDB.Close();
                }
            }
            catch (Exception error) { GlobalValues.ErrorHandler(error, 1430); return; }
        }
Title: Re: Disappearing Ground Units
Post by: skoormit on April 27, 2020, 03:32:28 PM


I've never seen a save problem in my own games without an error message.

I have started to change some save functions to have a second try/catch for each record rather than the whole insert, so maybe I need to complete that for every save function.

I think that's a good idea, but maybe not needed to fix this particular bug. If that would be time consuming, maybe just force the savegroundunits function to throw an exception and see if you actually get the expected error message.
Title: Re: Disappearing Ground Units
Post by: Erik L on April 27, 2020, 03:33:59 PM
How about updating the code to...

        public void SaveSystems()
        {
            try
            {
                // establish a database connection
                using (SQLiteConnection AuroraDB = new SQLiteConnection(GlobalValues.AuroraConnectionString))
                {
                    AuroraDB.Open();

                    // delete the existing records
                    SQLiteCommand del = new SQLiteCommand("DELETE FROM FCT_System WHERE GameID = " + GameID, AuroraDB);
                    del.ExecuteNonQuery();

                    using (var cmd = new SQLiteCommand(AuroraDB))
                    {
                        using (SQLiteTransaction transaction = AuroraDB.BeginTransaction())
                        {
                            // insert new records
                            foreach (StarSystem obj in SystemList.Values)
                            {
                                try
                                {
                                    cmd.CommandText =
                                        @"INSERT INTO FCT_System (SystemID, SystemNumber, Age, AbundanceModifier, Stars, GameID, JumpPointSurveyPoints, SystemTypeID, DustDensity, SolSystem, NoSensorChecks )
                                VALUES ( @SystemID, @SystemNumber, @Age, @AbundanceModifier, @Stars, @GameID, @JumpPointSurveyPoints, @SystemTypeID, @DustDensity, @SolSystem, @NoSensorChecks)";

                                    cmd.Parameters.AddWithValue("@SystemID", obj.SystemID);
                                    cmd.Parameters.AddWithValue("@SystemNumber", obj.SystemNumber);
                                    cmd.Parameters.AddWithValue("@Age", obj.Age);
                                    cmd.Parameters.AddWithValue("@AbundanceModifier", obj.AbundanceModifier);
                                    cmd.Parameters.AddWithValue("@Stars", obj.Stars);
                                    cmd.Parameters.AddWithValue("@GameID", GameID);
                                    cmd.Parameters.AddWithValue("@JumpPointSurveyPoints", obj.JumpPointSurveyPoints);
                                    cmd.Parameters.AddWithValue("@SystemTypeID", obj.SystemTypeID);
                                    cmd.Parameters.AddWithValue("@DustDensity", obj.DustDensity);
                                    cmd.Parameters.AddWithValue("@SolSystem", obj.SolSystem);
                                    cmd.Parameters.AddWithValue("@NoSensorChecks", obj.NoSensorChecks);
                                    cmd.ExecuteNonQuery();
                                }
                                catch (Exception error) { GlobalValues.ErrorHandler(error, 3249); return; }
                            }

                            transaction.Commit();
                        }
                    }

                    AuroraDB.Close();
                }
            }
            catch (Exception error) { GlobalValues.ErrorHandler(error, 1430); return; }
        }
I'd move the AuroraDB.Close(); to the finally section of the try/catch
Title: Re: Disappearing Ground Units
Post by: skoormit on April 27, 2020, 03:34:39 PM
How about updating the code
...

That looks exactly right.
Title: Re: Disappearing Ground Units
Post by: skoormit on April 27, 2020, 03:38:03 PM

I'd move the AuroraDB.Close(); to the finally section of the try/catch

That's good form in general, but the end of the using statement will force a Dispose(), which will also close the connection.
Title: Re: Disappearing Ground Units
Post by: Steve Walmsley on April 27, 2020, 03:38:07 PM
How about updating the code
...

That looks exactly right.

Thanks - some code grinding ahead :)

I'll start with the ones I know had issues (ships, ground units, etc.) and add the rest when the enthusiasm reservoir is high :)
Title: Re: Disappearing Ground Units
Post by: SpikeTheHobbitMage on April 27, 2020, 03:40:26 PM
One thing I'd like to point out (and I'm sorry if I'm getting annoying) is that the delete operation is outside of the transaction.  This means that if the insert is interrupted and gets rolled back, the delete still gets committed.

I'd move the AuroraDB.Close(); to the finally section of the try/catch
Shouldn't the 'using' clause take care of that?  Edit: ninjas everywhere.
Title: Re: Disappearing Ground Units
Post by: Erik L on April 27, 2020, 03:41:56 PM
One thing I'd like to point out (and I'm sorry if I'm getting annoying) is that the delete operation is outside of the transaction.  This means that if the insert is interrupted and gets rolled back, the delete still gets committed.

I'd move the AuroraDB.Close(); to the finally section of the try/catch
Shouldn't the 'using' clause take care of that?  Edit: ninjas everywhere.

Maybe, but I don't use using in C# :)
Title: Re: Disappearing Ground Units
Post by: Steve Walmsley on April 27, 2020, 03:44:51 PM
One thing I'd like to point out (and I'm sorry if I'm getting annoying) is that the delete operation is outside of the transaction.  This means that if the insert is interrupted and gets rolled back, the delete still gets committed.

I'd move the AuroraDB.Close(); to the finally section of the try/catch
Shouldn't the 'using' clause take care of that?

Good advice is never annoying :)

Now I am torn. If I prevent the delete then I could end up with a table that is out of sync with all the others, which might cause harder to find bugs. With the try/catch on the record level, would I not be better deleting and then only losing any individual records that fail?
Title: Re: Disappearing Ground Units
Post by: Bughunter on April 27, 2020, 03:46:08 PM

I'd move the AuroraDB.Close(); to the finally section of the try/catch

Yes agree with making sure it closes. Just be aware if it failed to open earlier it may throw an error trying to close it there. I don't know that happens with sqlite but have seen with other db:s. And not that it matters in this example since you would have an earlier error anyway.
Title: Re: Disappearing Ground Units
Post by: db48x on April 27, 2020, 03:47:56 PM
if you took out the return from the inner catch block, then it would have the advantage that if it fails to insert any single record, then only that one record is lost. If you logged the information about the failing record(s), you could track down the bug more easily.
Title: Re: Disappearing Ground Units
Post by: Earthrise on April 27, 2020, 03:50:26 PM
Steve, I've just had this issue happen to me, in v8. 0 .  The two police battalions I built more recently have disappeared, but the first two I built are still there (though their ORBAT has gone, which is also odd).    I am only about 15 years into this game.    My PC is set up with a British decimal separator.   

Please find a dropbox link with the DBs from just before it happened and just after it happened.   

As other people have reported, I too was AFK for a couple of hours inbetween the two saves, though the game was not advancing time.   

Hope this helps.   
https://www. dropbox. com/sh/de44pyqzo3hprni/AAAB0pL-1fvDFHMjKJ1xZzita?dl=0

(hyperlink is not working, sorry)
Title: Re: Disappearing Ground Units
Post by: skoormit on April 27, 2020, 03:58:49 PM
If I prevent the delete then I could end up with a table that is out of sync with all the others, which might cause harder to find bugs. With the try/catch on the record level, would I not be better deleting and then only losing any individual records that fail?

Which way is better depends entirely on your design decisions about error resilience.
The stance that "errors will happen and we should minimize their impact on a particular game instance" is reasonable, but the effort (to localize errors and minimize impacts) requires dev time.
Title: Re: Disappearing Ground Units
Post by: skoormit on April 27, 2020, 04:00:31 PM
...
As other people have reported, I too was AFK for a couple of hours inbetween the two saves, though the game was not advancing time.   
...

I'm curious: did any game-time pass between the two saves?
Title: Re: Disappearing Ground Units
Post by: Second Foundationer on April 27, 2020, 04:04:37 PM
Maybe you've already narrowed it down by now, I haven't read all the recent posts.   But my unqualified shot in the dark: Is it absolutely certain that it is the saving rather than the loading process that causes the error?

I began a new game with 4 distant starting NPRs briefly after 1.  8 release and kept manual db copies periodically.   A slow setup, two encounters with Trappist aliens, and, I don't remember exactly, about 35, in any case less than 40 (now very quick, thanks to C# Aurora) game years later, I had an unrelated program in a buggy script debug mode crash while saving aurora, and I thought that might have caused a save corruption: Next time I started Aurora, all ground units were gone (in total maybe around 40, most of them on Earth, a handful on fledgling colonies, and possibly 1 or 2 in transit on commercial transports, plus several garrisons on CMC rocks).   As for saving, Aurora had been running idle in the background for longer periods at times, so the save intervals were irregular.   And it is possible that I saved twice without advancing time. 

I have no experience with SQLite or C#, but I took a brief look into the db with an SQLite DB Browser, and it seemed that most if not all of the units were still basically listed in the formation table with their names, template numbers, etc.   Yet Aurora couldn't load the ground units, everything else seemed to be fine.   But as I had somewhat botched my desired setup (a gloomy pacifist Antarctic Union of WWIII survivors that came out way too militarized and also too populous), didn't suspect yet that this might be a general bug and feared there might be more serious, less obvious save corruption elsewhere, I decided to start over when the next version is out .  .  .   and wiped the manual saves, so I cannot pass on the db in question. 

But if it is just possible from other bug reports that the cause might be in the input rather than the output, it might be worth to glance over the startup/load/initialization function/s or whatever it is that gets the data into the running program.   In case it's relevant: I had deleted the "Federated Nations" (? or something like that) example/default game after my own Antarctica setup. 

Although I've been drawn into a descending orbit around Aurora for years and browse the forums periodically, I just registered now.   So, with my first post: Many thanks for sharing a marvelous game with us and your continued efforts to make us spend many more hours with it.   And to the generally helpful community here for answering all of my Aurora 4x questions so far, before I had ever asked them. 
Title: Re: Disappearing Ground Units
Post by: Earthrise on April 27, 2020, 04:08:27 PM
Quote from: skoormit link=topic=11094. msg128683#msg128683 date=1588021231
Quote from: Earthrise link=topic=11094. msg128677#msg128677 date=1588020626
. . .
As other people have reported, I too was AFK for a couple of hours inbetween the two saves, though the game was not advancing time.   
. . .

I'm curious: did any game-time pass between the two saves?

Yes some time had passed inbetween the two saves - I don't remember exactly how long.  Feel free to download my saves for further details  :)
Title: Re: Disappearing Ground Units
Post by: Bughunter on April 27, 2020, 04:09:19 PM
Second Foundationer - As I understood from previous reports the missing objects were gone from the db. If it is as you say you may have found a different bug and should post the db and exactly which units doesn't load.
Title: Re: Disappearing Ground Units
Post by: skoormit on April 27, 2020, 05:00:33 PM
Well, add this to the pile of clues: my fuel keeps disappearing from Luna.
I moved all my shipyards to Luna, as well as a refuelling station.
Then I used a tanker to transport several million units of fuel to Luna.

Sometime later, I built a ship and was told it had no fuel.
I looked at the Luna colony summary. Zero fuel available.

This has happened several times now. I can't figure out what precedes it, though I do often go a long time between saves, with long stretches of idle time.

I'm in year 12.
I haven't noticed anything else missing.
Title: Re: Disappearing Ground Units
Post by: Froggiest1982 on April 27, 2020, 05:03:14 PM
Well, add this to the pile of clues: my fuel keeps disappearing from Luna.
I moved all my shipyards to Luna, as well as a refuelling station.
Then I used a tanker to transport several million units of fuel to Luna.

Sometime later, I built a ship and was told it had no fuel.
I looked at the Luna colony summary. Zero fuel available.

This has happened several times now. I can't figure out what precedes it, though I do often go a long time between saves, with long stretches of idle time.

I'm in year 12.
I haven't noticed anything else missing.

I saw a fuel bug on the bug posts related to missing MSP to replenish ships that causes the lost of fuel. Might be that? Steve fixed it already for 1.9 release
Title: Re: Disappearing Ground Units
Post by: Steve Walmsley on April 27, 2020, 05:16:59 PM
if you took out the return from the inner catch block, then it would have the advantage that if it fails to insert any single record, then only that one record is lost. If you logged the information about the failing record(s), you could track down the bug more easily.

Yes, good point. That was my intention - copy paste problem :)
Title: Re: Disappearing Ground Units
Post by: Steve Walmsley on April 27, 2020, 05:19:01 PM
Well, add this to the pile of clues: my fuel keeps disappearing from Luna.
I moved all my shipyards to Luna, as well as a refuelling station.
Then I used a tanker to transport several million units of fuel to Luna.

Sometime later, I built a ship and was told it had no fuel.
I looked at the Luna colony summary. Zero fuel available.

This has happened several times now. I can't figure out what precedes it, though I do often go a long time between saves, with long stretches of idle time.

I'm in year 12.
I haven't noticed anything else missing.

I saw a fuel bug on the bug posts related to missing MSP to replenish ships that causes the lost of fuel. Might be that? Steve fixed it already for 1.9 release

Yes, missing fuel bug happens when you are out of MSP and try to replenish - fixed now.
Title: Re: Disappearing Ground Units
Post by: SpikeTheHobbitMage on April 27, 2020, 05:31:46 PM
One thing I'd like to point out (and I'm sorry if I'm getting annoying) is that the delete operation is outside of the transaction.  This means that if the insert is interrupted and gets rolled back, the delete still gets committed.

I'd move the AuroraDB.Close(); to the finally section of the try/catch
Shouldn't the 'using' clause take care of that?
Good advice is never annoying :)

Now I am torn. If I prevent the delete then I could end up with a table that is out of sync with all the others, which might cause harder to find bugs. With the try/catch on the record level, would I not be better deleting and then only losing any individual records that fail?
Then I shall endeavor to give good advice.  :)

You will need to fully suppress any row-level exceptions unless you intend to abort the whole save, because the using clause on the transaction will rollback if it sees one.  Returning from inside the loop has the same effect.  Maybe log those errors to a text file instead?  Remember to open in append mode. Edit: Actually, just remove the return after showing the popup so the save can continue.

Another issue is that if a non-recoverable error does occur or Aurora crashes during the save, only the current table gets rolled back.  That is guaranteed to leave the database in an inconsistent state with the current setup.  The solution to that problem is to start the transaction in the function that calls the save functions.  Just pass the connection in as an argument so they can all share it.  This has the added bonus of speeding up saves by a factor of about 7x.
Title: Re: Disappearing Ground Units
Post by: Graymane on April 27, 2020, 05:41:14 PM
Quote from: Steve Walmsley link=topic=11094. msg128542#msg128542 date=1588001230
Quote from: Omnivore link=topic=11094. msg128536#msg128536 date=1587999296
I haven't had this happen to me but I just saw something occur when I saved a game that had been running all night and thought it might be pertinent.

After I clicked save there was a point in time where I could close the game window yet the save hadn't completed yet.   This in and of itself may not be a problem with the way SQLite works in C#, but I have to ask, are you using multiple transactions?  If so, is the ground unit data being saved last? 

If the answer to both of those questions is true, then the following might be relevant:

"In SQLite, only one transaction is allowed to have changes pending in the database at a time.  Because of this, calls to BeginTransaction and the Execute methods on SqliteCommand may time out if another transaction takes too long to complete.  (Microsoft C# docs)"

Hope this helps,

PS: If this reply is unwelcome, please accept my apologies.

I am using one transaction for each of seventy-eight different table inserts.  The ground formations aren't last but they are in the last ten so you might be on to something. 

I open a connection and then close for each table as well.  Am I better using a single open/close and a single transaction for all inserts?
Hi Steve, I am most definitely not a C# or Sqlite or linq expert, but what is the intention of opening and closing a connection every time?  In other languages/DBs it is common to use connection pooling.   Second, is your intention that each of those 78 inserts are independent from each other?  If one fails should they all fail or not?  I would group those 78 into groups that must all succeed or fail together.   Otherwise you can leave a DB in an inconsistent state.   In a regular DB setup, that would be much faster as well.   I assume you are doing an in-memory DB or at least only your aurora process is updating it.   If that is the case, using connection pooling and optimizing your transaction might not have much performance impact.   I'd profile it.
Title: Re: Disappearing Ground Units
Post by: Steve Walmsley on April 27, 2020, 05:48:19 PM
You will need to fully suppress any row-level exceptions unless you intend to abort the whole save, because the using clause on the transaction will rollback if it sees one.  Returning from inside the loop has the same effect.  Maybe log those errors to a text file instead?  Remember to open in append mode.

Another issue is that if a non-recoverable error does occur or Aurora crashes during the save, only the current transaction gets rolled back.  That is guaranteed to leave the database in an inconsistent state with the current setup.  The solution to that problem is to start the transaction in the function that calls the save functions.  Just pass the connection in as an argument so they can all share it.  This has the added bonus of speeding up saves by a factor of about 7x.

OK, I need to clarify my understanding here :)

Let's assume that as suggested the transaction is setup in the function that calls all the separate save function. BeginTransaction needs the DB connection, so that would also need to be in the 'master save' function. The various SQLiteCommands for the deletes and the insert also need the DB connection but that could be passed to each function.

So it would look like

Using Connection
{
    Using Transaction
    {
         SaveFunction #1 (Connection)
        {
             Delete Table
             SQLiteCommand
             {
                   Insert Rows & log errors       
              }
         }

         SaveFunction #2 (Connection)
        {
             Delete Table
             SQLiteCommand
             {
                   Insert Rows & log errors   
              }
         }
    }
}

My understanding from what you described is that in this scenario, either the whole save is successful or the save doesn't happen at all. Any error for any record in any table prevent the save taking place, although I would retain the error log of the failed save. Is that correct?
Title: Re: Disappearing Ground Units
Post by: Steve Walmsley on April 27, 2020, 05:53:09 PM
Hi Steve, I am most definitely not a C# or Sqlite or linq expert, but what is the intention of opening and closing a connection every time?  In other languages/DBs it is common to use connection pooling.   Second, is your intention that each of those 78 inserts are independent from each other?  If one fails should they all fail or not?  I would group those 78 into groups that must all succeed or fail together.   Otherwise you can leave a DB in an inconsistent state.   In a regular DB setup, that would be much faster as well.   I assume you are doing an in-memory DB or at least only your aurora process is updating it.   If that is the case, using connection pooling and optimizing your transaction might not have much performance impact.   I'd profile it.

That's OK, this is my first time using SQLite :)  As usual, I write these programs to learn new stuff.

The different functions were added over time rather than being part of an original, thought out architecture. So yes they were intended to be separate, but that was based on the knowledge I had at the time. This discussion might change how I handle it.

I don't have an in-memory DB as such. I load the data into collection hierarchies because there is a lot more in the program than just the database fields. Then I covert back to the relational structure for saving.

Title: Re: Disappearing Ground Units
Post by: Graymane on April 27, 2020, 06:01:46 PM
Let me give a simple example, if you have a function that saves fleets and another that save ships and there are links between the 2, i. e. , the ships have a fleet id and the fleet have ship ids and you have separate transactions, then if saved the fleet successfully but the ships save fails, it can cause problems.   In that case, you could end up with your fleets pointing to ships that don't exist or vice versa.   If they were all in the same transaction, you avoid that problem and they are all successful or fail as a group.
Title: Re: Disappearing Ground Units
Post by: Steve Walmsley on April 27, 2020, 06:08:32 PM
Let me give a simple example, if you have a function that saves fleets and another that save ships and there are links between the 2, i. e. , the ships have a fleet id and the fleet have ship ids and you have separate transactions, then if saved the fleet successfully but the ships save fails, it can cause problems.   In that case, you could end up with your fleets pointing to ships that don't exist or vice versa.   If they were all in the same transaction, you avoid that problem and they are all successful or fail as a group.

Yes, I am aware of that issue. The load handles it by checking for links before loading anything, so the program doesn't end up with conflicting information once loaded. A ship won't load unless the parent fleet is already loaded for example, or a fleet won't load unless the parent race and current system are already loaded. The load order is set up to check all those dependencies. However, that doesn't solve the problem of the reason something failed to save in the first place.

I am a little nervous of getting into a situation when a player can't save anything at all because of a problem, even a minor issue that won't affect anything significant. He can't play the game and there is no db to check post-error. It may be better to just fail the individual problem records and highlight those problems.

Title: Re: Disappearing Ground Units
Post by: SpikeTheHobbitMage on April 27, 2020, 06:16:27 PM
You will need to fully suppress any row-level exceptions unless you intend to abort the whole save, because the using clause on the transaction will rollback if it sees one.  Returning from inside the loop has the same effect.  Maybe log those errors to a text file instead?  Remember to open in append mode.

Another issue is that if a non-recoverable error does occur or Aurora crashes during the save, only the current transaction gets rolled back.  That is guaranteed to leave the database in an inconsistent state with the current setup.  The solution to that problem is to start the transaction in the function that calls the save functions.  Just pass the connection in as an argument so they can all share it.  This has the added bonus of speeding up saves by a factor of about 7x.

OK, I need to clarify my understanding here :)

Let's assume that as suggested the transaction is setup in the function that calls all the separate save function. BeginTransaction needs the DB connection, so that would also need to be in the 'master save' function. The various SQLiteCommands for the deletes and the insert also need the DB connection but that could be passed to each function.

So it would look like

Using Connection
{
    Using Transaction
    {
         SaveFunction #1 (Connection)
        {
             Delete Table
             SQLiteCommand
             {
                   Insert Rows & log errors       
              }
         }

         SaveFunction #2 (Connection)
        {
             Delete Table
             SQLiteCommand
             {
                   Insert Rows & log errors   
              }
         }
    }
}

My understanding from what you described is that in this scenario, either the whole save is successful or the save doesn't happen at all. Any error for any record in any table prevent the save taking place, although I would retain the error log of the failed save. Is that correct?
Exactly correct.  A transaction should always leave the database in a consistent state.  The other more complicated option is to try to recover failed rows, but again with the same goal.  Edit: One thing to check here is that C# isn't issuing a rollback if an insert fails.  If it does, the solution is to wrap each insert in a savepoint, but I'm not quite certain how to do that in C#.  I'll need to get back to you if that is case.

One other thing to check:  Does that popup in the middle of the insert loop actually work, or does it hang?  If it hangs, that could explain the problem people reported of saving, leaving, and coming back to find Aurora frozen with a broken database.
Title: Re: Disappearing Ground Units
Post by: Steve Walmsley on April 27, 2020, 06:24:31 PM
One other thing to check:  Does that popup in the middle of the insert loop actually work, or does it hang?  If it hangs, that could explain the problem people reported of saving, leaving, and coming back to find Aurora frozen with a broken database.

Based on the current code, not the proposed code, if there is an error in an object such as a null value, an error pops up and the transaction is aborted. Because the transaction is at the table level, the data in the target table is deleted and no records are inserted. The program carries on to the next table.

I've always had that happen for every insert error over several years of dev work.
Title: Re: Disappearing Ground Units
Post by: SpikeTheHobbitMage on April 27, 2020, 06:39:16 PM
One other thing to check:  Does that popup in the middle of the insert loop actually work, or does it hang?  If it hangs, that could explain the problem people reported of saving, leaving, and coming back to find Aurora frozen with a broken database.

Based on the current code, not the proposed code, if there is an error in an object such as a null value, an error pops up and the transaction is aborted. Because the transaction is at the table level, the data in the target table is deleted and no records are inserted. The program carries on to the next table.

I've always had that happen for every insert error over several years of dev work.
Fair enough, though I'm still concerned about whatever is causing that hang.  Would Aurora be able to recover if the bad rows were stripped out without wiping the whole table?

Edit:  If you do go with dropping rows, maybe give the user the option of aborting the save and quitting so they can resume from their last good savepoint.
Title: Re: Disappearing Ground Units
Post by: Omnivore on April 27, 2020, 06:48:07 PM
The one thing that seems clear about this issue is that you need more information to track down the root cause.  Would you consider adding error logging to the next release so that you would have a concise and accurate report on what occurred around the time of the error? 

Part of the problem in a bug of this nature is human perception and rather coarse capabilities, a key clue might easily go unnoticed or be forgotten, the granularity of the snapshots you are able to look at (before and after databases) is evidently insufficient in this case and could possibly be likewise lacking in tracking later issues.

I'm retired so not very active but I spent some time talking to a few friends who are still on the job and there was a unanimous consensus on the need for logging in cases like these.

Hope this helps
Title: Re: Disappearing Ground Units
Post by: SpikeTheHobbitMage on April 27, 2020, 07:00:58 PM
The one thing that seems clear about this issue is that you need more information to track down the root cause.  Would you consider adding error logging to the next release so that you would have a concise and accurate report on what occurred around the time of the error? 

Part of the problem in a bug of this nature is human perception and rather coarse capabilities, a key clue might easily go unnoticed or be forgotten, the granularity of the snapshots you are able to look at (before and after databases) is evidently insufficient in this case and could possibly be likewise lacking in tracking later issues.

I'm retired so not very active but I spent some time talking to a few friends who are still on the job and there was a unanimous consensus on the need for logging in cases like these.

Hope this helps
So much this.  If nothing else, logging the bad rows would at least tell you why they were rejected.
Title: Re: Disappearing Ground Units
Post by: Erik L on April 27, 2020, 07:10:06 PM
This is what I use to log everything in my programs. Yes I know there are logger extensions/addins/whatevers, but this works for me.

Code: [Select]
       public static void WriteLog(string message, LogFile logType, LogLevel level, string moduleName = "", string gameName = "", string raceName = "")
        {
            string logFile;
            string header;

            DateTimeFormatInfo dtfi = new CultureInfo(currCultureInfo.ToString(), false).DateTimeFormat;
            dtfi.LongTimePattern = "HH:mm:ss";

            header = "[" + DateTime.Today.ToLongDateString() + " " + DateTime.Now.ToLongTimeString() + "] - ";

            switch (level)
            {
                case LogLevel.errorLevel:
                    header = header + "[ERROR]: " + moduleName;
                    break;
                case LogLevel.debugLevel:
                    header = header + "[DEBUG]: " + moduleName;
                    break;
                default:
                    header = header + "[INFO]: " + moduleName;
                    break;
            }
            message = header + message + "\r\n";

            switch (logType)
            {
                case LogFile.errorLog:
                    logFile = LogPath + @"\Error.log";
                    break;
                case LogFile.debugLog:
                    logFile = LogPath + @"\Debug" + DateTime.Today.ToString("yyyymmdd") + ".log";
                    break;
                case LogFile.gameLog:
                    logFile = LogPath + @"Game-" + gameName + ".log";
                    break;
                case LogFile.raceLog:
                    logFile = LogPath + @"Race-" + raceName + ".log";
                    break;
                default:
                    logFile = LogPath + @"\General.log";
                    break;

            }
            if (!File.Exists(logFile))
            {
                using (StreamWriter file = new StreamWriter(logFile))
                {
                    file.Write(message);
                }
            }
            else
            {
                using (StreamWriter file = new StreamWriter(logFile, true))
                {
                    file.Write(message);
                }
            }

        }
Title: Re: Disappearing Ground Units
Post by: RedDagger on April 27, 2020, 07:25:53 PM
Quote from: SpikeTheHobbitMage link=topic=11094. msg128701#msg128701 date=1588026706

Then I shall endeavor to give good advice.   :)

You will need to fully suppress any row-level exceptions unless you intend to abort the whole save, because the using clause on the transaction will rollback if it sees one.   Returning from inside the loop has the same effect.   Maybe log those errors to a text file instead?  Remember to open in append mode. Edit: Actually, just remove the return after showing the popup so the save can continue.

Another issue is that if a non-recoverable error does occur or Aurora crashes during the save, only the current table gets rolled back.   That is guaranteed to leave the database in an inconsistent state with the current setup.   The solution to that problem is to start the transaction in the function that calls the save functions.   Just pass the connection in as an argument so they can all share it.   This has the added bonus of speeding up saves by a factor of about 7x.

Instead of passing the connection already opened as a parameter in each function, Steve can put it in a Singleton class.  It might be less work refactoring.
Title: Re: Disappearing Ground Units
Post by: Omnivore on April 27, 2020, 07:39:18 PM
Quote from: SpikeTheHobbitMage link=topic=11094. msg128701#msg128701 date=1588026706

Then I shall endeavor to give good advice.   :)

You will need to fully suppress any row-level exceptions unless you intend to abort the whole save, because the using clause on the transaction will rollback if it sees one.   Returning from inside the loop has the same effect.   Maybe log those errors to a text file instead?  Remember to open in append mode. Edit: Actually, just remove the return after showing the popup so the save can continue.

Another issue is that if a non-recoverable error does occur or Aurora crashes during the save, only the current table gets rolled back.   That is guaranteed to leave the database in an inconsistent state with the current setup.   The solution to that problem is to start the transaction in the function that calls the save functions.   Just pass the connection in as an argument so they can all share it.   This has the added bonus of speeding up saves by a factor of about 7x.

Instead of passing the connection already opened as a parameter in each function, Steve can put it in a Singleton class.  It might be less work refactoring.

Once I got past my instinctive NO GLOBALS ARE EVIL reaction... ok I'm still not over it, but if it was deemed absolutely necessary you'd have to make it compatible with using statements and make sure it threw exceptions if accessed outside of such a scope, otherwise asking for trouble.

PS: Addressing all the concerns involved to make it robust would probably take more time and effort than passing the connection.
Title: Re: Disappearing Ground Units
Post by: Migi on April 27, 2020, 07:45:11 PM
As a non-programmer, I have only one probably unhelpful suggestion:
Did you sanitize your database inputs?
Is this caused by people mis-naming their drop pod troops as drop table troops?
(relevant xkcd (https://xkcd.com/327/))
Title: Re: Disappearing Ground Units
Post by: SpikeTheHobbitMage on April 27, 2020, 07:47:20 PM
Instead of passing the connection already opened as a parameter in each function, Steve can put it in a Singleton class.  It might be less work refactoring.
That seems to me to be overkill for something that could just as easily be tied to the game-state or some other long lived object if he wants to keep the connection open across saves.

Once I got past my instinctive NO GLOBALS ARE EVIL reaction... ok I'm still not over it, but if it was deemed absolutely necessary you'd have to make it compatible with using statements and make sure it threw exceptions if accessed outside of such a scope, otherwise asking for trouble.
Globals are evil and also unnecessary.  There is also no need to use a 'using' statement with a persistent connection as it will only be opened once and is crash-safe.  The transactions and prepared statements are the parts that need reliable cleanup.

As a non-programmer, I have only one probably unhelpful suggestion:
Did you sanitize your database inputs?
Is this caused by people mis-naming their drop pod troops as drop table troops?
(relevant xkcd (https://xkcd.com/327/))
Based on his sample code, Steve has learned the Sacred Art of the Prepared Statement.  Input sanitization is assured.
Title: Re: Disappearing Ground Units
Post by: se5a on April 27, 2020, 11:32:14 PM
Globals are *not* evil, and static and Singleton use are not even as bad as globals.
Globals should be used with great care, yes, but there are times when you can do far more damage to your codebase by avoiding a global than you would by using it. go watch some of Jon Blow's rants on this sort of thing.
Sorry for the off topic, but this is part of a disease that has been spreading through the programming community for a couple of decades, it's why we have to deal with such smegty slow software on a day to day basis.
Title: Re: Disappearing Ground Units
Post by: TMaekler on April 28, 2020, 01:29:16 AM
Don't want to create a side discussion; but seeing the potential problems with saving, I was wondering if using your own save format might solve the issues. Using a DB now derives from using it in the past because easier to handle when not all data is in RAM; but now it is... . So might be worth while to think about this.
Title: Re: Disappearing Ground Units
Post by: Kelewan on April 28, 2020, 02:03:56 AM
I have not triggered this bug jet, but i have followed this and the other bug reports.
Reading through the last paged i came to the conclusion that the function that saves
the ground forces is not the root of the problem.

Steve uses a transaction  around the inserts. So all or none of the ground forces  should be in the database.
Most reports say that some but not all ground forces are missing and there are no error messages shown.
This leads to my conclusions
 

So how do the ground forces go missing? What do we know?


Is there a place where the ground force data structure is copied and the data structure used to display diverges from the one that is used to save?
Title: Re: Disappearing Ground Units
Post by: se5a on April 28, 2020, 03:24:18 AM
Is there a place where the ground force data structure is copied and the data structure used to display diverges from the one that is used to save?
There's a bunch of places where the UI will not refresh unless the window is closed and reopened again, or the refresh button is clicked, this may explain the above.
Title: Re: Disappearing Ground Units
Post by: Steve Walmsley on April 28, 2020, 04:19:12 AM
I have not triggered this bug jet, but i have followed this and the other bug reports.
Reading through the last paged i came to the conclusion that the function that saves
the ground forces is not the root of the problem.

Steve uses a transaction  around the inserts. So all or none of the ground forces  should be in the database.
Most reports say that some but not all ground forces are missing and there are no error messages shown.
This leads to my conclusions
  • that there is no error inserting the ground forces in the database
  • that the missing ground forces where already missing in the data structure
 

So how do the ground forces go missing? What do we know?

  • in most cases it was a group of ground forces: e.g. all STOs, all recent created troops, all initial created troops
  • the action/actions didn't remove the troops from the display immediately, because nobody clearly remembers what they have done with the troops before
  • the action/actions are not that common or we would have more reports

Is there a place where the ground force data structure is copied and the data structure used to display diverges from the one that is used to save?

It does sound like some people are only losing some of their ground forces, so if that is the case you are correct that a save error would not cause that.

All ground formations are saved in a single collection within the Game object, which is loaded from the database. This acts in effect as a ground forces table. Then if I need to populate the OOB for a particular race for example, I will use LINQ to select the ground forces of a that race into a new list, but this is just a list of references to the master list. Only one object for each formation exists within the program. Objects are added to or removed from the master list when a formation is created or destroyed. That master list is then iterated for the save.

Based on this thread, I think I need to improve the save code anyway, but I agree that may not actually be the cause of the bug.
Title: Re: Disappearing Ground Units
Post by: Steve Walmsley on April 28, 2020, 04:20:23 AM
Don't want to create a side discussion; but seeing the potential problems with saving, I was wondering if using your own save format might solve the issues. Using a DB now derives from using it in the past because easier to handle when not all data is in RAM; but now it is... . So might be worth while to think about this.

The database is also really useful for viewing and modifying data outside the program.
Title: Re: Disappearing Ground Units
Post by: TMaekler on April 28, 2020, 04:24:08 AM
Don't want to create a side discussion; but seeing the potential problems with saving, I was wondering if using your own save format might solve the issues. Using a DB now derives from using it in the past because easier to handle when not all data is in RAM; but now it is... . So might be worth while to think about this.

The database is also really useful for viewing and modifying data outside the program.
I know. Did that myself as I had written you with the 1.7. to 1.8 DB - worked fine by the way. No errors since  :D
Title: Re: Disappearing Ground Units
Post by: Froggiest1982 on April 28, 2020, 05:00:34 AM
Wouldn't launch the game without privileged access (admin) cause some issues when writing/overwriting the database? I don't know if relevant...

it's so random and not everybody is affected so there must be something in common. I never had this issue yet (thank God) for instance so I really don't know, all I know is I got paranoid so I save every 30 minutes, close all windows prior saving and then run an interval and save again. I know I said that before, but the more I read here the more I get confused.
Title: Re: Disappearing Ground Units
Post by: Steve Walmsley on April 28, 2020, 05:08:57 AM
BTW can I just clarify if the units disappear after save, or after the game is reloaded?
Title: Re: Disappearing Ground Units
Post by: Argoniur on April 28, 2020, 05:29:22 AM
It's hard to know exactly when it happened, but I'm pretty sure it was after a reload since that's when I noticed my colony was starting to have unrest problems and my construction teams were no longer retrieving alien installations. 

I didn't save before reloading, I had saved about one ingame year before and decided to close and not save because I forgot about a bunch of lifepods and let them die and I wanted to replay it, so after reloading and trying to play the same way is when I noticed they were missing.
Title: Re: Disappearing Ground Units
Post by: Froggiest1982 on April 28, 2020, 05:29:47 AM
BTW can I just clarify if the units disappear after save, or after the game is reloaded?

quite sure reloaded
Title: Re: Disappearing Ground Units
Post by: Doren on April 28, 2020, 05:40:35 AM
Maybe some form of save event log for ground troops could be introduced? Would contain the troops that were saved so that people who are trying to reproduce this could look at them and see which ones went missing? Could be hidden by default

Not sure if worth the trouble though as I guess people could copy the Total force text and compare it to after save reload and even against previous save
Title: Re: Disappearing Ground Units
Post by: SpikeTheHobbitMage on April 28, 2020, 06:51:55 AM
Maybe some form of save event log for ground troops could be introduced? Would contain the troops that were saved so that people who are trying to reproduce this could look at them and see which ones went missing? Could be hidden by default

Not sure if worth the trouble though as I guess people could copy the Total force text and compare it to after save reload and even against previous save
One of the things that has been suggested is a more verbose error log, possibly making it general across all tables.

Another possible event chain that complicates diagnosis is a failed save followed by a reload and then creating more units before noticing the problem.

One of the fun possibilities is that more people might be affected than we know about, because if the units are still in memory after the failed save and something happens that clears the cause of the error, then the next save will restore the table with no evidence that there was ever a problem.
Title: Re: Disappearing Ground Units
Post by: Omnivore on April 28, 2020, 07:07:45 AM
Caution: pure speculation follows -

Or it might be that the save/load mechanisms have nothing to do with the bug.  Being that the items (game objects)  are being stored in a list and that list is basically a table with records as entries, somewhere there is a mechanism (dictionary/hash table) for translating record ids into list indexes or references to the items themselves perhaps.  In any case, what if the AI is, when marking an item as destroyed (or deleting), it is somehow getting the wrong id?   

Or it may be something else completely different, I'm going to force myself to stop posting on this issue because I'm down to pure guesswork past the above.

Title: Re: Disappearing Ground Units
Post by: SpikeTheHobbitMage on April 28, 2020, 07:17:30 AM
Or it may be something else completely different, I'm going to force myself to stop posting on this issue because I'm down to pure guesswork past the above.
I'll second this.  We've helped as much as we reasonably can with the information available.  The ball is in Steve's court now.  All we can do now from our end is to wait and see.  Cheers.
Title: Re: Disappearing Ground Units
Post by: db48x on April 28, 2020, 09:35:35 AM
For this type of problem I personally use RR to debug (rr-project.org). It records the entire execution of the program (with minimal overhead), and lets you replay the program backwards and forwards. If you can capture the bug in a recording even once, it becomes very easy to track down where the data was lost or corrupted. You can literally set a watchpoint on a variable or memory location that has the wrong value, then run the program in reverse (as weird as that sounds) until the watchpoint indicates that the value was changed. The program will stop right where this happens, so you can see what was going on. It's basically a superpower.

The downside in this case is that RR only works in Linux, and only on Intel CPUs (it uses the performance counters that Intel added a few years back to do the tracing without adding insane overhead).

However, Microsoft has announced that they're working on this same kind of feature for MSVC, and it's already becoming available for some use cases. I think you have to pay extra for it, and you have to run your program in Azure (Microsoft's computer rental service), and probably jump through some other hoops, but I really recommend looking into it. It makes these kinds of bugs so easy to track down that it's definitely worth it.
Title: Re: Disappearing Ground Units
Post by: Steve Walmsley on April 28, 2020, 12:07:58 PM
Thanks everyone for their help in trying to track this problem and the help in improving the save code.

I'm going to release v1.9 soon, which won't have the new save code, because I don't want to make too many changes at once. Then I will work on the save code change and error logging for the next version.
Title: Re: Disappearing Ground Units
Post by: AlitarSemiramis on April 28, 2020, 12:22:04 PM
For this type of problem I personally use RR to debug (rr-project.org). It records the entire execution of the program (with minimal overhead), and lets you replay the program backwards and forwards. If you can capture the bug in a recording even once, it becomes very easy to track down where the data was lost or corrupted. You can literally set a watchpoint on a variable or memory location that has the wrong value, then run the program in reverse (as weird as that sounds) until the watchpoint indicates that the value was changed. The program will stop right where this happens, so you can see what was going on. It's basically a superpower.

The downside in this case is that RR only works in Linux, and only on Intel CPUs (it uses the performance counters that Intel added a few years back to do the tracing without adding insane overhead).

However, Microsoft has announced that they're working on this same kind of feature for MSVC, and it's already becoming available for some use cases. I think you have to pay extra for it, and you have to run your program in Azure (Microsoft's computer rental service), and probably jump through some other hoops, but I really recommend looking into it. It makes these kinds of bugs so easy to track down that it's definitely worth it.

You can do similar things already in C# with several different Microsoft tools: Intellitrace, ETLs, WinDBG/SoS. Though It's hard to do this type of postmortem profiling/investigation unless you have repro steps.
Title: Re: Disappearing Ground Units
Post by: Second Foundationer on April 28, 2020, 12:59:48 PM
In case it is still of use and not already solved in 1. 9.  While rummaging around a bit in my aurora manual backup folder, recycle and backup bins in an unsuccessful search for a buried replica of the 1. 8 Antarctic db described in my earlier post, I found that the new, less setup-intensive game I started after that, shows the same issue caught at exactly the right moment, and this time, without any obvious Microsoft in the head disturbance to Aurora saving or loading.  In the db, ground unit formations seem to be there: names, IDs, parent formations, locations and all; and the formation elements, too, as far as I can tell; but from this db, none of them load into Aurora, and with any subsequent save (including load/do nothing/save/quit), the units vanish completely from the records.  – At least the player units, I don't know about NPRs.

One thing I notice in the db is that some NPR formations seem to have both PopulationID and ShipID set to 0.  I don't know what that means and if that's intended, but it feels like an error.  Maybe some bugged NPR unit transport? Or a save issue after all? Or it may be a different disappearing units bug from the main one as someone suggested.
Title: Re: Disappearing Ground Units
Post by: db48x on April 28, 2020, 01:16:56 PM
You can do similar things already in C# with several different Microsoft tools: Intellitrace, ETLs, WinDBG/SoS. Though It's hard to do this type of postmortem profiling/investigation unless you have repro steps.

I've not used Intellitrace, but I've heard that it's pretty useful. From what I understand though, it only records things that you've actually seen in the debugger as you stepped through. That does let you step back to any point you've already been. RR lets you record the whole program from start to finish, and then you can debug any point in the recording.

Steps to reproduce are very important; it's no good having any kind of recording if you can't reproduce the bug in one. Once you do catch the bug in a recording, you can replay it as many times as is necessary. One useful strategy has been to record every execution of the program, and only keep those recordings that happened to catch bugs. Another is to record all your automated tests. When a test fails you save the recording for examination.

If Steve had a Linux machine, I could record Aurora every time I ran it then send him the recording once I saw the bug happen. He'd then be able to debug the recording to track down the problem. Second Foundationer seems to have found some very important information. He's found a database where all the ground formations seem to have been saved to the database, but their location information is invalid. That tells us a lot about the error, but it still isn't the smoking gun. If we had a recording of the program as it saved that database, Steve would be able to follow that 0 ID back to where it came from. Maybe the real PopulationID was overwritten, or this copy of the data wasn't completely initialized. Either way, the recording would perfectly preserve that information for Steve to discover.
Title: Re: Disappearing Ground Units
Post by: Steve Walmsley on April 28, 2020, 01:32:16 PM
In case it is still of use and not already solved in 1. 9.  While rummaging around a bit in my aurora manual backup folder, recycle and backup bins in an unsuccessful search for a buried replica of the 1. 8 Antarctic db described in my earlier post, I found that the new, less setup-intensive game I started after that, shows the same issue caught at exactly the right moment, and this time, without any obvious Microsoft in the head disturbance to Aurora saving or loading.  In the db, ground unit formations seem to be there: names, IDs, parent formations, locations and all; and the formation elements, too, as far as I can tell; but from this db, none of them load into Aurora, and with any subsequent save (including load/do nothing/save/quit), the units vanish completely from the records.  – At least the player units, I don't know about NPRs.

One thing I notice in the db is that some NPR formations seem to have both PopulationID and ShipID set to 0.  I don't know what that means and if that's intended, but it feels like an error.  Maybe some bugged NPR unit transport? Or a save issue after all? Or it may be a different disappearing units bug from the main one as someone suggested.

Found it! Well, 95% of it :)

Thanks for the db. There are four units out of 872 that have 0 for both PopulationID and ShipID. When formations are loaded, any that do not have either a PopulationID and ShipID or skipped so they don't cause errors.

Except, I didn't type 'continue' in the foreach, I typed 'return'. Oops!

I still don't know why those NPR units lost the IDs, but that is a minor problem compared to the effect it had. Also, this only affects ground units, not ships, so any ship-related issue is a separate problem.

Fixed now. I will put out a v1.9.1 with the fix, but it will probably be tomorrow as we are about to open a bottle of wine :)
Title: Re: Disappearing Ground Units
Post by: skoormit on April 28, 2020, 02:20:04 PM
...
Except, I didn't type 'continue' in the foreach, I typed 'return'. Oops!
...

Ahhhhhhhhh...that satisfying feeling when you finally find it.

It's like banging your head against a wall for a few hours, because it feels so good when you stop.
Title: Re: Disappearing Ground Units
Post by: Bags77 on April 26, 2021, 02:21:37 AM
Hey Guys
I just had all my ground unit dissapear in my latest game.  It seems to be version 1. 51 (at least this is what the rar file was called, the install thread still describes it as version 1. 31)
I see that this bugs is supposed to be fixed in version 1. 9. 1 almost a year ago.
So my question: is the download link in this forum not updated anymore? If so: where can one get the latest version?
Title: Re: Disappearing Ground Units
Post by: Garfunkel on April 26, 2021, 06:31:10 AM
Hey Guys
I just had all my ground unit dissapear in my latest game.  It seems to be version 1. 51 (at least this is what the rar file was called, the install thread still describes it as version 1. 31)
I see that this bugs is supposed to be fixed in version 1. 9. 1 almost a year ago.
So my question: is the download link in this forum not updated anymore? If so: where can one get the latest version?
First of all, you should register your account so that you can post normally. Until you register, a moderator has to manually approve each post you make.

Secondly, the latest version is now 1.13.0, Steve just released it this weekend. You can download it here:
http://aurora2.pentarch.org/index.php?topic=10635.0

Just delete your current Aurora folder completely and download that one. Welcome!
Title: Re: Disappearing Ground Units
Post by: Jeltz on May 17, 2021, 01:08:22 PM
Hmmm... I don't know if it's a related issue or a desired game mechanic: a small troop transport with troops in refit (I forgot to unload them...); when the refit is complete the troops are gone...

v.1.13, no mods.

J.
Title: Re: Disappearing Ground Units
Post by: Droll on May 17, 2021, 02:15:16 PM
Hmmm... I don't know if it's a related issue or a desired game mechanic: a small troop transport with troops in refit (I forgot to unload them...); when the refit is complete the troops are gone...

v.1.13, no mods.

J.

You should repost this in the bug thread
Title: Re: Disappearing Ground Units
Post by: Jeltz on May 17, 2021, 04:02:02 PM
Hmmm... I don't know if it's a related issue or a desired game mechanic: a small troop transport with troops in refit (I forgot to unload them...); when the refit is complete the troops are gone...

v.1.13, no mods.

J.

You should repost this in the bug thread

Ok, done  ;)

J.
Title: Re: Disappearing Ground Units
Post by: jrnia on August 05, 2022, 11:55:05 AM
Make sure you add units to your formation.   You can train empty formation, but it's gonna disappear on the next tick.   

- your units build super fast
- disappear after
=> you didn't add units to your formation