---
title: "The reviewer says your cache can go stale: find the write that skips it | Proofline"
description: "A stale-cache claim is right when some write changes an input the cached value depends on and that write never reaches the invalidation."
url: https://proofline.sh/learn/ai-review-cache-invalidation-comment/
---

[Learning center](/learn/)

# The reviewer says your cache can go stale: find the write that skips it

A stale-cache claim is right when some write changes an input the cached value depends on and that write never reaches the invalidation. List the inputs, find every path that writes each one, and settle each path with a test that writes, then reads the cache. Decline the claim for a path only when you can show the path cannot occur.

Code review The Proofline team November 22, 2026 4 min read

## Turn the claim into a list of write paths

A cache entry depends on a set of inputs. The invalidation is correct when every write to any of those inputs clears or refreshes the entry. A reviewer that says "this can go stale" is naming one write it thinks the invalidation misses, or one it could not find a test for. Both are checkable in the code.

In ORM code, the writes that most often miss an invalidation are the ones that bypass the hook it listens on. Django's [`QuerySet.update()`](https://docs.djangoproject.com/en/5.2/ref/models/querysets/#update) "does an update at the SQL level and, thus, does not call any `save()` methods on your models, nor does it emit the `pre_save` or `post_save` signals". Bulk inserts, raw SQL, migrations and foreign keys set to `NULL` on delete can do the same. An invalidation wired to `post_save` misses all of them unless something else covers them.

## A missing test, added on request

In May 2026, NetBox opened a pull request that stores each device's and virtual machine's rendered config context, with signal handlers that "invalidate cached data upon any applicable change". A `NULL` value means "invalidated; render on demand". [Pull request](https://github.com/netbox-community/netbox/pull/22294)

Its automated Claude review listed, among other items, "No test for `Cluster.type`/`group` change → VM cache invalidation". It explained that the code "registers a handler for `Cluster` changes", "But there's no test in `test_configcontext_cache.py` that exercises a `Cluster.type` or `Cluster.group` change and verifies the associated VM's cache is invalidated." It raised the same point for a tenant's group. [Claude review](https://github.com/netbox-community/netbox/pull/22294#issuecomment-4623733871)

The maintainer answered item by item. Against both items he wrote "Added tests". [Maintainer's reply](https://github.com/netbox-community/netbox/pull/22294#issuecomment-4623954656) The merged test is the write-then-read shape this claim calls for:

```
def test_direct_upstream_cluster_type_change_invalidates_vm(self):
    ct1 = ClusterType.objects.create(name='CT1', slug='ct1')
    ct2 = ClusterType.objects.create(name='CT2', slug='ct2')
    cluster = Cluster.objects.create(name='Cluster', type=ct1)
    site = Site.objects.create(name='Site', slug='site')
    vm = VirtualMachine.objects.create(name='VM', site=site, role=self.role, cluster=cluster)
    _set_cache(vm, {'cached': True})

    cluster.snapshot()
    cluster.type = ct2
    cluster.save()

    self.assertIsNone(_get_cache(vm))
```

[Merged test file](https://github.com/netbox-community/netbox/blob/288c47d/netbox/extras/tests/test_configcontext_cache.py)

It sets a cached value, writes an upstream input the way the application does, and asserts the cache is empty afterward. The handler already existed, so here the reviewer found a gap in coverage, not a defect. The test is what keeps a later refactor of the handler registry from opening the gap.

## Over-invalidation is a cost, not a stale read

The same review said that reparenting a tenant group triggered invalidation although config contexts match tenant groups by direct equality: "This over-invalidates but doesn't produce stale caches." The maintainer marked it "Fixed". At the merge commit, `TenantGroup` no longer appears in the reparent handlers in `extras/signals.py`, and still appears in the delete handlers. [Merged signals](https://github.com/netbox-community/netbox/blob/288c47d/netbox/extras/signals.py)

The reviewer classified this one correctly itself. Extra invalidation costs renders. Missing invalidation serves wrong data. Weigh the two kinds of claim differently.

The merged file also shows a write path found by reading. A comment there says that deleting an upstream object referenced through a `SET_NULL` foreign key "silently nulls that FK with a bulk UPDATE that emits no post\_save signal", so the code invalidates on `pre_delete` instead.

## A claim declined because the path cannot occur

In August 2026 a follow-up pull request fixed invalidation queries that ran on whichever database Django's routers chose, instead of the one the save used. That left "a stale pre-rendered config context" on deployments with routers. [Pull request](https://github.com/netbox-community/netbox/pull/23028) Its Claude review said: "The alias is resolved too late — reads and writes can still split when `using` is `None`". [Claude review](https://github.com/netbox-community/netbox/pull/23028#issuecomment-5415744986) The maintainer replied: "Only a concern when `using=None`, which does not occur in practice." [Maintainer's reply](https://github.com/netbox-community/netbox/pull/23028#issuecomment-5416106274)

That reply holds for the signal handlers. Django passes every [model signal](https://docs.djangoproject.com/en/5.2/ref/signals/#post-save) a `using` argument, "The database alias being used", and at the merge commit every invalidation call in `extras/signals.py` forwards it. I did not check callers outside that file, such as plugins.

## Decide by write path

| What you find | Decision |
| --- | --- |
| A write changes a cached input and nothing on its path invalidates | Change. Add the invalidation and a write-then-read test for that path |
| The invalidation exists but no test exercises that write | Add the test. The claim is about coverage |
| The write bypasses the hook the invalidation listens on (`update()`, bulk insert, raw SQL, cascade) | Change, or invalidate where that write happens |
| The claim is that something invalidates too much | Dismiss or simplify. It costs work and does not serve stale data |
| The path the reviewer describes cannot be reached from any caller | Dismiss, and list the callers you checked |
| A time-to-live bounds the staleness and the product accepts that window | Dismiss, and cite where the window is agreed |

Write the test against the application's real write, as the NetBox test does with `cluster.save()`, not against the invalidation function. A test that calls the invalidation directly proves the function works and says nothing about whether the write reaches it. When the bot could not have seen the handler registry because it sits outside the diff, the [context guide](/learn/test-reviewer-context-beyond-diff/) covers how to tell a reviewer's blind spot from a defect.

## One month of free code review.

New organizations get one calendar month of hosted code review for private and public GitHub repositories. No card or cloud connection required. [Eligibility and fair-use terms](/free-code-review/).

[Start free with GitHub](https://proofline.sh/orgs/plan?offer=free_month)

## Related reading

-   ### [Does your code reviewer read beyond the diff? Build a test pull request](/learn/test-reviewer-context-beyond-diff/)
    
    Code review The Proofline team November 20, 2026 4 min read
    
-   ### [Trace an accepted code review finding to the lines it changed](/learn/reconstruct-accepted-review-finding/)
    
    Code review The Proofline team November 20, 2026 3 min read
