Fractal Softworks Forum

Please login or register.

Login with username, password and session length
Advanced search  

News:

Starsector 0.95.1a is out! (12/10/21); Blog post: The Pilgrim's Path (07/19/22)

Author Topic: [0.95a-RC15] Random ConcurrentModificationException in combat  (Read 1097 times)

briansd9

  • Ensign
  • *
  • Posts: 45
    • View Profile

Got a random crash. Playing with a ton of mods, but the stack trace seems entirely in the base game:

Code
22651134 [Thread-3] ERROR com.fs.starfarer.combat.CombatMain  - java.util.ConcurrentModificationException
java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.checkForComodification(Unknown Source)
at java.util.ArrayList$Itr.next(Unknown Source)
at com.fs.starfarer.combat.E.A.o00000(Unknown Source)
at com.fs.starfarer.combat.E.A.o00000(Unknown Source)
at com.fs.starfarer.combat.CombatEngine.advanceInner(Unknown Source)
at com.fs.starfarer.combat.CombatEngine.advance(Unknown Source)
at com.fs.starfarer.combat.CombatState.traverse(Unknown Source)
at com.fs.state.AppDriver.begin(Unknown Source)
at com.fs.starfarer.combat.CombatMain.main(Unknown Source)
at com.fs.starfarer.StarfarerLauncher.o00000(Unknown Source)
at com.fs.starfarer.StarfarerLauncher$1.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

Fought the battle again after restarting the game, everything worked fine. Is there any other information I can provide?
« Last Edit: July 16, 2021, 07:55:11 AM by briansd9 »
Logged

Alex

  • Administrator
  • Admiral
  • *****
  • Posts: 20604
    • View Profile
Re: [0.95a-RC15] Random ConcurrentModificationException in combat
« Reply #1 on: July 16, 2021, 08:06:36 AM »

Thank you for the report! Hmm - unfortunately with this kind of error it's really hard to tell what' the ultimate cause is, regardless of what's in the stack trace. (It does appear to be something collision-related, but that's not definitive.)

Posting your full mod list would be helpful in case someone has run into the same error and the mod list can be compared to narrow down what might be causing the problem.
Logged

Sutopia

  • Admiral
  • *****
  • Posts: 1005
    • View Profile
Re: [0.95a-RC15] Random ConcurrentModificationException in combat
« Reply #2 on: July 16, 2021, 08:39:05 AM »

This could happen if a mod is accessing the array list that was not meant to be touched and then when base game tried to access it the Java just throws the error.

But what? Is some mod trying to mess with advance method collection? That doesn’t sound safe at all!
Logged

briansd9

  • Ensign
  • *
  • Posts: 45
    • View Profile
Re: [0.95a-RC15] Random ConcurrentModificationException in combat
« Reply #3 on: July 16, 2021, 04:08:12 PM »

Code
AdvancedGunneryControl 0.10.1
Arma Armatura 1.4.5d
Arsenal Expansion 1.5.4a
Brighton Federation 0.0.1i
CWSP 1.2
Captain's Log 0.1.2
Caymon's Ship pack 1.0.7
Cebby Ship Pack 1.1.0
Combat Chatter 1.11.2
Console Commands 2021.4.10
DIY Planets 1.0.13
Detailed Combat Results 5.1.2
Diable Avionics 2.61
Experimental Ship Pack (EXPSP) 0.36
Fleet Action History 1.0.3
Fluff Ship Pack 0.2.3
Girls Frontline Portrait Pack 1.6.1
Girls Frontline Portrait Pack Part 2 1.1.1
HMI Supervillains 0.0.1g
Hazard Mining Incorporated 0.3.3q
High Tech Expansion 1.4.3
Hiigaran Descendants 2.1
Industrial.Evolution 2.2.b
Interesting Portraits Pack 1.2
Kadur Remnant 3.2.3
Keruvim Shipyards 0.2
Kingdom of Terra 0.13.1
LazyLib 2.6
Legacy of Arkgneisis v1.9.9
Luddic Enhancement 1.2.4d
Magellan Protectorate 1.08a
MagicLib 0.34
Mayasuran Navy 8.3.4 RC3
Missing Ships 0.4.1
More HullMods 1.8.0
More Ship Names 1.0
Musashi Manufactorum 1.1.1
Nexerelin 0.10.1c
No Such Organization - Phase ships 0.2.3
Oculian Armada 0.9.0
Oculian Pirates addon 0.2.1
Outer Rim Alliance 0.93rc3
P9 Colony Group 0.52a
Paraqueeght Mod 1.0
Pulse Industry 1.0
RoctesRats 0.12
Roider Union 1.2.2
Ruthless Sector 1.2.8
ScalarTech Solutions 0.7
Scy Nation 1.64
Seeker - Unidentified Contact 0.42
Shadowyards 0.9.5-rc4
SpeedUp 0.7.1
Tahlan Shipworks 0.7
The Exalted 1.3.2
The Star Federation 0.9.0 pre
Torchships And Deadly Armaments 0.16rc2
Underworld 1.6.1
VIC 1.3.1
Xhan Empire 2.21 Plans set in motion
asteroid ship pack 1.3a
prv Starworks v21
zz GraphicsLib 1.5.1
Logged

Morrokain

  • Admiral
  • *****
  • Posts: 2143
  • Megalith Dreadnought - Archean Order
    • View Profile
Re: [0.95a-RC15] Random ConcurrentModificationException in combat
« Reply #4 on: July 16, 2021, 08:11:03 PM »

I run into this when I am iterating through a list, and subsequently try and remove something from the list based upon certain conditions. Even if the list can technically have things removed and that would be fine, you can't do both at once.

Instead, w.e mod that is trying this needs to copy the list to a temporary one, remove any items from the temporary list, and then assign that list back to the original list once the iterator is finished.

Or, if that isn't possible, store any removed items in a completely new list and use that list's iterator to remove them from the original list once the original list has been fully iterated through.

As to what mod is running into this problem, that's anyone's guess really.
Logged

briansd9

  • Ensign
  • *
  • Posts: 45
    • View Profile
Re: [0.95a-RC15] Random ConcurrentModificationException in combat
« Reply #5 on: July 17, 2021, 01:19:37 AM »

It was a rather large battle against a Kingdom of Terra fleet, so the culprit can be narrowed down to that or one of the modships I used. Will track down list of possible mods later



Logged

Nick XR

  • Admiral
  • *****
  • Posts: 589
    • View Profile
Re: [0.95a-RC15] Random ConcurrentModificationException in combat
« Reply #6 on: July 19, 2021, 12:41:23 PM »

Thank you for the report! Hmm - unfortunately with this kind of error it's really hard to tell what' the ultimate cause is, regardless of what's in the stack trace. (It does appear to be something collision-related, but that's not definitive.)

Posting your full mod list would be helpful in case someone has run into the same error and the mod list can be compared to narrow down what might be causing the problem.

@Alex  This error scenario (exception from deep in the engine and no way to tell what mod might be causing it) seems pretty common and also seems almost impossible to divine which mod is causing the problem.  What if you created a subclass of ArrayList.Iter that only overrode the method `checkForComodification` to print the types in the list in the stack trace?  Then used that subclass everywhere.  Or maybe there's an even fancier way to use injection at runtime to just do it automatically?  I know what I just said sounds like a crazy and bad idea, but I can't think of any better way to solve the problem.  Maybe if this just happens in a few for-each loops you could try/catch and deliver a useful exception there?

Alex

  • Administrator
  • Admiral
  • *****
  • Posts: 20604
    • View Profile
Re: [0.95a-RC15] Random ConcurrentModificationException in combat
« Reply #7 on: July 19, 2021, 01:36:31 PM »

I don't think that's readily doable. Never mind that I'd have to refactor <checks> about 2,000 cases where I'm using ArrayList. But it's also not clear to me how you'd efficiently track what might've removed something from the list, since that can be done not in the iterator, but in the list's .remove() method.

Fundamentally, the problem is that the error pops up not where the "bad" code is, but somewhere else. Which does make it really hard to track down, but I don't think what you're proposing is workable, unfortunately.
Logged

Sutopia

  • Admiral
  • *****
  • Posts: 1005
    • View Profile
Re: [0.95a-RC15] Random ConcurrentModificationException in combat
« Reply #8 on: July 19, 2021, 02:56:29 PM »

Is advace() methods running multi thread in first place?
The possibilities I can think of is
1. Someone held the iterator reference
2. Someone spawned a standalone working thread instead of utilizing float amount passed in

Which I admit I did 2. in one of my mod in an older version which mess things up big times.
Logged

Alex

  • Administrator
  • Admiral
  • *****
  • Posts: 20604
    • View Profile
Re: [0.95a-RC15] Random ConcurrentModificationException in combat
« Reply #9 on: July 19, 2021, 03:03:47 PM »

Is advace() methods running multi thread in first place?
The possibilities I can think of is
1. Someone held the iterator reference
2. Someone spawned a standalone working thread instead of utilizing float amount passed in

Which I admit I did 2. in one of my mod in an older version which mess things up big times.

No, it's single-threaded. You can easily get a concurrent modification exception in single-threaded code. For example:

for (String curr : list} {
    list.remove(0);
}
Sort of thing. Basically anything that modifies a collection while something is also iterating over it.

(And, hmm, running things like this in threads is generally a bad idea. It might seem to work, but the vast majority of core code is *not* thread-safe, so it's likely to just be a crash of weird bug waiting to happen.)
Logged

Sutopia

  • Admiral
  • *****
  • Posts: 1005
    • View Profile
Re: [0.95a-RC15] Random ConcurrentModificationException in combat
« Reply #10 on: July 19, 2021, 03:07:06 PM »

Is advace() methods running multi thread in first place?
The possibilities I can think of is
1. Someone held the iterator reference
2. Someone spawned a standalone working thread instead of utilizing float amount passed in

Which I admit I did 2. in one of my mod in an older version which mess things up big times.

No, it's single-threaded. You can easily get a concurrent modification exception in single-threaded code. For example:

for (String curr : list} {
    list.remove(0);
}
Sort of thing. Basically anything that modifies a collection while something is also iterating over it.

(And, hmm, running things like this in threads is generally a bad idea. It might seem to work, but the vast majority of core code is *not* thread-safe, so it's likely to just be a crash of weird bug waiting to happen.)

From OP it suggested actual concurrent access due to inconsistent behavior after loading the save.
Which indicates high possibility of racing conditions.
Logged

Alex

  • Administrator
  • Admiral
  • *****
  • Posts: 20604
    • View Profile
Re: [0.95a-RC15] Random ConcurrentModificationException in combat
« Reply #11 on: July 19, 2021, 03:23:20 PM »

From OP it suggested actual concurrent access due to inconsistent behavior after loading the save.
Which indicates high possibility of racing conditions.

Hmm - it's not impossible that it's some kind of ill-advised mod-thread-code-thing, but it's also easy to see how the battle playing out slightly differently would lead to inconsistent results if the error only triggers in some specific set of circumstances.
Logged