Skip to content

Possible Race Condition in APC::updateHistogram #140

@skyellin

Description

@skyellin

We have noticed that our application occasionally times out. When this happens, logs nearly always indicate that the script stopped execution at "promphp/prometheus_client_php/src/Prometheus/Storage/APC.php on line 72" (sometimes line 71).

This is the following block inside \Prometheus\Storage\APC::updateHistogram

// Atomically increment the sum
// Taken from https://github.com/prometheus/client_golang/blob/66058aac3a83021948e5fb12f1f408ff556b9037/prometheus/value.go#L91
$done = false;
while (!$done) {
    $old = apcu_fetch($sumKey);
    if ($old !== false) {
        $done = apcu_cas($sumKey, $old, $this->toBinaryRepresentationAsInteger($this->fromBinaryRepresentationAsInteger($old) + $data['value']));
    }
}

My very superficial understanding leads me to believe that apcu_cas may be unlikely to return true if another process (or many processes) are also continuously updating the value stored at $sumKey, since $old may no longer match the persisted value.

Do you see any cause for concern or is my analysis incorrect?

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