ArrayList fastRemove exception

Post here your questions about SFS2X. Here we discuss all server-side matters. For client API questions see the dedicated forums.

Moderators: Lapo, Bax

User avatar
Zelek
Posts: 36
Joined: 24 Jun 2010, 22:36

ArrayList fastRemove exception

Postby Zelek » 20 Nov 2013, 05:45

I am getting an ArrayIndexOutOfBoundsException in my RequestHandler, and it's apparently happening in ArrayList.fastRemove(). This is the offending code in my extension:

Code: Select all

if (_playerHashMap.containsKey(location)) {
            List<Entity> playerList = _playerHashMap.get(location);

            if (playerList.contains(player)) {
                playerList.remove(player); // <-- this is where the exception occurs
            }
}


The exception is fairly rare, and it occurs even when I'm the only client connected to the server, so I'm not sure what the cause could be. Since the line right before verifies that the Player is in the List, I can only assume that two requests are being executed simultaneously. Does that seem plausible?
User avatar
Lapo
Site Admin
Posts: 21066
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: ArrayList fastRemove exception

Postby Lapo » 20 Nov 2013, 08:25

Could you please report the full stack trace of the error?
The List.remove(...) method is not documented to throw an ArrayOutOfBoundException. What kind of list implementation are you using?
Lapo
--
gotoAndPlay()
...addicted to flash games
User avatar
Zelek
Posts: 36
Joined: 24 Jun 2010, 22:36

Re: ArrayList fastRemove exception

Postby Zelek » 20 Nov 2013, 09:20

Here's the stack trace:

Code: Select all

19 Nov 2013 | 20:26:41,421 | ERROR | com.smartfoxserver.v2.controllers.ExtensionController-3 | v2.controllers.ExtensionController |     | java.lang.ArrayIndexOutOfBoundsException:
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
Exception: java.lang.ArrayIndexOutOfBoundsException
Message: -1
Description: Error while handling client request in extension: { Ext: GalaxiesExtension, Type: JAVA, Lev: ZONE, { Zone: GalaxiesZone }, {} }
Extension Cmd: MoveRequest
+--- --- ---+
Stack Trace:
+--- --- ---+
java.util.ArrayList.fastRemove(Unknown Source)
java.util.ArrayList.remove(Unknown Source)
com.codecommode.Managers.EntityManager.updateActiveEnt(EntityManager.java:123)
com.codecommode.Entity.Entity.updateLocation(Entity.java:249)
com.codecommode.Managers.MoveManager.handleMove(MoveManager.java:42)
com.codecommode.Requests.MoveRequest.handleInternalRequest(MoveRequest.java:84)
com.codecommode.Requests.ExclusiveRequestHandler.handleClientRequest(ExclusiveRequestHandler.java:25)
com.codecommode.Requests.MoveRequest.handleClientRequest(MoveRequest.java:31)
com.smartfoxserver.v2.extensions.SFSExtension.handleClientRequest(SFSExtension.java:192)
com.smartfoxserver.v2.controllers.ExtensionController.processRequest(ExtensionController.java:133)
com.smartfoxserver.bitswarm.controllers.AbstractController.run(AbstractController.java:96)
java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
java.lang.Thread.run(Unknown Source)


The code that I showed in my original post was line 123 of EntityManager.java. I am using an ArrayList.
User avatar
Lapo
Site Admin
Posts: 21066
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: ArrayList fastRemove exception

Postby Lapo » 20 Nov 2013, 09:41

Thanks,
it's a concurrency issue with the ArrayList. If two threads are running the same remove() operation "simultaneously" the problem will arise. In general all Java standard List implementations are not thread safe.

Two basic possible solutions:
1- synchronize manually
2- User Collections.synchronizedList(...) ( see Oracle javadoc )

cheers
Lapo

--

gotoAndPlay()

...addicted to flash games

Return to “SFS2X Questions”

Who is online

Users browsing this forum: JerodSa and 19 guests