Welcome to Episode 2 of Concurrency Hell:
Attack on Autopot
Attack on Autopot
1. What the heck?
On March 8th, we experienced a series of deadlocks (3, in fact).
All 3 were related to the same bit of code (which will be explained below).
Our auto deadlock detector kicked in and found the issue almost immediately (within 60 seconds).
This protocol automatically disables login and shuts down the server to ensure player data is safeguarded.
Thankfully, it did its job, and we were able to fully mitigate any impact to player save states.
But unfortunately, our mental states were not as lucky - but let's get back on topic:
2. What Exactly Happened?
Thread dumps showed two locks (the character lock and the inventory lock) being acquired in different orders by different methods - one thread locked the character and then attempted to lock the inventory, while another thread locked the inventory first and then tried to lock the character (the reverse operation).
The code in reapplyLocalStats() locked various character-related locks (chrLock, statLock, etc.) and then called getSlotLimit(), locking the inventory after the character. Meanwhile, other parts of the code locked the inventory first and then called methods that required the character lock. This mismatch caused a circular wait and froze the server. More specifically, the cause was due to the PetAutopotProcessor, which would acquire a lock on user inventories, then within the same lock, will trigger the heal, which triggers obtaining a chrLock - this is the root cause.
3. The Fix
It was quite simple - move the heal operation OUTSIDE of the useLock.
This ensures that the lock on the player inventory has time to complete before a chrLock is acquired.
Meaning, we never hold both locks at once, preventing the circular dependency.
BAD CODE:
Java:
inventory.lockInventory();
try {
// Insert your code that fetches HP values and processes removing the potion
player.updateHpMp(player.getHp() + incHp, player.getMp()); // DEADLOCKS HERE
} finally {
inventory.unlockInventory();
}
BETTER CODE:
Java:
inventory.lockInventory();
try {
// Insert your code that fetches HP values and processes removing the potion
} finally {
inventory.unlockInventory();
}
player.updateHpMp(player.getHp() + incHp, player.getMp()); // FIX: MOVED OUTSIDE OF TRY
All we're doing here is moving the updateHpMp function outside of the inventory lock, because updateHpMp updates the player stat pool, which requires a different lock that the inventory lock will eventually need. Thats it!
4. Why It Should Fix the Problem
Deadlocks occur when two locks are acquired in reverse order by different threads. By eliminating situations where we hold both the character lock and the inventory lock simultaneously, we remove any chance of a circular wait, for this particular bit of contention.
5. How We Confirmed
For me personally, I think the very first step to debugging anything is to replicate the bug.
In our case, we needed a way to ensure we can reliably reproduce the deadlock.
Upon running our simulation tests with just a single pet with a single HP potion, and spamming that across just 3 players, the deadlock was triggered.
We replicated this a few more times, all of which would deadlock the server after 10 seconds of spamming.
To confirm the fix, we simply moved the healing operation outside of the useLock, and, VOILA - the deadlock disappeared.
6. Summary
There was a lot of magic, hair pulling, and investigating behind the scenes, but once we identified the core issue of the 2 locks contending for first place, along with reviewing some of the more recent commits, we were able to successfully bash this deadlock right on its head.
ok ty bye for now
Last edited: