# Data race condition in automaton queries

**URL:** <https://discuss.elastic.co/t/data-race-condition-in-automaton-queries/353937>\
**Category:** Elasticsearch\
**Tags:** painless, runtime-fields\
**Created:** [February 22, 2024, 11:46pm UTC](https://discuss.elastic.co/t/data-race-condition-in-automaton-queries/353937 "2024-02-22T23:46:48Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![yfful](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yfful/32/88024_2.png) [@yfful](https://discuss.elastic.co/u/yfful)\
**Post date:** [February 22, 2024, 11:46pm UTC](https://discuss.elastic.co/t/data-race-condition-in-automaton-queries/353937/1 "2024-02-22T23:46:48Z")

</div>

Hello,

In a local unit test involving a runtime field and a regexp query with ES 8.12.1, I have experienced search inconsistencies with the result count. The query shown below uses the script `parity` which returns `even` or `odd` depending on the value of another numeric field. It returns sometimes an unexpected number of hits.

```json
{
  "query": {
    "regexp": {
      "outer_parity": {
        "value": "e.e."
      }
    }
  },
  "runtime_mappings": {
    "outer_parity": {
      "type": "keyword",
      "script": {
        "lang": "painless",
        "source": "parity"
      }
    }
  }
}

```

I believe that following this [change](https://github.com/elastic/elasticsearch/pull/101230) that enabled parallelization by default, the queries extending `AbstractStringScriptFieldAutomatonQuery` have a data race condition. If I understand that change correctly, then the code below can be called concurrently:

> <https://github.com/elastic/elasticsearch/blob/f0e43173eac4549ed78375b991ebd61c0cc13d2e/server/src/main/java/org/elasticsearch/search/runtime/AbstractScriptFieldQuery.java#L71-L77>

Therefore, the `BytesRefBuilder` scratch below is shared by all threads that execute a search on different segments, which would lead to the race condition I am seeing. With the query shared above, the `scratch` variable would contain `even` for example, although the `values` list passed in argument contains only `odd`. The race condition would explain this inconsistency.

> <https://github.com/elastic/elasticsearch/blob/f0e43173eac4549ed78375b991ebd61c0cc13d2e/server/src/main/java/org/elasticsearch/search/runtime/AbstractStringScriptFieldAutomatonQuery.java#L20>

What do you think ?

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [March 21, 2024, 11:47pm UTC](https://discuss.elastic.co/t/data-race-condition-in-automaton-queries/353937/2 "2024-03-21T23:47:14Z")

</div>

Closed on March 21, 2024, 11:47pm UTC

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
