Skip to content

Commit

Permalink
Fix local service lifecycle management
Browse files Browse the repository at this point in the history
  • Loading branch information
pplam committed Sep 12, 2024
1 parent 52973a3 commit a557480
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions src/main/kotlin/ai/devchat/plugin/DevChatToolWindow.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import javax.swing.SwingConstants
class DevChatToolWindow : ToolWindowFactory, DumbAware, Disposable {
private var ideService: IDEServer? = null
private var localService: LocalService? = null
private val coroutineScope = CoroutineScope(Dispatchers.Default)
private var coroutineScope: CoroutineScope? = null


override fun createToolWindowContent(project: Project, toolWindow: ToolWindow) {
Expand All @@ -40,17 +40,28 @@ class DevChatToolWindow : ToolWindowFactory, DumbAware, Disposable {
toolWindow.contentManager.addContent(content)
DevChatSetupThread().start()
ideService = IDEServer(project).start()
coroutineScope.launch {
while (!pythonReady) { delay(100) }
localService = LocalService().start()
val coroutineExceptionHandler = CoroutineExceptionHandler { _, exception ->
Log.error("Failed to start local service: ${exception.message}")
}
coroutineScope = CoroutineScope(Dispatchers.Default)
coroutineScope!!.launch(coroutineExceptionHandler) {
try {
while (!pythonReady) {
delay(100)
ensureActive()
}
localService = LocalService().start()
awaitCancellation()
} finally {
localService?.stop()
}
}
}

override fun dispose() {
DevChatWrapper.activeChannel?.close()
coroutineScope.cancel()
coroutineScope?.cancel()
ideService?.stop()
localService?.stop()
}

companion object {
Expand Down

0 comments on commit a557480

Please sign in to comment.