Jump to content

serializationtest crashes (segfault) one turn after it starts, in games with AIs


wasname321
 Share

Recommended Posts

Running a replay with -serializationtest=N segfaults at turn N+1 for me, in every game with Petra AIs I tried. For example skirmishes/acropolis_bay_2p with two Petra AIs and N = 200, on a clean release build of main 47f5fb4f (no local changes). The commits since then don't touch the simulation code.

Backtrace, shortened:

```
JS::shadow::Zone::needsIncrementalBarrier (this=0x0)
js::gc::PreWriteBarrierImpl
JS::Heap<JS::Value>::~Heap
ICmpTemplateManager::~ICmpTemplateManager      (m_CachedInstance)
CCmpTemplateManager::Deallocate
CComponentManager::ResetState
CComponentManager::~CComponentManager
CSimulation2Impl::Update                       (the secondary simulation is rebuilt every turn)
```

What I think happens:
1. The serialization test destroys the previous turn's secondary CComponentManager every turn.
2. ~CComponentManager first removes its extra GC roots tracer, and ResetState() clears m_TraceCache, so the components' cached JS instances aren't traced any more.
3. ResetState() then deletes the components in reverse order. Deleting the AIManager destroys the AI's script interface, and Context::UnRegisterRealm calls JS::FinishIncrementalGC if an incremental GC is running. MaybeIncrementalGC() at the end of each turn usually leaves one running.
4. That GC sweeps the cached instances of the components that haven't been deleted yet.
5. The next destructor, the TemplateManager's JS::Heap<JS::Value> m_CachedInstance, runs a write barrier on the dead cell.

Finishing any incremental GC at the start of ~CComponentManager, before anything stops being traced, fixed it for me:

```
CComponentManager::~CComponentManager()
{
    JSContext* cx = m_ScriptInterface.GetGeneralJSContext();
    if (JS::IsIncrementalGCInProgress(cx))
        JS::FinishIncrementalGC(cx, JS::GCReason::API);
    JS_RemoveExtraGCRootsTracer(cx, Trace, (void*)this);
    ResetState();
}
```

With it, Acropolis Bay ran 100 turns of the serialization test without problems, and a random map game ran 1,000 turns.

Something else I noticed while looking at this, though it wasn't the cause: Update assigns a new m_SecondaryTerrain before it replaces the context and component manager that still point to the old terrain. Resetting the component manager first, then the context, then the terrain avoids that.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...