Repository navigation
Fix concurrent map script entry leaving players half-transferred - #353
Open
asaf-shitrit wants to merge 1 commit into
Open
asaf-shitrit wants to merge 1 commit into
asaf-shitrit wants to merge 1 commit into
Conversation
asaf-shitrit
force-pushed
the
fix/map-script-concurrent-entry
branch
from
September 15, 2026 12:19
ebddbbe to
c248723
Compare
MinatoNami
added a commit
to MinatoNami/Cosmic-AI
that referenced
this pull request
Sep 20, 2026
…nt-entry #patch
| return true; | ||
| } catch (final ScriptException | NoSuchMethodException e) { | ||
| e.printStackTrace(); | ||
| } catch (final Exception e) { |
There was a problem hiding this comment.
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
Author
There was a problem hiding this comment.
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
force-pushed
the
fix/map-script-concurrent-entry
branch
from
November 4, 2026 12:02
c248723 to
fac51e6
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
MapScriptManagercaches 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
Only
ScriptExceptionandNoSuchMethodExceptionwere caught, so this escapedMapleMap#addPlayerpartway throughCharacter#changeMapInternal. At that point: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/explorationPointis 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
EventScriptManageralready applies to its shared engines (SynchronizedInvocable.of):SynchronizedInvocable, so concurrent entry is serialised while sequential use from different threads still worksConcurrentHashMapwithputIfAbsent, so two players loading the same script at once end up sharing one engineScriptExceptionandNoSuchMethodExceptionaround the cached engine'sinvokeFunction: with access serialised, script failures all arrive asScriptException. Log at ERROR instead ofprintStackTrace. A cached script that fails is no longer re-run on a fresh engine.MapleMap#addPlayerreleases 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 testpasses (JDK 21, 1886 tests)SPAWN_PLAYERfor each character, with party map ids updated.