Skip to content

ADFA-4328: Fix ClosedReceiveChannelException on debounced-action cancel#1409

Open
fryanpan wants to merge 3 commits into
stagefrom
ADFA-4328-keyeddebouncing-channel-closed
Open

ADFA-4328: Fix ClosedReceiveChannelException on debounced-action cancel#1409
fryanpan wants to merge 3 commits into
stagefrom
ADFA-4328-keyeddebouncing-channel-closed

Conversation

@fryanpan

@fryanpan fryanpan commented Jun 18, 2026

Copy link
Copy Markdown
Contributor

Jira Ticket: https://appdevforall.atlassian.net/browse/ADFA-4328
Sentry Issue: https://appdevforall-inc-9p.sentry.io/issues/APPDEVFORALL-14C

Reproduction Details

KeyedDebouncingAction.ActionEntry.cancel() closed the (CONFLATED) channel before cancelling the worker job. A worker parked on channel.receive() is woken by the close with a ClosedReceiveChannelException — which is not a CancellationException, so it escapes uncaught to the worker's CoroutineScope and crashes the app.

Stack Trace

ClosedReceiveChannelException: Channel was closed
  at kotlinx.coroutines.channels.BufferedChannel.getReceiveException(...)
  at kotlinx.coroutines.channels.BufferedChannel.resumeWaiterOnClosedChannel(...)
  at kotlinx.coroutines.channels.BufferedChannel.cancelSuspendedReceiveRequests(...)
  at kotlinx.coroutines.channels.BufferedChannel.close(...)
  at com.itsaky.androidide.utils.KeyedDebouncingAction$ActionEntry.cancel(SourceFile:4)
  at com.itsaky.androidide.utils.KeyedDebouncingAction.cancelPending(SourceFile:15)
  at com.itsaky.androidide.lsp.kotlin.compiler.CompilationEnvironment.onFileClosed(SourceFile:7)
  at com.itsaky.androidide.lsp.kotlin.KotlinLanguageServer.onDocumentClose(SourceFile:28)
  ... (EventBus AsyncPoster → ThreadPoolExecutor worker) ...

User Steps

User steps leading up to crash, based on Sentry breadcrumbs:

  • User is editing code; Kotlin completion + classpath indexing (createSearchScopes) debounce workers are active.
  • A document is closed / the app is backgrounded → KotlinLanguageServer.onDocumentCloseCompilationEnvironment.onFileClosedcancelPending cancels the debounced action while a worker is parked on receive() → uncaught ClosedReceiveChannelException.

Was able to reproduce in a unit test?

Yes.
KeyedDebouncingActionCancelTest (:common) starts a worker, parks it on receive(), then cancels and asserts nothing escapes to a recording CoroutineExceptionHandler. Baseline (close-then-cancel): FAILS with ClosedReceiveChannelException. Branch: passes.

What Was Fixed

In cancel(), cancel the job first, then close the channel; wrap the worker loop in try { … } catch (ClosedReceiveChannelException) { break } catch (CancellationException) { throw e }. CompilationEnvironment also adds a defensive CoroutineExceptionHandler that logs non-cancellation failures.

Testing

:common:testV8DebugUnitTest → green (red on baseline, green on fix, single worktree). Local CodeRabbit review: no findings.

Reviewer note (from local review): the defensive CoroutineExceptionHandler now also logs-and-swallows any other exception thrown by an action body (not just channel-close); confirm that's intended. Consider runTest/virtual time to make the repro timing-deterministic.


Fixes APPDEVFORALL-14C

@fryanpan fryanpan marked this pull request as ready for review June 19, 2026 11:33
@coderabbitai

coderabbitai Bot commented Jun 19, 2026

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

@fryanpan, we couldn't start this review because you've reached your PR review rate limit.

More reviews will be available in 27 minutes and 36 seconds. Learn how PR review limits work.

Your organization has used up its prepaid credits, and credit purchases are no longer available. Enable the review add-on in the billing tab to keep reviews running — you're only billed for reviews past your plan's rate limits ($0.25/file).

⌛ How to resolve this issue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based credits.

🚦 How do rate limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan refill rate.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, the refill rate gradually slows as usage increases. The highest same-day bursts are limited more strictly.

Please see our Fair Usage Limits Policy for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 80ea9384-c60b-4bd1-ba7e-7f7d95cba1ef

📥 Commits

Reviewing files that changed from the base of the PR and between d73ec0b and 5a39efa.

📒 Files selected for processing (4)
  • common/build.gradle.kts
  • common/src/main/java/com/itsaky/androidide/utils/KeyedDebouncingAction.kt
  • common/src/test/java/com/itsaky/androidide/utils/KeyedDebouncingActionCancelTest.kt
  • lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/compiler/CompilationEnvironment.kt
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch ADFA-4328-keyeddebouncing-channel-closed

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

fryanpan and others added 3 commits June 19, 2026 09:58
Sentry APPDEVFORALL-14C. Cancel job before closing channel, use
receiveCatching, add a CoroutineExceptionHandler to the LSP scope.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
…-close crash

Adds a :common unit test that parks a debounce worker on channel.receive(),
cancels the entry, and asserts no ClosedReceiveChannelException escapes to the
scope CoroutineExceptionHandler. Red on the pre-fix baseline (cancel() closed
the channel before cancelling the job), green with the fix.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@fryanpan fryanpan force-pushed the ADFA-4328-keyeddebouncing-channel-closed branch from 623aec8 to 5a39efa Compare June 19, 2026 16:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants