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

Side effects: files, network and processes

The analysis in Reads and writes from tokens sees the function’s own variables and fields. It cannot see the world outside the program. Take this function:

#[psync::graph]
async fn recreate(api: &Api, name: &str, obj: &Obj) -> Result<(), Error> {
    api.delete(name).await?;
    api.create(obj).await?;
    Ok(())
}

Neither statement writes a variable the other reads, and api is a shared & handle. Without more information, psync would start the create before the delete finished, even though both act on the same object on the server. It would also start the create when the delete fails, which sequential code never does. The same goes for fs::write("cfg.toml", ..) followed by fs::read_to_string("cfg.toml"): the file connects the two statements, but no variable does.

The effect analysis closes this gap. It gives every statement a list of effects, each one a read or a write of a target in a domain. Statements whose effects conflict keep their order, and a write never runs before an earlier statement that might leave.

In numbers

23 of 89rewrites of real functions from the audit in which psync had run a write before an earlier exit, or reordered two operations on the same resource
5of them need a one-line hint to rewrite at all (now 79 of 90 automatic and 10 with hints; 1 is still impossible)
11functions in the kube-rs and DataFusion benchmark patches; 9 generate identical code, and 2 now keep their writes in order
3.79×graph_suite geometric mean (3.82× before); 4 examples gained a speculate hint, and one keeps two uploads of the same file in order

Domains and targets

psync recognizes effects from the call itself. It sees tokens, not types, so it goes by paths and names:

DomainRecognized fromReadsWritesTarget
filea path through fs (std::fs::write, tokio::fs::read_to_string), File::create/File::open, OpenOptions, DirBuilderread, read_to_string, read_dir, metadata, canonicalize, open, …everything else (write, create_dir_all, remove_file, rename, copy, …)the path argument; rename/copy also the second
networkTcpStream, UdpSocket, TcpListener, UnixStream, net::connect, bind, …send_to, send, write, write_allthe address
environmentenv::env::var, …set_var, remove_var, set_current_dirthe variable name
processCommand::new, process::always a writeconflicts with every domain (cargo psync check, which knows more, exempts a read of the environment)
SQLan awaited chain with an SQL statement as a string literal: sqlx::query(".."), sqlx::query!(".."), client.query("..", &[]), client.execute("..", &[])SELECT, SHOW, EXPLAIN, VALUES, and a WITH that only selectsevery other statement (INSERT, UPDATE, DELETE, DDL, BEGIN, SET, …), and reads that lock or write (FOR UPDATE, INTO, nextval, EXPLAIN ANALYZE)none: an SQL write conflicts with every service call
HTTPa Method::X argument (client.request(Method::PUT, url), any path ending in Method::X) or a builder’s .method(Method::X) / .method("X")GET, HEAD, OPTIONS, TRACE (RFC 9110’s safe methods)every other methodthe URL argument (.uri(..) or .url(..) for a builder), compared as a path; a POST or an unknown method may touch anything on the service; the receiver is the service
Redisthe redis crate’s command methods (con.hget(k, f), con.lpush(q, x)), and redis::cmd("..") with a literal commandhget, mget, lrange, smembers, zrange, ttl, …hset, hdel, mset, sadd, zadd, get_del, …; list pushes and pops and stream appends are ordered; a redis::cmd psync cannot readthe key (the first argument, or the first .arg(..))
serviceany other awaited callits name starts with a read verb: get, list, head, fetch, query, describe, …its name starts with a write verb: create, put, post, patch, delete, upload, insert, update, commit, …the receiver path (api, self.s3) is the domain; the verb’s first argument is the target

Some details:

  • The first verb in a method chain decides. client.get(url).header(..).send().await reads. s3.put_object().bucket(b).key(k).send().await writes. A verb is the whole name or its first _-separated word, so delete_namespaced_pod, put_blob and list_objects_v2 classify without any configuration.

  • Ordered verbs (send, append, push, publish, enqueue, emit, notify, ack, …) are writes whose relative order is observable. Two of them on one domain never reorder, whatever their targets.

  • Different receivers are different services. self.cache and self.db do not conflict. An awaited free function (delete_user(id).await) reaches an unknown service, which may be behind any receiver. A receiver bound inside one statement (its own socket or client) is a different receiver from another statement’s, even with the same name.

  • Plain synchronous calls count only when they reach a known domain. std::fs::write(..) is a write. map.get(k) is not a service call, because it is not awaited.

  • An HTTP method decides. client.request(Method::GET, url).send().await reads, although send is an ordered verb: the method says what the request does, RFC 9110 says GET, HEAD, OPTIONS and TRACE are safe (they do not change the server), and a write is everything else. Two GETs of one URL run together; a GET and a PUT of it keep their order; a PUT to another literal URL does not conflict. URLs compare as paths, so a DELETE of https://h/a keeps its order with a GET of https://h/a/x. A POST may do anything on the server, so it conflicts with every request to it (test http_methods_decide_by_rfc_9110). These are the rules of the HTTP specification in What psync guarantees. Safe is a promise the server makes. A server that changes state on a GET (a counter, a lazy migration) breaks it. The method decides before any verb list, so effects(write(..)) cannot override it; mark such a statement #[psync::seq].

  • Redis commands are known by name. The redis crate names its methods after the commands, and most command names are not verbs: hget, zcard, lpush. psync knows them (the list is on Names psync knows), matched before the verbs, because get_del deletes although its first word reads. redis::cmd("HGET").arg(key) is classified by its command, like an SQL statement, with the key as the target; redis::cmd(name) with a command psync cannot read is a write (test redis_commands_classify). In the audit this mattered. loco’s requeue (web-34) removes a job from the processing set with conn.srem(..) after job.to_json()?. srem used to be unclassified, so a read, and psync ran it before the ?: had serialization failed, the job would already have left the processing set, which sequential code never does. Now srem is a write and waits for the ?; web-33 is the same shape with zrem.

  • SQL is decided by the statement. query is a read verb, so until this rule sqlx::query("DELETE FROM t").execute(db).await counted as a read, and ran at the same time as a SELECT on the same table. Now the statement’s first word decides, and the name of the method does not. A statement psync cannot read, in a call it knows runs SQL (sqlx::query(&sql), diesel::sql_query(s)), may be anything, so it is a write. The database may be behind any receiver, and the statement’s tables are not compared, so an SQL write is ordered with every service call around it (test sql_statements_decide_read_or_write).

    #[psync::graph]
    async fn purge(db: &Pool) -> Result<(), Error> {
        let old = sqlx::query("SELECT id FROM jobs WHERE done").fetch_all(db).await?;
        let n = sqlx::query("SELECT count(*) FROM jobs").fetch_one(db).await?;
        sqlx::query("DELETE FROM jobs WHERE done").execute(db).await?;
        Ok(())
    }
      region 0:
        n0: old = fetch_all()? [await] {read service (sqlx::query: SELECT)}
        n1: n = fetch_one()? [await] {read service (sqlx::query: SELECT)}
        n2: execute()? [await] {write service (sqlx::query: DELETE)}
        n0 -> n2 (write service (sqlx::query: DELETE) vs read service (sqlx::query: SELECT))
        n1 -> n2 (write service (sqlx::query: DELETE) vs read service (sqlx::query: SELECT))
        level 0: n0 || n1
        level 1: n2
    

    The two SELECTs run together, and the DELETE waits for both. (It would wait anyway, for their ?s, but the SELECT edge is also the reason under speculate.)

The lists live in psync-graph/src/effects.rs.

When two effects conflict

Two effects conflict when at least one writes, their domains may be the same, and their targets may be the same. Targets are compared only as far as tokens allow:

  • Two string literals are either equal or not: "a.toml" and "b.toml" differ. For files, a directory and the paths inside it overlap: remove_dir_all("out") conflicts with read("out/a.txt").
  • format!("prefix{x}") keeps its literal prefix. format!("m.secret_storage.key.{id}") cannot equal "m.megolm_backup.v1", but may equal "m.secret_storage.key.k1".
  • Two format! strings whose placeholders are plain names or fields are compared piece by piece. format!("{dir}/AppRun") and format!("{dir}/linuxdeploy") agree up to {dir}/ and then differ in their text, so they name different files. Within one region a name has one value (a statement that rebinds or changes it is ordered with both), so this is exact. They still conflict if they first differ at a placeholder ({dir} against {other}, which may hold the same text), if one is a directory of the other ({dir}/a and {dir}/a/b), if the format specs differ ({dir:?}), or if a placeholder is computed ({} of self.base(), which may return something new each call).
  • A target that is just a name, fs::write(&a, ..), takes the value of the block’s latest let a = ..; before it, when that binding is not mut and its value is a literal, a format! or a join. let a = dir.join("a.json") names the file {dir}/a.json, the same template as format!("{dir}/a.json"). A join counts only when the name cannot be absolute: its text must start with a character other than / or \, and not with a drive letter such as C:, because Path::join with an absolute path replaces the base. The binding’s value must not depend on a mut binding, and a later let of a name the value depends on forgets it.
  • Anything else computed may be anything. Two such file paths can still run concurrently, in asynchronous code and on threads, and they are compared at run time (Paths compared at run time). So can two computed URLs of Method::X requests, and two computed keys of redis::cmd(..) (URLs and Redis keys).

Path::new(x), PathBuf::from(x) and the like name what x names, so Path::new(&format!("{dir}/one")).exists() reads the file {dir}/one.

Like literals, templates compare paths as text. Two different names in one directory are taken to be different files, even though a symbolic link could make them one.

path.exists(), is_file(), metadata() and the other methods of std::path::Path that read the file system are file reads of the receiver. They are also kept as reads of a service named by the receiver, in case the receiver is some other type with a method of that name. Until this change they were only the service read, so in synchronous code if !p.exists() { .. } and a later fs::write(&p, ..) could run at once (test path_exists_reads_the_file_system).

Reads never conflict with reads. Here is a read-check-recreate function:

#[psync::graph]
async fn replace(api: &Api, limits: &Limits, name: &str, obj: &Obj) -> Result<(), Error> {
    let old = api.get(name).await?;
    let quota = limits.get(obj.ns).await?;
    api.delete(name).await?;
    api.create(obj).await?;
    Ok(())
}

and what psync builds for it:

  region 0:
    n0: old = get()? [await] {read `api` `name` (get)}
    n1: quota = get()? [await] {read `limits` `obj . ns` (get)}
    n2: delete()? [await] {write `api` `name` (delete)}
    n3: create()? [await] {write `api` `obj` (create)}
    n0 -> n2 (write `api` `name` (delete) vs read `api` `name` (get))
    n1 -> n2 (may exit before `delete` runs)
    n0 -> n3 (write `api` `obj` (create) vs read `api` `name` (get))
    n1 -> n3 (may exit before `create` runs)
    n2 -> n3 (write `api` `name` (delete) vs write `api` `obj` (create))
    level 0: n0 || n1
    level 1: n2
    level 2: n3

The two reads still overlap. The delete waits for both, and the create waits for the delete.

Files work the same way:

  region 0:
    n0: fs::write()? [await] {write file "app/cfg.toml" (fs::write)}
    n1: cfg = fs::read_to_string()? [await] {read file "app/cfg.toml" (fs::read_to_string)}
    n2: readme = fs::read_to_string()? [await] {read file "README.md" (fs::read_to_string)}
    n0 -> n1 (write file "app/cfg.toml" (fs::write) vs read file "app/cfg.toml" (fs::read_to_string))
    level 0: n0->n1 || n2

Writes are not speculated

Early exits explained that psync may start an await before it knows whether an earlier ? leaves the function. For a read that is harmless. For a write it is not, because the sequential program would not have written at all. So:

  • In a region, a statement with a write effect waits for every earlier statement that may exit (may exit before `delete` runs).

  • In a loop, prefetching starts the prefixes of later items before earlier items finish. In fact it runs every item’s prefix, as Runtime support explains. If the body can end the loop (?, return, break, or a labeled continue to an outer loop), a prefix with a write is not prefetched:

    loop `for f in files`: would write `store` `f` (upload) for later items before knowing whether an
    earlier item ends the loop (`#[psync::speculate]` allows it)
    
  • In fission, the first part runs for every item before the rest runs for any item. So it may not contain writes when the body can end the loop.

Writes across iterations

Prefetching also reorders iterations against each other: item 3’s prefix may run before item 2’s rest. psync assumes that targets computed from the loop item name different objects in different iterations. store.upload(f) for f in files touches a different object per item. A target that does not mention the item, such as a literal, a field or an unknown free function, is the same in every iteration:

  • a prefetched write to such a target is refused (would write ... in every iteration at once);
  • a prefetched read that conflicts with such a write elsewhere in the body is refused too.

If two items can name the same object, for example a list with duplicates, the assumption is false. For files, psync checks it at run time. When every per-item path can be evaluated ahead (dir.join(name), format!("{name}.json")), the generated code compares the paths of all items first and runs the loop as written if two items share a file that one of them writes (Loop rewriting). For other targets, or a path that comes from awaited work, deduplicate first (the fission pattern from jdx/mise does exactly this), or mark the loop #[psync::seq].

Hints and options

Hint or optionSaysEffect
#[psync::speculate] on a statement or for looprunning these writes when sequential code would not have is harmless (idempotent uploads, cache fills, PUTs of the same value)lifts the speculation rule only
#[psync::graph(speculate)]the same, for the whole function
#[psync::independent] on a statementits effects commute with the statements around it (two writes to different objects through one client)lifts effect conflicts for that statement; data dependencies stay
#[psync::independent] on a loopiterations do not depend on each otherlifts the cross-iteration rules; the speculation rule stays
#[psync::graph(effects(read(..), write(..), ordered(..)))]the listed names (whole names or first words) read, write, or write in orderextends the verb lists, for your own APIs
#[psync::graph(strict)]unclassified awaited calls may do anythingsee below
#[psync::graph(calls = ..)]how the whole function’s calls behavesee Marking a whole function

Hints are assertions. psync does not check them.

Unclassified calls and strict

An awaited call whose name has no known verb, such as client.request(req) or do_work(x), is unclassified. By default it is assumed to read, which is how psync behaved before effects existed. explain lists every such call, so you can see what you are trusting:

  unclassified (assumed to read; `strict` treats them as writes): request

Under #[psync::graph(strict)], an unclassified call is a write to an unknown resource. It conflicts with every effect and never runs speculatively. Use strict where a wrong guess is expensive, and teach psync your API’s names with effects(..) to get the concurrency back.

Marking a whole function

calls = .. says, for the whole function, what you know about its calls:

OptionUnclassified callsStatements’ effects
noneread (write with strict)ordered where they conflict
calls = readread, even with strictordered where they conflict
calls = writewrite (the same as strict)ordered where they conflict
calls = write_independentwrite: never run ahead of an earlier exitcommute: no effect order between statements
calls = independentreadcommute: no effect order between statements

Calls that psync does classify keep their verb: under calls = read, db.put(k).await is still a write. The independent forms put #[psync::independent] on every statement, so data dependencies and the exit rule still hold. Only conflicts between effects are lifted.

calls = write_independent fits a function that sends unrelated writes, such as a handler that notifies several systems. None of them may happen if an earlier step failed, but they need not wait for each other. A write never runs ahead of an earlier exit, so this helps only when the writes have no ? between them. Check their results afterwards:

#[psync::graph(calls = write_independent)]
async fn on_signup(api: &Api, mail: &Mail, crm: &Crm, id: u64, email: &str) -> Result<(), Error> {
    api.check_quota(id).await?;
    let sent = mail.welcome(email).await;
    let tracked = crm.track(id).await;
    sent?;
    tracked?;
    Ok(())
}

welcome and track have no verb that psync knows. Here is what psync-expand prints for each option:

OptionSchedule
nonelevel 0: n0 || n1->n3 || n2: the email may go out although check_quota failed
calls = writen0, then n1, then n2 || n3, then n4: safe, and one call at a time
calls = write_independentn0, then n1->n3 || n2, then n4: after the quota check, both at once

With mail.welcome(email).await? on each line instead, every call would wait for the one before it, because that one’s ? might leave.

The test calls_marks_the_whole_function in psync-graph checks every row of the table.

What the audit found

Re-running psync over the 90 confirmed real-world findings showed the analysis at work.

Five rewrites now need a hint:

  • matrix-rust-sdk delete_all_known_secrets and mark_backup_as_enabled write account data in a loop or one after another, behind ?.
  • apache/iggy route_data writes Iceberg tables in a loop behind ?.
  • zed open (file diff view) performs two project.update(..) calls.
  • zed get_git_committer runs git.run(..) twice, and run counts as a write verb.

Eighteen more keep their concurrency but now order a write. Among them are deletes in neon’s physical GC, puts in delta-rs, uploads in mise and GreptimeDB, send_to_device in matrix-rust-sdk, post_validator_voluntary_exit in Lighthouse, create_dir_all in Quickwit, and shutdown in DataFusion’s writer orchestration.

Each of these 23 was a place where the earlier psync could perform a write the sequential program would not have, or reorder two operations on one resource.

The analysis is also conservative where names mislead. DataFusion’s ListingSchemaProvider::refresh calls self.factory.create(..), which builds a table provider in memory. psync reads create as a write and no longer prefetches that loop. One line restores it: #[psync::speculate] on the loop, or effects(read(create)) on the function.

Limits

This is still a token-level analysis:

  • Names can lie. A get that writes, or an unclassified call that deletes, is trusted as named. strict removes the second risk. Only types can remove the first (see the roadmap).
  • Aliasing outside the program is invisible. Two receivers that reach the same server, a symlink, or two paths to one file are treated as different.
  • Callees are opaque to the macro. An awaited call to your own helper is classified by the helper’s name, not by what its body does. cargo psync check reads the bodies and reports where that guess was wrong.
  • Panics are not exits, from tokens alone. The macro cannot tell which calls may panic, so a write may still run ahead of an earlier call that panics. The compiler can: cargo psync check reports such a write, and with compiler facts the macro keeps it in order.

Prior work and novelty

WorkWhat it doesHow psync differs
ReSynchronizer (ECOOP 2021, Table 1)Models the file system and the network as one pseudo-variable each; exec, spawn and eval touch everything.psync’s effects have targets: a path, a URL, a key. A file read waits only for writes that may touch the same file, not for every file write.
Deterministic Parallel Java (OOPSLA 2009)Methods declare reads and writes of nested regions, and the compiler checks that parallel tasks do not interfere. Regions nest, and overlap by prefix.psync infers its effects instead of asking for annotations, and its places (w/fs/t/<path>, see Certified schedules) resemble DPJ’s region paths: nested, overlapping by prefix, here for files, URLs and keys.
Quasi-Static Scheduling for Safe Futures (PPoPP 2008)Inserts barriers where a static alias analysis cannot rule out a conflict between futures.psync’s gates compare the values at run time (paths, and since #197 file identity: device and inode), for resources outside the program.
Commutativity analysis (Rinard and Diniz, 1997)Proves that in-memory operations commute, by symbolic execution.psync takes commutativity of outside services as a specification (The outside world).

Exercises

Predict the edges, then check with psync-expand FILE LINE.

  1. async fn f1() -> std::io::Result<()> {
        tokio::fs::remove_dir_all("out").await?;
        let a = tokio::fs::read("out/a.txt").await?;
        let b = tokio::fs::read("cfg.toml").await?;
        Ok(())
    }
  2. async fn f2(client: &Client, id: &str) -> Result<(), Error> {
        let backup = client.get_account_data("m.megolm_backup.v1").await;
        client.put_account_data(format!("m.secret_storage.key.{id}"), "{}").await;
        let key = client.get_account_data("m.secret_storage.key.k1").await;
        Ok(())
    }
  3. async fn f3(&self, id: u64) -> Result<(), Error> {
        let user = self.db.get_user(id).await?;
        self.cache.put(id, &user).await;
        let other = self.db.get_user(id + 1).await?;
        delete_user(id).await;
        let again = self.db.get_user(id).await?;
        Ok(())
    }
    Why does delete_user conflict with both get_user(id) and get_user(id + 1)?
  4. async fn f4(api: &Api) -> Result<(), Error> {
        let cfg = api.get_config("main").await;
        let out = tokio::process::Command::new("ls").output().await;
        let home = std::env::var("HOME");
        let more = api.get_config("other").await;
        Ok(())
    }
  5. async fn f5(c: &Client) -> Result<(), Error> {
        let a = c.request(Method::GET, "https://h/users/1").send().await?;
        let b = c.request(Method::GET, "https://h/users/2").send().await?;
        c.request(Method::DELETE, "https://h/users/2").send().await?;
        let d = c.request(Method::GET, "https://h/groups").send().await?;
        c.request(Method::POST, "https://h/audit").send().await?;
        Ok(())
    }
    Which requests run together? Why does the POST wait for every other request, even the GET of /groups?
Answers
  1. One edge, n0 -> n1 (write file "out" (fs::remove_dir_all) vs read file "out/a.txt" (fs::read)): a directory contains the paths inside it. cfg.toml is neither, so the level is n0->n1 || n2. The reads may run before remove_dir_all returns, because reads are not held back by an earlier ?.
  2. Only n1 -> n2. The template m.secret_storage.key.{id} keeps its literal prefix, so it cannot be m.megolm_backup.v1, but it may be m.secret_storage.key.k1. The level is n0 || n1->n2.
  3. Three edges into delete_user (n3), from n0, n1 and n2, and one out of it, to again. An awaited free function reaches an unknown service (write service `id` (delete_user)), which may be behind any receiver. Its target id is computed, and so are id and id + 1, which tokens cannot tell apart, so it conflicts with every read and write around it. The cache.put waits for user (data), and get_user(id) and get_user(id + 1) run together. The levels are n0 || n2, then n1, then n3->n4.
  4. A process conflicts with every domain: it could read or write anything. So n0 -> n1, n1 -> n2 and n1 -> n3. The levels are n0->n1, then n2 || n3. The two get_config reads would have run together without the command. (cargo psync check knows more, and lets the environment read through.)
  5. The three GETs run together (level 0: n0 || n1 || n3): safe methods only read. The DELETE waits for the GET of the same URL (a conflict) and for the first GET’s ? (a write may not run before an earlier exit). The POST comes last: RFC 9110 lets a POST do anything the server decides, so its target is the whole service c, and it conflicts with every request there, /groups included. Levels: n0 || n1 || n3, then n2, then n4. (Check with psync-expand.)