Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TestCoroutineScheduler leaks canceled delayed jobs #3398

Closed
pyricau opened this issue Aug 9, 2022 · 3 comments
Closed

TestCoroutineScheduler leaks canceled delayed jobs #3398

pyricau opened this issue Aug 9, 2022 · 3 comments

Comments

@pyricau
Copy link

pyricau commented Aug 9, 2022

Jobs that are cancelled are expected to become garbage collectable. Unfortunately, TestCoroutineScheduler does not listen to any job cancellation signal. TestCoroutineScheduler keeps cancelled jobs in its queue (TestCoroutineScheduler#events) until the current time of TestCoroutineScheduler is advanced past the original planned time of the job that was cancelled.

I'm providing a unit test that reproduces this issue here: #3399

This is unfortunately a major bug for us at Square / Block, because:

  • TestCoroutineScheduler is used by Compose in its provided test rules
  • Compose clickable element in scrollable containers set their pressed state using a delayed cancellable job (source) and that delayed cancellable job has a transitive strong reference to the backing AndroidComposeView.
  • As a result, if said AndroidComposeView gets detached, the TestCoroutineScheduler will prevent it from being garbage collected until its time is past the clickable timeout. In our UI tests, automatic leak detection triggers in between (as the view gets detached) and fails the test.
  • So basically we ended up having to disable leak detection in all our Compose tests due to this TestCoroutineScheduler bug.

If we reach into TestCoroutineScheduler#events using reflection to clear all canceled jobs from the queue, the jobs do become garbage collectable and we can keep leak detection enabled in our Compose test (which is a huge deal for us)

We initially filed this as a Compose bug here: https://issuetracker.google.com/issues/238277402

@qwwdfsad
Copy link
Member

Thanks for the detailed report!
@dkhalanskyjb who is the main maintainer of the test module is on vacation right now, so delayed (he-he) answer here is expected

@dkhalanskyjb
Copy link
Collaborator

Thank you! Yes, this is a bug in the test module. The fix, as is common, a single line of code.

@pyricau
Copy link
Author

pyricau commented Sep 13, 2022

Nice fix, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants