ADFA-2730 | Fix crash during file tree loading#920
Conversation
📝 WalkthroughRelease Notes: Fix crash during file tree loading (PR
|
| Cohort / File(s) | Summary |
|---|---|
Tree Building Refactor app/src/main/java/com/itsaky/androidide/fragments/sidebar/FileTreeSelectionFragment.kt |
Replaced synchronous traversal with a private suspend fun buildTreeNodes(context: Context, ...); added currentCoroutineContext().ensureActive() checks during iteration; use passed Context for MultiSelectFileTreeViewHolder/node creation; guard setupTreeView with isAdded; updated lambda parameter names and path handling; added imports android.content.Context, kotlinx.coroutines.currentCoroutineContext, kotlinx.coroutines.ensureActive. |
Sequence Diagram(s)
(omitted)
Estimated code review effort
🎯 3 (Moderate) | ⏱️ ~20 minutes
Suggested reviewers
- itsaky-adfa
- jomen-adfa
- dara-abijo-adfa
Poem
🐇 I hopped through folders, light and spry,
Suspending steps beneath the sky,
Context cradled in my tiny paw,
Coroutines humming as I saw,
Nodes aligned—ready to fly ✨
🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 warning)
| Check name | Status | Explanation | Resolution |
|---|---|---|---|
| Docstring Coverage | Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. | Write docstrings for the functions missing them to satisfy the coverage threshold. |
✅ Passed checks (2 passed)
| Check name | Status | Explanation |
|---|---|---|
| Title check | ✅ Passed | The title 'ADFA-2730 | Fix crash during file tree loading' directly and concisely describes the main change: fixing a crash in file tree loading with the ticket reference. |
| Description check | ✅ Passed | The description is comprehensive and directly related to the changeset, explaining the crash, root cause, and three specific fixes implemented in the code. |
✏️ Tip: You can configure your own custom pre-merge checks in the settings.
✨ Finishing touches
- 📝 Generate docstrings
🧪 Generate unit tests (beta)
- Create PR with unit tests
- Post copyable unit tests in a comment
- Commit unit tests in branch
fix/ADFA-2730-file-tree-crash
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.
Comment @coderabbitai help to get the list of available commands and usage tips.
app/src/main/java/com/itsaky/androidide/fragments/sidebar/FileTreeSelectionFragment.kt
Outdated
Show resolved
Hide resolved
c248cbc to
33a211b
Compare
Capture context safely and add cancellation checks in background thread
594fca2 to
b2459d0
Compare
Description
Fixed a
DiagnosticCoroutineContextException(caused byIllegalStateException: Fragment not attached to a context) that occurred when navigating back from the file tree selection screen while files were still loading.The crash happened because
requireContext()was being invoked inside a background coroutine (Dispatchers.IO) after the Fragment had already been detached.I fixed this by:
buildTreeNodesfunction.currentCoroutineContext().ensureActive()inside the loop. This ensures the background operation stops immediately if the user navigates away, preventing the code from attempting to use resources after detachment.Details
Stacktrace addressed:
java.lang.IllegalStateException: Fragment FileTreeSelectionFragment not attached to a context. at androidx.fragment.app.Fragment.requireContext at com.itsaky.androidide.fragments.sidebar.FileTreeSelectionFragment.buildTreeNodesBefore changes
Screen.Recording.2026-02-03.at.12.05.10.PM.mov
After changes
Screen.Recording.2026-02-03.at.5.12.52.PM.mov
Ticket
ADFA-2730
Observation
The addition of
ensureActive()is critical to ensure cooperative cancellation, preventing the background thread from continuing to process files unnecessarily after the screen is closed.