Skip to content

Incorrect Cache invalidation in Nullaway 0.12.12 #1351

Closed

Description

@felixdesouza

I believe #1328 introduces a bug because it doesn't keep its caches in sync.

In an internal repo, we were getting the following error messages when running NullAway 0.12.12:

BugPattern: NullAway
       Stack Trace:
       java.lang.NullPointerException: Cannot invoke "org.checkerframework.nullaway.dataflow.cfg.ControlFlowGraph.getTreeLookup()" because "this.cfg" is null
        at org.checkerframework.nullaway.dataflow.analysis.AbstractAnalysis.getResult(AbstractAnalysis.java:180)
        at com.uber.nullaway.dataflow.DataFlow.resultFor(DataFlow.java:318)
        at com.uber.nullaway.dataflow.DataFlow.resultForExpr(DataFlow.java:296)
        at com.uber.nullaway.dataflow.DataFlow.expressionDataflow(DataFlow.java:241)
        at com.uber.nullaway.dataflow.AccessPathNullnessAnalysis.getNullness(AccessPathNullnessAnalysis.java:115)
        at com.uber.nullaway.NullAway.nullnessFromDataflow(NullAway.java:2746)
        at com.uber.nullaway.NullAway.mayBeNullExpr(NullAway.java:2712)
        at com.uber.nullaway.NullAway.lambda$handleInvocation$1(NullAway.java:2047)
        at com.uber.nullaway.InvocationArguments.forEach(InvocationArguments.java:86)
        at com.uber.nullaway.NullAway.handleInvocation(NullAway.java:2014)
        at com.uber.nullaway.NullAway.matchMethodInvocation(NullAway.java:444)
        at com.google.errorprone.scanner.ErrorProneScanner.processMatchers(ErrorProneScanner.java:509)
        ...

In the following block:

    ControlFlowGraph cfg = cfgCache.getUnchecked(CfgParams.create(path, env));
    AnalysisParams aparams = AnalysisParams.create(transfer, cfg);
    @SuppressWarnings("unchecked")
    Analysis<A, S, T> analysis = (Analysis<A, S, T>) analysisCache.getUnchecked(aparams);
    if (performAnalysis && !alreadyRunAnalyses.contains(aparams)) {
      analysis.performAnalysis(cfg);
      alreadyRunAnalyses.add(aparams);
    }

What I think happens is that:

  • Dataflow#analysisCache will evict an analysis, but it won't clear it from alreadyRunAnalyses
  • When we see the same path again, we will create new analyses, but because the aparams completed before at some point, the if condition will fail as it already contains the particular aparams.
  • It will therefore not run analysis.performAnalysis(cfg) which initialises the fields we care about cfg
  • The analysis you get back will be malformed and you will get the NullPointerException.

The fix is probably along the lines of adding a removal listener to analysisCache that will also remove the key from aparams, keeping the two caches in sync.

What was a bit confusing to me was that it was non-deterministic in that some builds would pass, others would fail. However, when it failed, it would consistently fail at the same point. So perhaps it's some sort of ordering issue when Errorprone/Nullaway is processing the files. This has made it a bit difficult to get a reliable reproduction, but I believe the above steps is the only way to get a malformed Analysis object back.

I'm new to the NullAway repository, so I'm not sure about all the different lifecycles, but how I think this manifests is when you have a
class with lots of different method invocations, but you return to one previously handled

Activity

  1. msridhar commented on Nov 25, 2025

    @msridhar
    Contributor

    Thanks for this report! Your explanation makes a lot of sense. I did do a further change to this code in #1334 that might fix the issue you are seeing. I don't know how easy it is for you to try to make a local snapshot build (instructions here) and use that to test. I'd like to think about this a bit more, though, and try to make sure we have a more ironclad guarantee that these caches are consistent.

  2. felixdesouza commented on Nov 26, 2025

    @felixdesouza
    Author

    I think that may do it?

    My thought - if you're open to it - would be to forego the two caches, and just have an analysis that's keeps track of whether it's been performed or not.

    What if you have a MemoizingAnalysis - please pick a better name 😅 - where it extends Analysis, and takes in the analysis as a delegate.

    • Multiple calls to performAnalysis will only return the first result => gets us the desired behaviour.
    • Don't need to keep multiple caches in sync. If the first analysis is evicted, we'll need to perform the analysis again to get results.
    • I don't know too well how this is consumed so this could be moot, but you could potentially add validation on calling getResult when you haven't done any analysis.

    I initially had an idea for having a type like CompletableAnalysis that didn't extend Analysis but would keep track of whether the analysis has been performed yet, but you'd need to return the underlying analysis, so you could potentially get out of sync that way.

  3. msridhar commented on Nov 26, 2025

    @msridhar
    Contributor

    That's a great idea! I gave it a shot in #1353. It's running through benchmarking / CI now; let's see how it goes 🙂

  4. felixdesouza commented on Nov 26, 2025

    @felixdesouza
    Author

    Hello again!

    I built a snapshot of #1353 and ran a build 10 times in a row for one of the failing repos. 100% pass rate 🎉 .

    Ran a similar test (10 in a row) but for 0.12.12 and 40% builds failed and similarly all at the same point. 😬

    Mostly for my curiosity, I'd love to know where the non-determinism comes from here, I wouldn't have thought gradle would be calling the compiler with an arbitrary order of source files but idk.

  5. msridhar commented on Nov 26, 2025

    @msridhar
    Contributor

    Thanks for confirming! We'll try to get the fix landed and a new release cut soon. And yeah I'm not sure about the non-determinism; would be good to track down but might take some work.

  6. added a commit that references this issue on Nov 26, 2025
    7cd8b1b
  7. felixdesouza commented on Nov 28, 2025

    @felixdesouza
    Author

    Thanks for merging this! Do you have a rough idea of when you might cut a release?

  8. msridhar commented on Nov 28, 2025

    @msridhar
    Contributor

    We're aiming to cut a release early next week @felixdesouza. Thanks!

  9. msridhar commented on Dec 1, 2025

    @msridhar
    Contributor

    @felixdesouza NullAway 0.12.13 is released with this fix

  10. felixdesouza commented on Dec 1, 2025

    @felixdesouza
    Author

    fantastic! thanks so much!

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions