Skip to content

ADFA-2730 | Fix crash during file tree loading#920

Merged
jatezzz merged 2 commits intostagefrom
fix/ADFA-2730-file-tree-crash
Feb 4, 2026
Merged

ADFA-2730 | Fix crash during file tree loading#920
jatezzz merged 2 commits intostagefrom
fix/ADFA-2730-file-tree-crash

Conversation

@jatezzz
Copy link
Collaborator

@jatezzz jatezzz commented Feb 3, 2026

Description

Fixed a DiagnosticCoroutineContextException (caused by IllegalStateException: 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:

  1. Capturing the Context safely in the main thread before the background operation starts.
  2. Passing this context as a parameter to the recursive buildTreeNodes function.
  3. Adding 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.buildTreeNodes

Before 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.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Feb 3, 2026

📝 Walkthrough

Release Notes: Fix crash during file tree loading (PR #920)

  • Fixed crash caused by DiagnosticCoroutineContextException ("Fragment not attached to a context") when navigating back from the file-tree selection screen while files were still loading.
  • Captures Context on the main thread before starting background processing and passes it into the tree-building routine.
  • Converted tree construction to a suspending function (buildTreeNodes) that runs on a background dispatcher and accepts an explicit Context parameter.
  • Added cooperative cancellation by calling currentCoroutineContext().ensureActive() inside the file-processing loop so traversal stops immediately when the coroutine is cancelled.
  • Guarded UI updates with isAdded (fragment-attached) checks before calling setupTreeView and other fragment-bound operations.
  • Adjusted selected-files handling and path computations to use explicit parameters and target File instances; switched traversal to use target.walkTopDown for clarity.

⚠️ Risks & Best-practice concerns

  • File I/O lacks explicit try/catch: permission issues or IO errors (e.g., listFiles() returning null) may still cause failures or incomplete trees.
  • Recursion depth is unbounded: extremely deep directory hierarchies could cause stack-overflow.
  • No limits on processed files/memory: very large repositories (thousands of files) may still consume excessive memory or impact UI rendering performance.
  • Silent handling of null listFiles(): skipped directories are not logged or surfaced to the user.
  • Passing Context objects across threads increases risk if the Context is an Activity; ensure the Context used is appropriate (application Context when long-lived references are needed).

Testing recommendations

  • Rapidly navigate away while file tree is loading to verify cooperative cancellation.
  • Test with very large (10k+) and deeply nested (>500 levels) directory structures.
  • Test with directories having restricted permissions and verify failures are handled gracefully.

Walkthrough

Refactors fragment tree construction to a suspending, context-aware builder that checks coroutine cancellation, passes an explicit Android Context to node/view-holder creation, and guards UI setup behind fragment attachment state.

Changes

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 ⚠️ Warning 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.

❤️ Share

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

@jatezzz jatezzz force-pushed the fix/ADFA-2730-file-tree-crash branch from c248cbc to 33a211b Compare February 4, 2026 13:48
@jatezzz jatezzz force-pushed the fix/ADFA-2730-file-tree-crash branch from 594fca2 to b2459d0 Compare February 4, 2026 16:02
@jatezzz jatezzz merged commit ed2aefa into stage Feb 4, 2026
2 checks passed
@jatezzz jatezzz deleted the fix/ADFA-2730-file-tree-crash branch February 4, 2026 16:15
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