Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

What psync guarantees about the outside world

A fair objection to everything in this part: the compiler sees only the client. A server may change its state even on a read. A GET may bump a rate-limit counter, refresh a session or fill a cache, so two “reads” may not commute at the server, and no client-side analysis can tell, whether it reads tokens, MIR, or anything else. So what can psync guarantee about concurrent accesses?

The objection is right that nothing can be guaranteed outright. It is wrong that nothing can be guaranteed. Every correct reordering, by a person or a tool, is correct relative to a model of the outside world. psync’s contribution is to make that model explicit, prove its schedules against it, and make it testable. This chapter states the guarantee precisely. The proofs are in verify/World.lean, checked by Lean in CI.

The guarantee, in four layers

LayerWhat psync knowsFromStrength
client memoryno two concurrent branches race on a variable; borrows end before movesRust’s ownership types, checked by rustc on the generated codesound: rustc rejects the code otherwise
client I/O footprintwhich files, services and processes each branch can reach, through every callee and every dyn implementationMIR summaries and link-time class-hierarchy analysis (cargo psync check)sound up to stated gaps: FFI, function pointers, callees it marks opaque
the outside worldwhich pairs of requests commutea commutativity specification: psync’s built-in rule, or one you giveassumed, but explicit and testable
compositionthe schedule behaves like the sequential programworld_levels_correct, in Leanmachine-checked

The first row is what a tool for a language without ownership types cannot build soundly, and it is why the JavaScript tools in this space describe themselves as unsound.

The model

World.lean describes three things.

  • The server is a labelled transition system: a function step that takes a request and the server’s state to a new state and a response. Nothing is assumed about which requests change the state. A read that writes, such as a counter, is an ordinary transition.
  • The client program is psync’s statements. Each one reads and writes client locations, as in Proofs. It may send one request, computed from what it reads, and update the client from the response.
  • The world is the client’s store, the server’s state, and the response each call received.

Two requests commute at the server when, in either order, the server ends in the same state and each gets the same response. A specification is a relation on requests, and it is sound for a server when every pair it allows really commutes.

Two statements are independent relative to the spec when their client footprints are disjoint (no edge between them) and any requests they send are ones the spec allows to commute.

The theorem

world_levels_correct. If the specification is sound for the server, then every concurrent execution of psync’s schedule, in whatever interleaving each level takes, ends with the same client state, the same server state, and the same response for every call as the sequential program.

The proof reuses the whole scheduling argument. Schedule.lean’s theorems are restated for any state and any independence relation under which steps commute (the Kernel section). The only new step is that two independent statements commute in the world (world_commute). Their client updates commute because of the footprints, and their requests commute because the spec is sound.

With the empty specification, in which no two requests commute (empty_spec_sound), the theorem holds for every server. Then no two calls overlap, and only client work and requests to different servers can. That is the conservative end. strict moves psync towards it for the calls it cannot classify.

psync’s built-in rule is a specification, and it can be wrong

By default, psync decides from names or, with compiler facts, from bodies. A call is a read, a write or an ordered write of a service, with a target when one is known. Two calls overlap unless one writes, both are on the same service, and their targets may be the same.

That rule is a commutativity specification, so the theorem says exactly when it is safe: when the rule is sound for the servers involved. Its weakest clause is “two reads always commute”. World.lean proves both sides on a small server that counts every request, as a rate limiter does:

inductive Req
  | get (k : Nat)   -- returns the value, and bumps the counter
  | count           -- returns the counter, and bumps it

theorem psync_rule_sound_for_gets (k k' : Nat) : Commutes kv (.get k) (.get k')
theorem psync_rule_unsound_for_counters :
    ¬ Sound kv (fun a b => psyncSpec (kvEff a) (kvEff b))

Two gets commute: the counter ends the same either way, and neither response mentions it. A get and a count do not: count’s answer depends on whether the get came first. psync’s rule calls both reads (get_value, fetch_count) and would overlap them, so for this server the rule is unsound, and the guarantee does not apply.

When your server is like this, say so. Make the call that observes the counter a write (effects(write(fetch_count))) or keep it in place (#[psync::seq]). Either way, the specification psync schedules with becomes sound again.

Specifications for HTTP, S3 and Kubernetes

verify/Specs.lean writes three real protocols as server models and proves a specification sound for each. The models are the assumptions: one copy of the state, requests applied one at a time (linearizable), and nothing else changing the state behind the client’s back.

ServerThe spec allowsProvedAnd proved not to commute
HTTP (RFC 9110): resources on paths; GET, HEAD, list are safe; PUT, DELETE change their target; POST does whatever the server doestwo safe requests; or two non-POST requests on paths apart (neither a prefix of the other)httpSpec_sound, for every meaning of POSTtwo identical PUTs (http_put_twice_not_commute)
S3, without versioningthe same; and the same PUT or DELETE twices3Spec_soundtwo PUTs of different bodies to one key
Kubernetes APIfor clients that ignore resourceVersion: two reads; writes to different objects; a list and a write in another namespacekube_commutes_up_to_rvtwo writes to different objects, exactly (kube_writes_not_commute)

Two of these results correct intuitions that psync’s rule relies on:

  • Idempotent is not commutative. RFC 9110 calls PUT idempotent: doing it twice leaves the state as doing it once. But the first PUT to a new path answers 201 Created and the second 200 OK, so the two do not commute, and a client that looks at the status sees the order. S3’s PutObject answers with the ETag, which depends only on the body, so there two identical PUTs do commute.
  • Kubernetes writes to different objects do not commute. Every write takes the next value of one cluster-wide counter, the resourceVersion, and returns it. Run two applys in the other order and each object gets the other’s version. psync’s built-in rule, which lets writes with different targets overlap, is therefore sound only for clients that do not compare resourceVersions across objects. The proof makes this precise. Erasing resourceVersions is a simulation: it commutes with every request (kube_simulation). On the erased server the spec is sound, so requests the spec allows commute up to resourceVersions. A client that uses resourceVersion for optimistic concurrency on one object is unaffected. A client that orders events from different objects by resourceVersion sees the difference.

Testing the specification

The specification is the only assumption, and it is about something you can observe. A server-side log (the Kubernetes API server’s audit log, S3 access logs) records which requests were applied in which order. If two requests that psync ran concurrently were applied in an order that changed the outcome, that log entry is a counterexample to the specification, not just a failed test. The benchmark harnesses already record these logs (see the case studies). scripts/spec-check/check_logs.py checks those logs against the specifications above. For every pair of requests that may have been in flight at once, it asks whether the spec lets them commute, using the Lean file’s definitions:

scripts/spec-check/check_logs.py --kube logs/audit.log --s3 logs/garage.log

On the logs of the production-shaped benchmark (kube-rs against a real API server, DataFusion against Garage, an S3-compatible store):

LogRequestsPairs that may have overlappedAllowed, and whyNot allowed
Kubernetes audit31,638 (31,078 list, 560 get)65,893two reads: all0
S3 (Garage)77,149 (71,168 GET, 5,888 PUT, 93 list)4,533,859two reads: 3,678,981; keys apart: 854,8780

No two requests the specification forbids were ever in flight together. For the Kubernetes log this is a weak result, because the benchmark only reads, and two reads always commute. For S3 it is a real one. While objects were being uploaded, 854,878 pairs that included a PUT overlapped another request, and every one of them was on a key apart from the other request’s key.

How overlap is decided matters, and the checker errs towards checking more:

  • An audit event records when the request was received and when its response completed, so its interval is exact.
  • Garage logs only when a request starts. On one HTTP/1.1 connection, a request ends before the next one starts, so the next start bounds it. The last request on a connection is bounded by 5 seconds. Both bounds lengthen intervals, so every pair that really overlapped is checked.

The checker is tested on logs with planted violations (scripts/spec-check/selftest.sh, run in CI): a GET and a PUT of one key, a listing and a PUT under its prefix, two writes to one Kubernetes object, and a list and a create in the same namespace.

What this does not test is the server side: that the pairs the spec allows really commute at the server. That needs bodies and resourceVersions, which these logs do not record (Metadata audit level). A RequestResponse-level audit log would show it, as would S3 access logs with ETags.

What is not modelled

  • One client, one deterministic server. With other clients, even the sequential program’s result depends on their timing. The theorem compares the two runs against the same server behaviour, and a specification meant to hold under concurrent clients has to say so.
  • The generated Rust, which rustc type- and borrow-checks.
  • Footprint soundness for real Rust. That MIR summaries over-approximate what a function touches is proved only for a small fragment of MIR (verify/Footprint.lean, see Footprints, proved for a fragment of MIR). For the rest of Rust, cargo psync check checks it and nothing proves it.

Prior work and novelty

WorkWhat it doesHow psync differs
The Scalable Commutativity Rule (Clements et al., SOSP 2013)Defines when operations commute as seen through an interface, depending on the state, and checks 18 POSIX calls.The same notion of commutativity, used for a different end: psync uses it to justify reordering a client’s calls, not to design scalable kernels.
Linearizability (Herlihy and Wing, 1990)Each operation appears to take effect at one instant between its call and its return.psync’s world model makes each server operation one atomic step of a transition system; that is an assumption of linearizability, stated here.
CompCert (2009)Treats I/O as a trace that must not change.psync allows the trace to change where the specification says the operations commute, and must therefore state the specification, and test it against server logs.

Exercises

  1. An API’s get_item(k) returns the item and the number of times it has been read. psync runs two get_item calls concurrently. Does the guarantee apply? What do you change?
  2. Two identical PUTs to one new path run concurrently. Do they commute on a generic HTTP server? On S3 without versioning?
  3. A controller applies two different ConfigMaps concurrently, then logs which one has the larger resourceVersion. Is the schedule still “the same as sequential”?
  4. The audit-log check found 0 forbidden pairs in 65,893 Kubernetes pairs. Why does the chapter call that a weak result, and the S3 one a real one?
  5. Which planted violation would catch a psync bug that started an S3 PUT while a LIST of its bucket prefix was in flight?
Answers
  1. No. The response depends on how many reads came before, so two get_items do not commute. This is psync_rule_unsound_for_counters with a different name: psync’s rule calls both reads, and the rule is unsound for this server. Declare the call a write (effects(write(get_item))), or keep one in place with #[psync::seq].
  2. Not on a generic HTTP server: the first answers 201 Created, the second 200 OK, so the order is visible (http_put_twice_not_commute). On S3 they do: the ETag depends only on the body, and s3Spec allows the same PUT twice (s3Spec_sound).
  3. Only up to resourceVersions. Each write takes the next value of one cluster-wide counter, so in the other order each object gets the other’s version (kube_writes_not_commute). A client that compares versions across objects sees the difference. Keep the second apply in place.
  4. The Kubernetes benchmark only reads, and two reads always commute under the spec, so no pair could have failed. On S3, 854,878 overlapping pairs included a PUT, and each needed its keys to be apart. That is a check that could have failed.
  5. “A listing and a PUT under its prefix”, one of the four in scripts/spec-check/selftest.sh.