Elffuss BraidHub

kikocis/continue public

fix: dispose JCEF JSQuery handler correctly

The JCEF browser wasn’t registered as a Disposable when held by the service. Service was disposed, but we had leak, because this caused JSQuery handlers to run on a disposed browser (browsers are disposed automatically by default according to docs), throwing exceptions when opening another project.

This fix registers both the browser and the handler properly. I tested it manually, and opening multiple projects no longer causes errors.

Note: I think it would be useful to have an e2e test at some stage that opens a project, closes it, and then opens another one. Such a test should catch this issue early.
Changeseta8496f39d35876df9821fc07c2167d94a5767614e1545d9653be3cfeab12e07d
AuthorJakub Blach <jakubblach@gmail.com> · 2025-09-04 17:21 +02:00
CommitterJakub Blach <jakubblach@gmail.com> · 2025-09-05 21:07 +02:00
Parents80ec3503f561

Browse the files at this changeset

2 files changed

modified extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/browser/ContinueBrowser.kt +9 −3
@@ -1,28 +1,29 @@
11 package com.github.continuedev.continueintellijextension.browser
22
33 import com.github.continuedev.continueintellijextension.constants.MessageTypes
4-import com.github.continuedev.continueintellijextension.services.ContinueExtensionSettings
54 import com.github.continuedev.continueintellijextension.services.ContinuePluginService
65 import com.github.continuedev.continueintellijextension.utils.uuid
76 import com.google.gson.Gson
7+import com.intellij.openapi.Disposable
88 import com.intellij.openapi.components.service
99 import com.intellij.openapi.diagnostic.Logger
1010 import com.intellij.openapi.project.Project
11+import com.intellij.openapi.util.Disposer
1112 import com.intellij.ui.jcef.*
1213 import org.cef.CefApp
1314 import org.cef.browser.CefBrowser
1415 import org.cef.handler.CefLoadHandlerAdapter
1516 import javax.swing.JComponent
1617
17-class ContinueBrowser(private val project: Project) {
18+class ContinueBrowser(private val project: Project): Disposable {
1819
1920 private val log = Logger.getInstance(ContinueBrowser::class.java.simpleName)
2021 private val browser: JBCefBrowser = JBCefBrowser.createBuilder().setOffScreenRendering(true).build()
22+ private val myJSQueryOpenInBrowser = JBCefJSQuery.create(browser as JBCefBrowserBase)
2123
2224 init {
2325 CefApp.getInstance().registerSchemeHandlerFactory("http", "continue", CustomSchemeHandlerFactory())
2426 browser.jbCefClient.setProperty(JBCefClient.Properties.JS_QUERY_POOL_SIZE, 200)
25- val myJSQueryOpenInBrowser = JBCefJSQuery.create(browser as JBCefBrowserBase)
2627 myJSQueryOpenInBrowser.addHandler { msg: String? ->
2728 val json = Gson().fromJson(msg, BrowserMessage::class.java)
2829 val messageType = json.messageType
@@ -100,6 +101,11 @@
100101 browser.cefBrowser.executeJavaScript(script, getGuiUrl(), 0)
101102 }
102103
104+ override fun dispose() {
105+ Disposer.dispose(myJSQueryOpenInBrowser)
106+ Disposer.dispose(browser)
107+ }
108+
103109 // todo: remove and use types.Message
104110 private data class BrowserMessage(
105111 val messageType: String,
modified extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/browser/ContinueBrowserService.kt +13 −3
@@ -1,22 +1,32 @@
11 package com.github.continuedev.continueintellijextension.browser
22
3+import com.intellij.openapi.Disposable
34 import com.intellij.openapi.components.Service
45 import com.intellij.openapi.components.service
56 import com.intellij.openapi.project.Project
7+import com.intellij.openapi.util.Disposer
68 import com.intellij.ui.jcef.JBCefApp
79
810 @Service(Service.Level.PROJECT)
9-class ContinueBrowserService(project: Project) {
11+class ContinueBrowserService(project: Project): Disposable {
1012
1113 private val browser: ContinueBrowser? =
1214 if (JBCefApp.isSupported())
1315 ContinueBrowser(project)
1416 else null
1517
18+ override fun dispose() {
19+ if (browser != null)
20+ Disposer.dispose(browser)
21+ }
22+
1623 companion object {
1724
18- fun Project.getBrowser() =
19- service<ContinueBrowserService>().browser
25+ fun Project.getBrowser(): ContinueBrowser? {
26+ if (isDisposed)
27+ return null
28+ return service<ContinueBrowserService>().browser
29+ }
2030
2131 }
2232 }