|
27 | 27 | import java.util.Map; |
28 | 28 | import java.util.Optional; |
29 | 29 |
|
30 | | -import org.graalvm.polyglot.Context; |
31 | 30 | import org.restheart.polyglot.PolyglotClassloaderHelper; |
32 | 31 | import org.restheart.polyglot.PolyglotThreadUtils; |
33 | 32 | import org.graalvm.polyglot.Engine; |
@@ -68,7 +67,7 @@ public JSInterceptorFactory(Optional<MongoClient> mclient, Configuration config) |
68 | 67 | try { |
69 | 68 | // Engine.create() touches Truffle thread locals, must run on a platform thread (see PolyglotThreadUtils) |
70 | 69 | // and needs PluginsClassloader so ServiceLoader can find js-language's TruffleLanguageProvider |
71 | | - this.engine = PolyglotThreadUtils.onPlatformThread(() -> PolyglotClassloaderHelper.withPluginsClassloaderResult(Engine::create)); |
| 70 | + this.engine = PolyglotThreadUtils.onPlatformThread(PolyglotThreadUtils::createEngine); |
72 | 71 | } catch (Exception e) { |
73 | 72 | throw new IllegalStateException("Error creating polyglot Engine", e); |
74 | 73 | } |
@@ -108,7 +107,8 @@ public JSInterceptorFactory(Optional<MongoClient> mclient, Configuration config) |
108 | 107 | var sindexPath = pluginPath.toUri().toString(); |
109 | 108 | LOGGER.debug("Resolved interceptor path: {}", sindexPath); |
110 | 109 |
|
111 | | - try (Context ctx = ContextQueue.newContext(engine, "foo", config, LOGGER, mclient, "", contextOptions)) { |
| 110 | + var ctx = ContextQueue.newContext(engine, "foo", config, LOGGER, mclient, "", contextOptions); |
| 111 | + try { |
112 | 112 |
|
113 | 113 | // ******** evaluate and check options |
114 | 114 | var optionsScript = "import { options } from '" + sindexPath + "'; options;"; |
@@ -352,6 +352,16 @@ public JSInterceptorFactory(Optional<MongoClient> mclient, Configuration config) |
352 | 352 | interceptor.getClass().getName(), |
353 | 353 | interceptor, |
354 | 354 | new HashMap<>()); |
| 355 | + } finally { |
| 356 | + try { |
| 357 | + // Context.close() touches thread locals, must run on a platform thread, see PolyglotThreadUtils |
| 358 | + PolyglotThreadUtils.onPlatformThread(() -> { |
| 359 | + ctx.close(); |
| 360 | + return null; |
| 361 | + }); |
| 362 | + } catch (Exception e) { |
| 363 | + LOGGER.warn("Error closing context for {}", pluginPath, e); |
| 364 | + } |
355 | 365 | } |
356 | 366 | } |
357 | 367 |
|
|
0 commit comments