[Bug]: Add-on JavaScript context not cleaned up when removing an add-on.

Open
#6,041 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
42/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Quiet
Tech stack
java, javascript
Domain
desktop, game-dev

Research direction

Start from the stack in AddOnLibrary.java (callMTSFunction / onInit eval) and the remove-library path under File > Add On Libraries. Trace how addon JS namespaces and Graal/polyglot contexts are created versus torn down so leftover namespaces (data.listNamespaces("addon:")) and const redefinition errors go away. Done when removing a library clears its context and re-adding the same add-on works without ParserException.

Written by the indexing model from the issue text.

Description

bug
Describe the Bug

When an add-on is installed in a campaign, a new context is created within which the add-on's JavaScript can executed. When the add-on is removed, this context remains. Adding the same add-on again can then cause errors (e.g.,, if the same variables gets defined) or fail for logical reasons if old state is used.

To Reproduce
  1. Download this example add-on: collision-example.mtlib
  2. Drag the add-on into a blank campaign.
  3. Open File > Add On Libraries ..., select the add-on and click Remove Library.
    • Notice that typing [r: data.listNamespaces("addon:")] in the chat window results in the add-on namespace still be visible.
  4. Drag the add-on into the campaign again.
  5. Notice the error that gets presented.

The key detail in the attached add-on is that it evals this line during onInit:

const namespace = 'com.kwvanderlinde.collision';

The display of the error is not 100% consistent, so you may have to repeat steps (3) & (4) a couple times.

Expected Behaviour

When an add-on is removed, all of its state is also removed (namespace and associated context). It should be possible to add the add-on again to the same campaign without issue (unless the add-on itself is at fault for doing something very bad).

Screenshots

No response

MapTool Info

1.18.6

Desktop

Linux Mint 22.1

Additional Context

Here is the error:

java.util.concurrent.CompletionException: net.rptools.parser.ParserException: com.oracle.truffle.polyglot.PolyglotEngineException error executing expression js.evalURI("com.kwvanderlinde.collision", "lib://com.kwvanderlinde.collision/example.js").
	at net.rptools.maptool.model.library.addon.AddOnLibrary.lambda$callMTSFunction$15(AddOnLibrary.java:672)
	at java.desktop/java.awt.event.InvocationEvent.dispatch(Unknown Source)
	at java.desktop/java.awt.EventQueue.dispatchEventImpl(Unknown Source)
	at java.desktop/java.awt.EventQueue$4.run(Unknown Source)
	at java.desktop/java.awt.EventQueue$4.run(Unknown Source)
	at java.base/java.security.AccessController.doPrivileged(Unknown Source)
	at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
	at java.desktop/java.awt.EventQueue.dispatchEvent(Unknown Source)
	at net.rptools.maptool.client.swing.MapToolEventQueue.dispatchEvent(MapToolEventQueue.java:63)
	at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
	at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
	at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
	at java.desktop/java.awt.WaitDispatchSupport$2.run(Unknown Source)
	at java.desktop/java.awt.WaitDispatchSupport$4.run(Unknown Source)
	at java.desktop/java.awt.WaitDispatchSupport$4.run(Unknown Source)
	at java.base/java.security.AccessController.doPrivileged(Unknown Source)
	at java.desktop/java.awt.WaitDispatchSupport.enter(Unknown Source)
	at java.desktop/java.awt.Dialog.show(Unknown Source)
	at java.desktop/javax.swing.JOptionPane.showOptionDialog(Unknown Source)
	at java.desktop/javax.swing.JOptionPane.showMessageDialog(Unknown Source)
	at java.desktop/javax.swing.JOptionPane.showMessageDialog(Unknown Source)
	at net.rptools.maptool.client.MapTool.showMessage(MapTool.java:257)
	at net.rptools.maptool.client.MapTool.showInformation(MapTool.java:380)
	at net.rptools.maptool.client.MapTool.showInformation(MapTool.java:363)
	at net.rptools.maptool.client.TransferableHelper.lambda$importData$1(TransferableHelper.java:566)
	at java.desktop/java.awt.event.InvocationEvent.dispatch(Unknown Source)
	at java.desktop/java.awt.EventQueue.dispatchEventImpl(Unknown Source)
	at java.desktop/java.awt.EventQueue$4.run(Unknown Source)
	at java.desktop/java.awt.EventQueue$4.run(Unknown Source)
	at java.base/java.security.AccessController.doPrivileged(Unknown Source)
	at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
	at java.desktop/java.awt.EventQueue.dispatchEvent(Unknown Source)
	at net.rptools.maptool.client.swing.MapToolEventQueue.dispatchEvent(MapToolEventQueue.java:63)
	at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
	at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
	at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
	at java.desktop/java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.desktop/java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.desktop/java.awt.EventDispatchThread.run(Unknown Source)
Caused by: net.rptools.parser.ParserException: com.oracle.truffle.polyglot.PolyglotEngineException error executing expression js.evalURI("com.kwvanderlinde.collision", "lib://com.kwvanderlinde.collision/example.js").
	at net.rptools.maptool.client.MapToolLineParser.parseExpression(MapToolLineParser.java:1124)
	at net.rptools.maptool.client.MapToolLineParser.parseLine(MapToolLineParser.java:857)
	at net.rptools.maptool.client.MapToolLineParser.runMacroBlock(MapToolLineParser.java:1456)
	at net.rptools.maptool.client.MapToolLineParser.runMacro(MapToolLineParser.java:1380)
	at net.rptools.maptool.client.MapToolLineParser.runMacro(MapToolLineParser.java:1177)
	at net.rptools.maptool.model.library.addon.AddOnLibrary.lambda$callMTSFunction$15(AddOnLibrary.java:670)
	... 38 more
Dominant language
Java
Stars
927
Forks
286
Avg merge
3d 17h
Merged PRs (30d)
94

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from RPTools/maptool

All issues in RPTools/maptool

Similar issues

More Java issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.