Skip to content

Conversation

@catrielmuller
Copy link
Contributor

Context

This PR addresses critical performance and stability issues in the JetBrains plugin, specifically targeting webview flickering and thread management problems that were causing UI freezes and resource leaks. The changes implement a comprehensive solution across multiple phases to prevent thread leaks, improve UI responsiveness, and enhance overall plugin stability.

The main issues addressed:

  • Webview flickering during rendering causing poor user experience
  • Thread leaks from improper coroutine scope management
  • UI freezes due to blocking operations on the EDT (Event Dispatch Thread)
  • Resource leaks from undisposed monitoring infrastructure
  • Race conditions in concurrent scope operations

Implementation

Phase 1: Core Thread Management Improvements

WebViewInstance.kt

  • Replaced Timer with IntelliJ's Alarm API for better thread management and automatic cleanup
  • Implemented proper coroutine scope lifecycle with SupervisorJob for fault isolation
  • Added bounded thread pool (boundedIODispatcher) to prevent thread explosion
  • Moved EDT operations outside synchronized blocks to prevent deadlocks
  • Added proper disposal of coroutine scopes and thread pools in dispose()

Key changes:

  • Synchronized blocks now only handle flag checks, not callback execution
  • Page load callbacks execute on EDT using invokeLater to avoid blocking
  • JavaScript execution retries use Alarm instead of Timer for proper lifecycle management
  • Scope registry integration for monitoring active coroutine scopes

Phase 2: Monitoring Infrastructure

New monitoring classes:

These tools provide visibility into resource usage and help identify leaks during development and testing.

Phase 3: Configuration and Tuning

PerformanceSettings.kt

  • Added configurable debounce settings for file events, editor activation, and editor edits
  • Configurable RPC concurrency limits to prevent overwhelming the system
  • Default values balanced for responsiveness and resource usage

Phase 4: VSCode DOM Patches

deps/patches/vscode/jetbrains.patch

  • Updated VSCode DOM implementation to fix webview rendering issues
  • Improved event handling to prevent flickering
  • Better synchronization between webview state and UI updates

Phase 5: Build System Integration

package.json & turbo.json

  • Added jetbrains-host:build task for building the JetBrains host component
  • Proper dependency management in Turbo configuration

Testing

Comprehensive test coverage added:

Tradeoffs

  • Memory vs Thread Count: Bounded thread pools use slightly more memory but prevent unbounded thread growth
  • Responsiveness vs Resource Usage: Debounce settings balance UI responsiveness with system load
  • Monitoring Overhead: Monitoring infrastructure adds minimal overhead but provides valuable debugging information

How to Test

Testing Webview Stability

  1. Open a JetBrains IDE with the Kilo Code plugin installed
  2. Open the Kilo Code chat panel (webview)
  3. Interact with the chat interface (send messages, scroll, resize)
  4. Expected: No flickering or visual artifacts during rendering
  5. Expected: Smooth UI updates without freezes

Testing Thread Leak Prevention

  1. Enable thread monitoring by setting log level to DEBUG
  2. Open and close the Kilo Code panel multiple times (20+ iterations)
  3. Monitor thread count in IDE logs or using a profiler
  4. Expected: Thread count should stabilize and not grow unbounded
  5. Expected: No "thread leak detected" warnings in logs

Testing Resource Cleanup

  1. Open the Kilo Code panel
  2. Perform various operations (file edits, command execution, etc.)
  3. Close the panel
  4. Check IDE logs for disposal messages
  5. Expected: All resources properly disposed (check for "WebView instance released" messages)
  6. Expected: No resource leak warnings

Testing Under Load

  1. Open multiple project windows with Kilo Code enabled
  2. Perform rapid operations in each window (file edits, panel toggles)
  3. Monitor system resources (CPU, memory, thread count)
  4. Expected: System remains responsive
  5. Expected: Thread count stays within reasonable bounds (< 100 growth)
  6. Expected: No UI freezes or deadlocks

Running Tests

# Run all JetBrains plugin tests
cd jetbrains/plugin
./gradlew test

# Run specific test suites
./gradlew test --tests "ThreadLeakPreventionTest"
./gradlew test --tests "PerformanceSettingsTest"

Monitoring During Development

Enable debug logging to see monitoring output:

// In your IDE's log settings, set these to DEBUG level:
ai.kilocode.jetbrains.monitoring
ai.kilocode.jetbrains.webview

This will show:

  • Thread count changes
  • Scope lifecycle events
  • Resource allocation/disposal
  • Performance metrics

@changeset-bot
Copy link

changeset-bot bot commented Jan 19, 2026

🦋 Changeset detected

Latest commit: 6c7a69b

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
kilo-code Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@catrielmuller catrielmuller requested a review from Copilot January 19, 2026 13:14
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR implements comprehensive performance and stability improvements for the JetBrains plugin, addressing critical issues including webview flickering, thread leaks, UI freezes, and resource management problems. The changes span multiple layers including thread management infrastructure, monitoring tools, configuration, and core plugin components.

Changes:

  • Replaced Timer with IntelliJ's Alarm API for better lifecycle management and automatic cleanup
  • Implemented bounded thread pools to prevent unbounded thread growth
  • Added comprehensive monitoring infrastructure (ThreadMonitor, ScopeRegistry, DisposableTracker)
  • Introduced configurable performance settings with debouncing for file events, editor activation, and edits
  • Applied extensive VSCode DOM patches to fix webview rendering issues
  • Moved blocking EDT operations outside synchronized blocks to prevent deadlocks
  • Added proper coroutine scope lifecycle management with SupervisorJob for fault isolation

Reviewed changes

Copilot reviewed 22 out of 23 changed files in this pull request and generated no comments.

Show a summary per file
File Description
turbo.json Added jetbrains-host:build task for build system integration
package.json Added jetbrains-host:build npm script
ThreadMonitorTest.kt Tests for thread monitoring functionality and resource cleanup
ScopeRegistryTest.kt Tests for coroutine scope lifecycle tracking
DisposableTrackerTest.kt Tests for disposable resource tracking and cleanup
ThreadLeakPreventionTest.kt Integration tests validating thread leak prevention
PerformanceSettingsTest.kt Tests for performance configuration values
ThreadMonitor.kt New monitoring class for tracking thread count and detecting leaks
ScopeRegistry.kt New registry for tracking active coroutine scopes
DisposableTracker.kt New tracker for monitoring disposable resources
PerformanceSettings.kt Configurable performance settings for debouncing and concurrency
WebViewManager.kt Major refactoring with Alarm API, bounded thread pools, and improved rendering configuration
RooToolWindowFactory.kt Replaced Timer with Alarm, optimized status updates, batched UI operations
TerminalInstance.kt Added monitoring integration, removed SPDX headers
WecoderPlugin.kt Added bounded IO dispatcher and thread monitoring
EditorHolder.kt Implemented debounced event channels for activation and edits
EditorAndDocManager.kt Added debounced file event processing
ExtensionSocketServer.kt Improved client thread cleanup and lifecycle management
RPCProtocol.kt Added semaphore for RPC concurrency control
FileRPCProtocolLogger.kt Added monitoring imports (minimal change)
GitCommitMessageAction.kt Removed SPDX header and kilocode_change marker
jetbrains.patch Extensive VSCode DOM implementation updates for rendering fixes
brown-regions-start.md Changeset documenting the fixes

@kiloconnect
Copy link
Contributor

kiloconnect bot commented Jan 19, 2026

Code Review Summary

Status: No Issues Found | Recommendation: Merge

This PR introduces thread leak prevention and performance improvements for the JetBrains plugin. The implementation follows good practices:

  • Thread-safe collections: Uses ConcurrentHashMap for ScopeRegistry and DisposableTracker
  • Proper resource cleanup: disposeAll() in DisposableTracker handles exceptions gracefully with try-finally
  • Safe casts: WebViewInstance uses safe cast (as?) for dispatcher shutdown
  • Comprehensive test coverage: Unit tests for all new monitoring classes plus integration tests for thread leak prevention
  • Configurable settings: PerformanceSettings allows tuning debounce values and RPC concurrency limits
Files Reviewed (14 files)
  • .changeset/brown-regions-start.md - Changeset
  • deps/patches/vscode/jetbrains.patch - VSCode patch updates (formatting changes)
  • jetbrains/plugin/src/main/kotlin/ai/kilocode/jetbrains/config/PerformanceSettings.kt - New performance settings
  • jetbrains/plugin/src/main/kotlin/ai/kilocode/jetbrains/monitoring/ScopeRegistry.kt - Coroutine scope tracking
  • jetbrains/plugin/src/main/kotlin/ai/kilocode/jetbrains/monitoring/ThreadMonitor.kt - Thread monitoring
  • jetbrains/plugin/src/main/kotlin/ai/kilocode/jetbrains/monitoring/DisposableTracker.kt - Disposable resource tracking
  • jetbrains/plugin/src/main/kotlin/ai/kilocode/jetbrains/webview/WebViewInstance.kt - WebView improvements
  • jetbrains/plugin/src/test/kotlin/ai/kilocode/jetbrains/config/PerformanceSettingsTest.kt - Tests
  • jetbrains/plugin/src/test/kotlin/ai/kilocode/jetbrains/monitoring/ScopeRegistryTest.kt - Tests
  • jetbrains/plugin/src/test/kotlin/ai/kilocode/jetbrains/monitoring/ThreadMonitorTest.kt - Tests
  • jetbrains/plugin/src/test/kotlin/ai/kilocode/jetbrains/monitoring/DisposableTrackerTest.kt - Tests
  • jetbrains/plugin/src/test/kotlin/ai/kilocode/jetbrains/integration/ThreadLeakPreventionTest.kt - Integration tests
  • package.json - Build script addition
  • turbo.json - Turbo task configuration

@@ -1,9 +1,6 @@
// SPDX-FileCopyrightText: 2025 Weibo, Inc.
//
// SPDX-License-Identifier: Apache-2.0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know why this was here, removing it is no issue?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've been removing headers from the files we've modified because LLM models tend to add this header to new files as well.

// Version tracking for theme injection
const THEME_VERSION = Date.now();

// Enhanced idempotency check - prevent injection if less than 100ms has passed
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any reason why those cant change within 100ms? Feels a bit random, cant we do actual idempotency somehow?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is to prevent flickering when the plugin browser windows it's out of focus.

Copy link
Contributor

@markijbema markijbema left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems sensible, but concurrency stuff is hard to review for correctness; but I assumed you've tested this well enough.

I've asked some questions, but feel free to merge

@catrielmuller catrielmuller merged commit 443a1b9 into main Jan 19, 2026
12 checks passed
@catrielmuller catrielmuller deleted the catrielmuller/jetbrains-threads-fix branch January 19, 2026 15:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

3 participants