Skip to content

Fix concurrent map script entry leaving players half-transferred - #353

Open
asaf-shitrit wants to merge 1 commit into
P0nk:masterfrom
asaf-shitrit:fix/map-script-concurrent-entry
Open

asaf-shitrit wants to merge 1 commit into
P0nk:masterfrom
asaf-shitrit:fix/map-script-concurrent-entry

Conversation

@asaf-shitrit

@asaf-shitrit asaf-shitrit commented Sep 15, 2026 •

Copy link
Copy Markdown

Summary

MapScriptManager caches one script engine per map script and invokes it from whichever channel event loop the entering player is on. When two players trigger the same script at the same instant, GraalJS refuses the second thread and throws. That exception escapes the middle of a map change and leaves the character half-transferred.

The bug

IllegalStateException: Multi threaded access requested by thread ... but is not allowed for language(s) js

Only ScriptException and NoSuchMethodException were caught, so this escaped MapleMap#addPlayer partway through Character#changeMapInternal. At that point:

Step Result
Character added to the map's player list, map id updated done
Map script runs throws
Spawn broadcast to other players in the map never runs
Party map update never runs

The character becomes a "ghost": the server has it in the map, but no one else can see it, its party entry still shows the previous map, and its next login does not load cleanly.

Why it is wider than one map

Engines are cached per script, not per map. onUserEnter/explorationPoint is shared by the town maps of Victoria Island, Sleepywood, El Nath, Ludibrium, Aqua Road, Mu Lung, Nihal Desert and Leafre, so two players entering two different towns at the same moment can collide. The most natural way to hit it is a party entering a map through a portal together.

Fix

The same guard EventScriptManager already applies to its shared engines (SynchronizedInvocable.of):

  • wrap each cached engine in SynchronizedInvocable, so concurrent entry is serialised while sequential use from different threads still works
  • keep engines in a ConcurrentHashMap with putIfAbsent, so two players loading the same script at once end up sharing one engine
  • keep catching only ScriptException and NoSuchMethodException around the cached engine's invokeFunction: with access serialised, script failures all arrive as ScriptException. Log at ERROR instead of printStackTrace. A cached script that fails is no longer re-run on a fresh engine.

MapleMap#addPlayer releases its character write lock before running map scripts, so the new lock adds no lock ordering risk.

How it was found

On a private test server running several automated clients that follow a player through portals, so they routinely entered maps within milliseconds of each other. That timing is rare for human players, which is likely why this has gone unnoticed.

Testing

  • mvn test passes (JDK 21, 1886 tests)
  • Before the fix: the owner and two following clients re-entering Kerning City together reproduced the exception and a ghosted character.
  • After the fix: four repeated simultaneous three-client entries between Kerning City and 103000100 produced no exceptions, and on every entry the other clients received SPAWN_PLAYER for each character, with party map ids updated.

@asaf-shitrit
asaf-shitrit force-pushed the fix/map-script-concurrent-entry branch from ebddbbe to c248723 Compare September 15, 2026 12:19
MinatoNami added a commit to MinatoNami/Cosmic-AI that referenced this pull request Sep 20, 2026
return true;
} catch (final ScriptException | NoSuchMethodException e) {
e.printStackTrace();
} catch (final Exception e) {

@artemmikhalitsin artemmikhalitsin Oct 27, 2026 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No reason to blanket-catch exception here, as both of the ways that invokeFunction can throw an unchecked exception are covered. This would only serve to silently hide future programming errors

NullPointerException - impossible when function name is specified in-line
IllegalArgumentException - again, script object specified in-line

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Narrowed in fac51e6. With access serialised, JS errors, host exceptions and stack overflow from start all arrive as ScriptException (checked on js-scriptengine 24.0.1). Left the second catch as on master: it also guards getInvocableScriptEngine.

MapScriptManager caches one script engine per map script and shares it
between every player who triggers that script. Players are handled on
different channel event loop threads, and a GraalJS context refuses to
be entered by a second thread while another is inside it:

  IllegalStateException: Multi threaded access requested by thread ...
  but is not allowed for language(s) js

Only ScriptException and NoSuchMethodException were caught, so this
escaped MapleMap#addPlayer partway through Character#changeMapInternal.
By then the character was already in the map's player list with its map
id updated, but the spawn broadcast and the party map update never ran:
the character was invisible to everyone else in the map, its party entry
showed the previous map, and its next login did not load cleanly.

The engine is cached per script, not per map, so the exposure is wider
than one map. onUserEnter/explorationPoint is shared by the town maps of
Victoria Island, Sleepywood, El Nath, Ludibrium, Aqua Road, Mu Lung,
Nihal Desert and Leafre, so two players entering two different towns in
the same instant can collide. The most natural way to hit it is a party
entering a map through a portal together.

Fix it the way EventScriptManager already guards its shared engines:
- wrap each cached engine in SynchronizedInvocable, so concurrent entry
  is serialised while sequential use from different threads still works
- store engines in a ConcurrentHashMap with putIfAbsent, so two players
  loading the same script at once end up sharing one engine
- keep catching only ScriptException and NoSuchMethodException around
  the cached engine's invokeFunction: with access serialised, script
  failures all arrive as ScriptException. Log at ERROR instead of
  printStackTrace. A cached script that fails is no longer re-run on a
  fresh engine

MapleMap#addPlayer releases its character write lock before running map
scripts, so the new lock adds no lock ordering risk.
@asaf-shitrit
asaf-shitrit force-pushed the fix/map-script-concurrent-entry branch from c248723 to fac51e6 Compare November 4, 2026 12:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants