Status Update
Comments
an...@gmail.com <an...@gmail.com> #2
This is an inherently unsafe thing to ask for (which is why we have no built in support for this use case), but to explain why, I'll need to go into a lot of detail with what 'running suspend code when a lifecycle state is at least X' implies.
Namely: What behavior do you want to have if the Lifecycle falls below your chosen Lifecycle.State
while your suspending work is running?
The way the when
APIs worked is that the work was paused - when you hit a suspension point, your code just wouldn't run anymore. This is purposefully not supported anymore (as it could leave things hanging for a very long time if for instance the user hit the Home button).
So the only other options are (ignoring the case when the Lifecycle is DESTROYED
and the whole coroutine scope is cancelled):
1. Let the suspending code continue to run to completion.
This has the benefit that the entire block runs atomically - i.e., it wouldn't be cancelled half way through.
That approach looks like:
lifecycleScope.launch {
// Suspend until you are STARTED
withStarted { }
// Run your code that happens after you become STARTED here
doYourOneTimeWork()
// Note: your code will continue to run even if the Lifecycle falls below STARTED
}
This type of code comes with the assumption that your one time work does not depend on staying above your state, but that's not something a library could know. For example, if you run a FragmentTransaction
after your suspending work, the state might be saved prior to that call happening.
2. Cancel the suspending code and restart it when you come back above that state.
This is exactly the contract for repeatOnLifecycle
- re-running the block every time you go back above the given State. However, there's no way for a library to know if it is safe for you to re-run the entire block or if you need to checkpoint more often.
In the naive case, where you can run the entire block multiple times until it actually completes successfully, this is just tracking a isComplete
flag:
lifecycleScope.launch {
var isComplete = false
repeatOnLifecycle(Lifecycle.State.STARTED) {
if (!isComplete) {
// Do your work here. It will be canceled if the Lifecycle
// goes down while it is running
doYourOneTimeWork()
// Then mark the work as complete
isComplete = true
}
}
}
Of course, the 'it is safe to rerun the entire block if it gets canceled half way through' is a really big assumption.
3. Cancel the suspending code and don't restart it
This is similar to the previous one, but uses a finally
block to set the isComplete
flag no matter if the work was cancelled or not.
lifecycleScope.launch {
var isComplete = false
repeatOnLifecycle(Lifecycle.State.STARTED) {
if (!isComplete) {
try {
// Do your work here. It will be canceled if the Lifecycle
// goes down while it is running
doYourOneTimeWork()
} finally {
// By marking the work as complete in the finally block,
// we never restart the block just leaving it potentially
// half complete.
isComplete = true
}
}
}
}
I'm struggling to find a valid use case where you want to leave the work only partially finished, so I mostly include this for completeness.
I hope this gives a little more background on this problem space and why only we have the APIs we have - ideally, you'd avoid all of these cases entirely by not trying to run one-time suspending code when a lifecycle state is at least X.
ra...@google.com <ra...@google.com>
[Deleted User] <[Deleted User]> #3
Thanks for the detailed response! I think my majority use cases fall into either 1 or 3, i.e. if user initiated an action and then closed the activity/fragment, I would like the action (as a suspend block) to also get cancelled upon lifecycle reaching destroyed. I guess in fact for use case 3, I really just need lifecycleScope.launch
without even needing repeatOnLifecycle
. (In fact, it sounds like launchWhenCreated
acts identically as launch
...)
gh...@google.com <gh...@google.com> #4
In examples 2 and 3 wouldn't repeatOnLifecycle
continue to be executed every time the lifecycle state gets to STARTED
, even after isComplete
has been set to true
? Is there no way to tell the repeatOnLifecycle function that it can stop repeating?
an...@gmail.com <an...@gmail.com> #5
Re launch
to cancel the whole code block if you want. Obviously that only works well if you are doing a naive all or nothing approach and not something where you are check-pointing at multiple points in your suspending block.
tn...@google.com <tn...@google.com> #6
an...@gmail.com <an...@gmail.com> #7
an...@gmail.com <an...@gmail.com> #8
wo...@gmail.com <wo...@gmail.com> #9
Cause it might get confusing for people finding that documentation page and then being told that it's gonna be deprecated.
wo...@gmail.com <wo...@gmail.com> #10
could you clarify exactly what you mean in these?
have a custom lint rule using lintChecks on your app module have a custom lint rule using lintChecks on your library module
By this I mean a declaration in your modules dependency block:
lintChecks project(":features:language_lint")
di...@gmail.com <di...@gmail.com> #11
As opposed to lintPublish
? Or is that not relevant?
wo...@gmail.com <wo...@gmail.com> #12
For me this is for local checks so I'm using lintChecks. Removing lintChecks from the library module and only applying it on the app module fixed the lint crash. However that's not optimal as I want to run the lint checks individually on the library module as well. It would not be interesting if this is really the cause of the issue or if it doesn't fix it for others.
[Deleted User] <[Deleted User]> #13
I do not have any custom lint checks declared but there might be a transient lint check pulled in.
[Deleted User] <[Deleted User]> #14
We have checkDependencies=true
on our app module and apply there custom lint checks. Disabling custom lint checks has not solved the issue, though we have more custom lint checks via external dependencies (for example, timber).
an...@gmail.com <an...@gmail.com> #15
#14, could you check with checkDependencies=false
? It fixes problem on my project. Would be nice to hear more proves.
[Deleted User] <[Deleted User]> #16
#15, I've set checkDependencies = false
, but left our custom lint checks enabled. So far lint finished successfully without issue errors.
an...@gmail.com <an...@gmail.com> #17
In my case, all builds of the >50 were a success after checkDependencies = false
. However, disabling checkDependencies
is not a solution as well. Since all our code is split in small logical modules, there is no point in lint. Too many false-positives and true-negatives.
gh...@google.com <gh...@google.com> #18
I cherry-picked a change to 4.1 Beta 4 which essentially changes this error into a logged warning rather than a hard failure. That does not "fix" the problem, but it at least gets us back to the behavior of 3.6. Can someone confirm that this workaround has worked for them?
Also, I think I know what the root cause is (see notes below). However, it's hard to be sure without a repro scenario. Would anyone be willing to share an open-source project that reproduces this bug? (You can also send the project to
Notes:
-
My current theory is that this bug is related to
.https://issuetracker.google.com/159733104#comment6
I.e., I think Lint should not be reusing the same Kotlin compiler environment to analyze multiple modules, which is what we do whencheckDependencies=true
. -
It is still strange that the failure is nondeterministic. However, one potential reason is that the Kotlin compiler caches many things via
SoftReference
.
an...@gmail.com <an...@gmail.com> #19
Sorry for my long delay. I was tried with 4.1.0-beta05
(newest version to date), and the problem is still here. I added new log in case lines were changed.
Would anyone be willing to share an open-source project that reproduces this bug?
I have only closed source project with this problem. Maybe someone knows some quite large multimodule opensource project I could try setup AGP with lint and try reproduction?
an...@gmail.com <an...@gmail.com> #20
I tried to reproduce with two small multimodule projects, but all builds works flawlessly. Also, I tried to reproduce using checkDependencies
, and 6 other helper modules. 145k CLOC without tests. I suppose there is a good chance problem happen due to the cache.
It is still strange that the failure is nondeterministic.
It is even non-deterministic in CI/docker builds using the same fresh environment and exactly the same code each time. Build cache and daemon is disabled there. Multithreading should be involved as well.
In addition, I found in most cases failed due to the same file
br...@google.com <br...@google.com> #21
gh...@google.com <gh...@google.com> #22
There are some architectural changes coming in Lint that will fix this properly, but they will not make it into 4.2.
However, there is another workaround you can try that will be available starting in AGP 4.2 Beta 4.
Essentially you can tell Lint to "reset" the Kotlin compiler between each module analysis, thereby clearing all caches. I did not make this the default behavior because the effect on performance is unknown/risky.
To enable this behavior, either set the environment variable LINT_DO_NOT_REUSE_UAST_ENV
to "true", or set the JVM system property lint.do.not.reuse.uast.env
to "true". For example, you can do this from the command line:
./gradlew -Dlint.do.not.reuse.uast.env=true :app:lintDebug
Or by putting this in your gradle.properties
file:
systemProp.lint.do.not.reuse.uast.env=true
gh...@google.com <gh...@google.com> #23
Status updates:
-
I'm still hoping someone can confirm whether the workaround in
worked for them in AGP 4.2.comment#22 -
Starting with AGP 7.0-alpha13 the root cause of this issue should be fixed completely due to the new "partial analysis" mode (enabled by default) in which Lint analyzes modules independently and merges results along the way. It fixes the issue because Lint will no longer use the same Kotlin compiler environment to analyze multiple modules (and so the risk of stale caches is gone).
ws...@gmail.com <ws...@gmail.com> #24
I'm still hoping someone can confirm whether the workaround in
worked for them in AGP 4.2. comment#22
This unfortunately did not work for us. We actually see lint crashes after enabling via systemProp.lint.do.not.reuse.uast.env=true
. See the attached log. It's also much slower, as expected.
AGP 4.2 seems to make this error much more frequent compared to 4.1, for some reason.
gh...@google.com <gh...@google.com> #25
Can you file a new bug? You can add a comment here linking to it.
ws...@gmail.com <ws...@gmail.com> #26
Sure. Without that workaround, we get the same
ERROR: package fragment is not found for module:<lint-module> is a module[ModuleDescriptorImpl@5e5e6c61] file:KtFile: LiveViewerViewData.kt
error in this ticket occasionally (1-2% of the time?).
gh...@google.com <gh...@google.com> #27
Oh, I misunderstood. So, using lint.do.not.reuse.uast.env
is what causes the new crash? That is unfortunate, and I will investigate. Let me know if there is a sample project you can share that reproduces the issue.
ws...@gmail.com <ws...@gmail.com> #28
Not sure if you still wanted the new bug, but here it is:
gh...@google.com <gh...@google.com> #29
FYI the issue in
And to confirm, lint.do.not.reuse.uast.env
should no longer be needed at all in AGP 7.0+.
ku...@gmail.com <ku...@gmail.com> #30
Thanks #29 . I am noticing that when this happens, after the error log for
package fragment is not found for module:<lintWithKotlin> is a module
There is another error
java.lang.IllegalArgumentException: Argument for @NotNull parameter 'descriptor' of org/jetbrains/kotlin/codegen/context/CodegenContext.intoPackagePart must not be null
at org.jetbrains.kotlin.codegen.context.CodegenContext.$$$reportNull$$$0(CodegenContext.java)
at org.jetbrains.kotlin.codegen.context.CodegenContext.intoPackagePart(CodegenContext.java)
at org.jetbrains.kotlin.asJava.builder.LightClassDataProviderForClassOrObject$computeLightClassData$1$1.invoke(LightClassDataProvider.kt:51)
at org.jetbrains.kotlin.asJava.builder.LightClassDataProviderForClassOrObject$computeLightClassData$1$1.invoke(LightClassDataProvider.kt:38)
at org.jetbrains.kotlin.asJava.builder.LightClassBuilderKt.buildLightClass(LightClassBuilder.kt:64)
at org.jetbrains.kotlin.asJava.builder.LightClassDataProviderForClassOrObject$computeLightClassData$1.invoke(LightClassDataProvider.kt:47)
at org.jetbrains.kotlin.asJava.builder.LightClassDataProviderForClassOrObject$computeLightClassData$1.invoke(LightClassDataProvider.kt:38)
at org.jetbrains.kotlin.cli.jvm.compiler.CliLightClassGenerationSupport.createDataHolderForClass(CliLightClassGenerationSupport.kt:64)
at org.jetbrains.kotlin.asJava.builder.LightClassDataProviderForClassOrObject.computeLightClassData(LightClassDataProvider.kt:45)
at org.jetbrains.kotlin.asJava.builder.LightClassDataProviderForClassOrObject.compute(LightClassDataProvider.kt:61)
at com.intellij.psi.impl.PsiCachedValueImpl.doCompute(PsiCachedValueImpl.java:54)
at com.intellij.util.CachedValueBase.lambda$getValueWithLock$1(CachedValueBase.java:235)
at com.intellij.openapi.util.RecursionManager$1.doPreventingRecursion(RecursionManager.java:113)
at com.intellij.openapi.util.RecursionManager.doPreventingRecursion(RecursionManager.java:72)
at com.intellij.util.CachedValueBase.getValueWithLock(CachedValueBase.java:236)
at com.intellij.psi.impl.PsiCachedValueImpl.getValue(PsiCachedValueImpl.java:43)
at
Does the fix address both of these?
gh...@google.com <gh...@google.com> #31
package fragment is not found
and Argument for @NotNull parameter...
), the workaround in 4.2 is to put
systemProp.lint.do.not.reuse.uast.env=true
in your gradle.properties
file as described in
gh...@google.com <gh...@google.com> #32
Marking fixed in AGP 7.0.
That is, the lint.do.not.reuse.uast.env
workaround should no longer be needed in AGP 7.0+. And, you should no longer see the following warnings/errors while running Lint:
ERROR: package fragment is not found for module:<lintWithKotlin>...
ERROR: Could not generate LightClass for...
java.lang.IllegalArgumentException: Argument for @NotNull parameter 'descriptor' of org/jetbrains/kotlin/codegen/context/CodegenContext.intoPackagePart must not be null
The issue is fixed due to the new "Lint partial analysis" mode (enabled by default) which creates a standalone UastEnvironment
instance for each module, thereby avoiding stale caches in the Kotlin compiler frontend.
Please comment on the bug if you see any similar errors in AGP 7.0+.
Description
AGP: 4.0.0 Kotlin: 1.3.72 JVM: from docker image: 8u252-jdk-slim
Failure happens in CI builds, which always are clean. Failures are random. Sometimes builds are a success, but more often it is not. Each time random classes failed. For example, in this case, failed this little data class:
I included full stacktrace as file