A @Volatile property is updated with ++ or +=, which is not atomic

tx0/kotlin:volatile-compound-updaterecommendedwarningreliability/concurrencydepth fastbug, concurrency

What it finds

@Volatile var count = 0 makes each read and write visible to other threads, but count++ reads and then writes, and two threads can both read the old value. Updates are lost without any error. An atomic type or a lock makes the update atomic.

From the rule's source, volatile_compound_update.rs.

Example from the rule's tests
class Counter {    @Volatile    private var count = 0    @Volatile    private var running = false    private var plain = 0     fun hit() {        count++tx0 reports: the update of the volatile count is not atomic        this.count += 2tx0 reports: the update of the volatile count is not atomic        count = 0        running = true        plain++    }}
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/kotlin:volatile-compound-update" = "warning"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

// tx0: ignore[tx0/kotlin:volatile-compound-update] reason="..."

tx0 explain tx0/kotlin:volatile-compound-update shows the same in the terminal. Found a false positive? Report it.