Re: Race condition in OnHeapHnswGraph

Michael Sokolov <[email protected]> Wed, 14 Jan 2026 18:25:29 -0500
Newsgroups gmane.comp.jakarta.lucene.user
Message-ID <CAGUSZHDYtidgixwKj0-f_zXCtzErfbo+jRL45ruJr1dV9Ygg0A@mail.gmail.com>
yeah this looks silly: do you want to open a PR to fix?

On Mon, Jan 12, 2026 at 6:39=E2=80=AFAM Viliam =C4=8Eurina <viliam.durina@g=
mail.com> wrote:
>
> Hi all,
>
> I'm looking at `OnHeapHnswGraph` code and noticed that the volatile field
> `graphRamBytesUsed` is modified in `addNode` in a racy way:
>
>   long bytesUsed =3D graphRamBytesUsed;
>   graphRamBytesUsed =3D bytesUsed + l;
>
> This is equivalent to `graphRamBytesUsed +=3D l`.
>
> This code is susceptible to lost update due to non-atomic read-modify-wri=
te
> operation.
>
> I guess the it's not really a problem, because this code is in fact
> single-threaded when documents are added to the index. It might be
> concurrent during merging, but then `ramBytesUsed()` isn't called, and it=
's
> a wasted work.
>
> If the above assumption is correct, then this field should not be volatil=
e
> to improve performance. If it's not, it should be replaced with
> `AtomicLong` or `LongAdder`.
>
> Viliam