SabberStone icon indicating copy to clipboard operation
SabberStone copied to clipboard

Casting error from Enchantment to ICharacter.

Open lunjohnzhang opened this issue 4 years ago • 5 comments

Hello,

We have implemented a .NETCore Evaluator to iteratively run games. When we try to use the Game.Process(PlayTask gameTask) function to process a game, .NET gives us this error after certain iterations

Unhandled Exception: System.AggregateException: One or more errors occurred. (Unable to cast object of type 'SabberStoneCore.Model.Entities.Enchantment' to type 'SabberStoneCore.Model.Entities.ICharacter'.) ---> System.InvalidCastException: Unable to cast object of type 'SabberStoneCore.Model.Entities.Enchantment' to type 'SabberStoneCore.Model.Entities.ICharacter'. at SabberStoneCore.Model.Game.Process(PlayerTask gameTask) in /Users/yulunzhang/Desktop/Coding/icaros/hearthstone/SabberStone/SabberStoneCore/src/Model/Game.cs:line 486 at DeckEvaluator.Evaluation.GameEvaluator.PlayGame() in /Users/yulunzhang/Desktop/Coding/icaros/hearthstone/EvoStone/DeckEvaluator/src/Evaluation/GameEvaluator.cs:line 165 at DeckEvaluator.Evaluation.GameDispatcher.runGame(Int32 gameId, GameEvaluator ev) in /Users/yulunzhang/Desktop/Coding/icaros/hearthstone/EvoStone/DeckEvaluator/src/Evaluation/GameDispatcher.cs:line 72 at System.Threading.Tasks.Parallel.<>c__DisplayClass19_01.<ForWorker>b__1(RangeWorker& currentWorker, Int32 timeout, Boolean& replicationDelegateYieldedBeforeCompletion) --- End of stack trace from previous location where exception was thrown --- at System.Threading.Tasks.Parallel.<>c__DisplayClass19_01.<ForWorker>b__1(RangeWorker& currentWorker, Int32 timeout, Boolean& replicationDelegateYieldedBeforeCompletion) at System.Threading.Tasks.TaskReplicator.Replica1.ExecuteAction(Boolean& yieldedBeforeCompletion) at System.Threading.Tasks.TaskReplicator.Replica.Execute() --- End of inner exception stack trace --- at System.Threading.Tasks.TaskReplicator.Run[TState](ReplicatableUserAction1 action, ParallelOptions options, Boolean stopOnFirstFailure) at System.Threading.Tasks.Parallel.ForWorker[TLocal](Int32 fromInclusive, Int32 toExclusive, ParallelOptions parallelOptions, Action1 body, Action2 bodyWithState, Func4 bodyWithLocal, Func1 localInit, Action1 localFinally) --- End of stack trace from previous location where exception was thrown --- at System.Threading.Tasks.Parallel.ThrowSingleCancellationExceptionOrOtherException(ICollection exceptions, CancellationToken cancelToken, Exception otherException) at System.Threading.Tasks.Parallel.ForWorker[TLocal](Int32 fromInclusive, Int32 toExclusive, ParallelOptions parallelOptions, Action1 body, Action2 bodyWithState, Func4 bodyWithLocal, Func1 localInit, Action1 localFinally) at System.Threading.Tasks.Parallel.For(Int32 fromInclusive, Int32 toExclusive, ParallelOptions parallelOptions, Action`1 body) at DeckEvaluator.Evaluation.GameDispatcher.Run() in /Users/yulunzhang/Desktop/Coding/icaros/hearthstone/EvoStone/DeckEvaluator/src/Evaluation/GameDispatcher.cs:line 127 at DeckEvaluator.Program.Main(String[] args) in /Users/yulunzhang/Desktop/Coding/icaros/hearthstone/EvoStone/DeckEvaluator/src/Program.cs:line 119 [1] 78423 abort dotnet bin/DeckEvaluator.dll 1

This error exposes a casting error in Game.cs, but when we look through the source code of SabberStone it doesn't seem to have such a casting on the specified place (line 486 of Game.cs). We will appreciate if you can help us with this.

SabberStone version: 2.1.0 (we pull the code from master and compiled it) .NETCore version: 2.1

lunjohnzhang avatar May 14 '20 21:05 lunjohnzhang

Can you publish the code that triggers this exception? It will help with discovering where the issue lies.

Bert-Proesmans avatar May 17 '20 11:05 Bert-Proesmans

So I checked the MASTER ref on your EvoStone fork (https://github.com/lunjohnzhang/EvoStone/tree/a0eb3eb8594fa0d32a49a3c6f99abc056b41eb7d) as is. My worker did a complete run with Evolution as strategy. There weren't any crashes. Without more details, this issue might be caused by EvoStone. When providing more info, mention the commit hash specifically and publish a reproduction if possible, or make your custom code public.

Bert-Proesmans avatar May 17 '20 12:05 Bert-Proesmans

Sorry for the late reply, I was doing something else over the past week.

I have opened another branch here that contains the code to reproduce the error. Commit hash: https://github.com/lunjohnzhang/EvoStone/commit/02ac5568742814846a39d73b3bbbfbdd6bc4196c

To run it, make sure that SabberStoneCore.dll is correctly compiled. Then, please go to TestBed/DeckSearch, and then run python setup.py to set up the project. Then run dotnet bin/DeckEvaluator.dll config/paladin_me.tml error_deck.tml. I remove the threading stuff in the original repo so the error is clearer.

The code is running one round of 200 games between the deck inside error_deck.tml and a fixed set of 200 opponent decks. However, note that the code does not produce the error every time when you run it, and even though it produces the error, it may produce it at different times regardless of the fact that it should be running the same thing every time. For me, it produces the error 7 out of 10 runs. This is another weird thing regarding this error.

Thank you very much for you help and feel free to ask me any questions regarding this project.

@Bert-Proesmans

lunjohnzhang avatar May 25 '20 10:05 lunjohnzhang

So we're specifically looking for an error that isn't 100% reproducible. This happens during a serialized run of 200 games. At about 10 runs of the above command the error should manifest at least a few times.

I'm running now. Hopefully I'll get some reproduction this evening.

Bert-Proesmans avatar May 25 '20 16:05 Bert-Proesmans

Ok, so the casting happens here: https://github.com/HearthSim/SabberStone/blob/master/SabberStoneCore/src/Model/Game.cs#L499

The PlayerTask has been calculated from a different game. It's applied to another game that has forked off and potentially desynced somehow. The target identifier within the gameTask points to a non-character in the forked game.

I don't know if this is intended and badly handled or unintentional. Using git blame appoints @rnilva with this commit https://github.com/HearthSim/SabberStone/commit/6a08c573dd1af05f23eddc4b295169af6be38437

Bert-Proesmans avatar May 25 '20 16:05 Bert-Proesmans