Skip to content

test(home): stop a Home test's mock request from outliving the test - #575

Merged
Quick104 merged 1 commit into
mainfrom
fix/562-main-dispatcher-leak
Oct 14, 2026
Merged

Quick104 merged 1 commit into
mainfrom
fix/562-main-dispatcher-leak

Conversation

@Quick104

@Quick104 Quick104 commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

Problem

Closes #562
Related issue: N/A
Validation tasks: none

PersonalListViewModelGenerationTest > aResetSupersededByARefreshDoesNotStrandIsLoading failed intermittently in :shared:testDebugUnitTest. On 2026-10-14 it failed CI and the release workflow's unit-test step five times. That test was the victim. The leak came from HomeViewModelCacheIdentityTest.realtimeSignalsDuringARefreshRunOneMoreRefreshAfterIt.

That test returned as soon as its follow-up request entered MockEngine. MockEngine had no dispatcher, so it answered on a real IO thread, sometimes after @AfterTest had called Dispatchers.resetMain(). The late response finished HomeSectionsV2Api.list()'s withContext(decodeDispatcher) and resumed HomeViewModel on Dispatchers.Main, which no longer existed. kotlinx-coroutines-test then reported the error against the next runTest in the JVM as UncaughtExceptionsBeforeTest.

The CI trace matches this: the uncaught exception ran on DefaultDispatcher-worker-3 @call-context#6086, Ktor's call coroutine, resuming a withContext on an UnconfinedTestDispatcher.

Approach

The three Home clients in HomeViewModelCacheIdentityTest now build MockEngine with MockEngineConfig.dispatcher set to the test's own dispatcher, as HomeScreenV2Test already does. No mocked request can outlive the test on another thread. Production code is unchanged, and no test is retried, skipped, or ignored.

Validation

  • Reproduced before the fix. Running HomeViewModelCacheIdentityTest and PersonalListViewModelGenerationTest together failed 13 of 25 times, with the same exception chain as CI. Failures alternated run to run, which fits Gradle running the previously failed class first.
  • Same pair after the fix: 0 of 25 runs failed.
  • A temporary harness, not committed, ran each Home test through setUp/tearDown, left Main reset for 20 ms, then started an empty runTest. Before the fix, the realtime test leaked into the next runTest in 348 of 600 iterations and the other two Home tests in 0 of 1,200. After the fix, all three leaked in 0 of 600.
  • Full :shared:testDebugUnitTest after the fix: 15 of 15 runs passed, 1,949 tests each, and no Dispatchers.Main errors anywhere in the test output.

Evidence

No user-visible change. Reproduction steps, CI and local traces, and test logs:
Evidence: https://evidence.siloserver.org/r/silo-android/pr-575/

Risks

None identified. The change touches only one test file.

Follow-up, not in this PR: android-shared's MembershipActivationTest also leaves view model work in flight on real threads when tests return. It runs under Robolectric, so Main falls back to the looper and this crash can't happen there. Joining its view model jobs before resetMain() would make it deterministic.

Checklist

  • I read and can explain the complete diff.
  • This pull request addresses one concern.
  • The Evidence section shows every change a user can see, or says there is none.

AI Disclosure

  • Harness: Claude Code (in T3 Code)
  • Tool(s): Claude Code (Bash, file edits, read-only Explore subagent), GitHub CLI (gh), Gradle
  • Model(s): Claude Opus 5.5 (claude-opus-5-5)
  • Involvement: Fully AI-generated, not yet human verified
  • Adversarial review: not run. A read-only subagent checked the other setMain tests for the same leak and found only the follow-up above.

🤖 Generated with Claude Code

Note

Run Home test mock requests on the test dispatcher via shared homeClient factory

Adds a private homeClient factory in HomeViewModelCacheIdentityTest.kt that builds the mock HTTP client with content negotiation and the test instance's dispatcher. Replaces inline HttpClient/MockEngine construction in three tests with the shared factory. Behavioral Change: mock request handlers now run on the runTest dispatcher, so blocked and follow-up requests resume under test control instead of the engine's default dispatcher.

Macroscope summarized 930ef37.

…t dispatcher

realtimeSignalsDuringARefreshRunOneMoreRefreshAfterIt returned as soon as
its follow-up request entered MockEngine. MockEngine had no dispatcher, so it
answered on a real IO thread after tearDown had reset Dispatchers.Main. The
late response resumed HomeViewModel on the missing Main dispatcher, and
kotlinx-coroutines-test reported that against the next runTest in the JVM,
usually PersonalListViewModelGenerationTest.

The file's Home clients now run MockEngine on the test's dispatcher, so no
request outlives the test on another thread.

Closes #562

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 14, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-14T16:32:47.316708Z 930ef37 PR opened
🔒 Security Review ✅ Completed 2026-10-14T16:32:46.442952Z 930ef37 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@silo-kody

silo-kody Bot commented Oct 14, 2026 •

Copy link
Copy Markdown

Silo Kody — review complete

Review finished. Check the inline comments for findings and verify each suggestion against the code and tests.

Reviewing changes in Silo
  • Include the related issue, expected behavior, and validation steps in the PR description.
  • For API changes, describe the effect on Apple and Android clients and Jellyfin compatibility.
  • For plugin changes, identify the affected SDK contract, plugin, and catalog entry.
  • Follow this repository's AGENTS.md and CONTRIBUTING.md.
  • Request another review with @kody start-review in a PR comment.
  • React with 👍 or 👎 to give feedback on individual suggestions.
Review settings
Review Options

The following review options are enabled or disabled:

Options Enabled
Bug ✅
Performance ✅
Security ✅
Business Logic ❌

@coderabbitai

coderabbitai Bot commented Oct 14, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 66528c9c-8af0-4bcb-8f45-c0a0dd4f5227
📥 Commits

Reviewing files that changed from the base of the PR and between e98a043 and 930ef37.

📒 Files selected for processing: 1
  • shared/src/commonTest/kotlin/org/siloserver/silo/viewmodel/HomeViewModelCacheIdentityTest.kt

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.

📝 Walkthrough

The HomeViewModel cache identity tests now use a shared homeClient helper. It configures MockEngine to use the test dispatcher and installs JSON content negotiation.

Changes: Home view model cache identity tests

Layer / File(s) Summary
Configure and use the shared test client
shared/src/commonTest/kotlin/org/siloserver/silo/viewmodel/HomeViewModelCacheIdentityTest.kt
The test file adds homeClient and uses it for three response clients. The existing response data and request sequencing remain unchanged.

Priority: ⬇️ Low

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Other

Suggested reviewers: rhainland

Merge Risk: ⚪ Minimal · up to 930ef

This test-only change does not alter production behavior, and the reported repeated test runs support merging after normal checks.

Pre-merge checks: Passed 5
Passed Passed checks: 5 passed
Check name Status Explanation
Linked Issues check Passed Issue #562 requires the intermittent PersonalListViewModelGenerationTest failure to pass reliably. The PR keeps the Home test mock requests on HomeViewModelCacheIdentityTest's test dispatcher thro…
Out of Scope Changes check Passed The changes are limited to shared/src/commonTest/kotlin/org/siloserver/silo/viewmodel/HomeViewModelCacheIdentityTest.kt. They configure the three Home mock clients to use the test dispatcher and pre…
Docstring Coverage Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff. Analyzed 0 functions across 1…
Description check Passed The description clearly explains the intermittent test failure, identifies the MockEngine dispatcher leak, describes the test-only fix, and reports validation results. It is directly related to the ch…
Title check Passed The title clearly and concisely describes the main change: preventing a Home test's mock request from outliving the test.
✨ Finishing Touches
📝 Generate docstrings
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

@silo-triage silo-triage Bot added bug Something isn't working priority: P2 Limited scope, workaround exists, or polish labels Oct 14, 2026
@Quick104
Quick104 merged commit 98c6fd8 into main Oct 14, 2026
8 checks passed
@Quick104
Quick104 deleted the fix/562-main-dispatcher-leak branch October 14, 2026 16:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working priority: P2 Limited scope, workaround exists, or polish

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[bug] PersonalListViewModelGenerationTest fails intermittently in CI without a Main dispatcher

1 participant