The Flow Book
  1. Why Flow

One week at the support desk

  1. A payment that timed out
  2. What the run was told
  3. Writing the payments library
  4. Waiting for the manager
  5. The next morning
  6. When the process dies mid-call
  7. Fixing a bug in a half-finished run
  8. Three carriers at once
  9. Answering calls from your own service

Projects

  1. What people build
  2. A research agent you can check

The language tour

  1. Values, functions and blocks
  2. Records, choices and match
  3. Modules, libraries and Python functions
  4. Result, ToolProblem and ?

Appendix

  1. The flow command
Language reference ↗

When the process dies mid-call

Friday afternoon you ship a deploy, and the deploy restarts the machine that runs the desk's programs. One of them was in the middle of refunding Dana's 40 dollars. The provider had already paid, and its answer was on the way back when the process was killed.

This is the timeout from A payment that timed out again, with a difference that matters. Then, a live process received a NetworkError from the library and the program decided what to do. Now there's no process to receive anything. The record holds a question and no answer, and the next process that picks the run up has to put something in the gap. Whatever it puts there, the program will act on.

Killing a run with the refund out

In 1-refund-timed-out, PROVIDER=slow makes the stand-in provider pay at once and take 30 seconds to answer. Start the run in the background:

PROVIDER=slow flow run main.flow order=A-1042 dollars=40 &

Wait until the provider's ledger shows the payment, then kill Flow the hard way, as a crash or a power cut would. The run isn't driven by the flow you put in the background but by the Flow daemon, the host it handed the run to, so kill the daemon and everything it runs:

cat payments/ledger.txtA-1042 40 re_1042
pkill -9 -f 'flow daemon'

Dana has been paid, and the program was never told. A killed run prints nothing, so ask flow runs what's on disk:

flow runs8bd7f4352a64	stopped	main.flow	…		the host driving it restarted

The run is stopped: the process that drove it is gone, and the run stays open with nothing driving it. A host note in its history says so, "the host driving it restarted". If you look before the killed process has let go of the run, flow runs shows it as running instead.

flow show 8bd7f4352a64run 8bd7f4352a64state: stoppedopen root#1 payments.refund  order = "A-1042"  dollars = 40  returns std@v1/result.Result<payments.Receipt, payments.PayError>  it may have done its work already, and a resend asks it again under the same key  resend: flow resume 8bd7f4352a64 --resend 'root#1'2 of 2 facts · 0 hidden   1 Program    8bd7f4352a64 ./main.main       order = "A-1042"       dollars = 40   2 Call       root#1 payments.refund       order = "A-1042"       dollars = 40host activity (4 records; not semantic facts):…full host records: flow show 8bd7f4352a64 --json

The record ends at the refund request. Flow writes a request down before sending it and an answer before the program sees it, so wherever a process dies, what's left is a shorter record of the same run. It can be missing an answer that arrived too late; it never holds one that didn't arrive.

Asking the library again

A crashed run is continued like a waiting one, with flow resume in a new process. The new process runs the program from the top against the record, and at the refund it finds a request with no answer. Flow doesn't guess what became of it. Resumed with no options, the run waits on that call again, and the command exits with status 3:

flow resume 8bd7f4352a648bd7f4352a64 waiting: every open call waits for a replyopen root#1  it may have done its work already, and a resend asks it again under the same key  resend: flow resume 8bd7f4352a64 --resend 'root#1'  payments.refund    arguments: {"dollars":40,"order":"A-1042"}reply details: flow show 8bd7f4352a64 'root#1'next: flow show 8bd7f4352a64

The call stays open until something says what happened to it, and the one that can find out is the library that sent it. --resend asks it: it sends the open call to the library again, under the same key. What the library does with a key it has seen before is its own business, and this one sends every refund with the call's key as its idempotency key (Writing the payments library). This provider remembers keys, so the repeated request pays nothing and gets the provider's real answer:

PROVIDER=paid flow resume 8bd7f4352a64 --resendRefunded(Receipt { refund_id: "re_1042" })note: Open calls were selected for resend. The original call ID does not guarantee external deduplication; an external effect may happen again.8bd7f4352a64 completednext: flow replay 8bd7f4352a64
cat payments/ledger.txtA-1042 40 re_1042

The note is the command being careful: Flow can't know whether the service at the other end recognises a repeat, so it says plainly that the effect may happen again. This one does, and A-1042 has one line in the ledger. The record shows the refund asked once and answered once, because to the program it was one call; the resend shows only in the host's activity:

flow show 8bd7f4352a64run 8bd7f4352a64state: completed4 of 4 facts · 0 hidden   1 Program    8bd7f4352a64 ./main.main       order = "A-1042"       dollars = 40   2 Call       root#1 payments.refund       order = "A-1042"       dollars = 40   3 Reply      root#1 Ok       value = {"Ok":{"refund_id":"re_1042"}}   4 End        Completed       value = {"Refunded":{"refund_id":"re_1042"}}host activity (14 records; not semantic facts):…full host records: flow show 8bd7f4352a64 --jsonnext: flow replay 8bd7f4352a64

For that reason --resend is something you ask for on each resume, and never the default. Against a provider that doesn't remember keys, the same command pays twice, and nothing in Flow can stop it.

When sending again isn't safe

For a provider like that, the library has to recognise the key itself. It can write down each key before it sends the request, and when a call arrives with a key it has already sent, answer Interrupted instead of paying again:

if key in sent_keys():    raise Interrupted("sent before the process died; not sending it again")

main.flow already has an arm for Interrupted: it's the one that looks the refund up instead of sending it again. You wrote no code for crashes. The arm you wrote for "the money may have moved" covers this one, because from the program's side a crash mid-call is one more way for the money to have moved without anyone hearing about it.

And when a person checks the provider's dashboard and finds the refund, the answer they found goes back through the library too. A library whose runs are in the default store can take it by the call's key, with the toolkit's reply: the receipt inside Ok, {"Ok":{"refund_id":"re_1042"}} as JSON. Result, ToolProblem and ? lists the answers reply takes, and Answering calls from your own service says which runs it reaches.

What Flow doesn't do here

Flow doesn't make an effect happen once. It writes down what the program was told, and it leaves the decision about a call nobody heard back from to someone who knows the service. A deploy that kills flow doesn't necessarily stop what the library had already set going: a real client in that position may still be mid-request, and its payment can land after the crash, even after a lookup has found nothing. A lookup is evidence, not proof, and Unsettled exists for the times it comes back empty.

Nothing restarts a dead run on its own either; what a host does with calls that were out is the Reference's to say. It stays stopped on disk until your application notices, with flow runs here, and runs flow resume. A plain pkill, the signal a process supervisor usually sends, leaves the same record as pkill -9.

Temporal handles the same moment with activity retries and heartbeats: a worker that dies mid-activity has the activity retried on another worker, after a timeout, and the activity's own code is expected to be safe to repeat. Flow's default is the opposite, to repeat nothing and wait for you to say what happened, and it costs you a lookup or a decision on every interrupted call. For a refund that is a cost worth paying. For a call that is always safe to repeat, --resend is one flag.

Exercises

  1. Crash a run with PROVIDER=slow, and before resuming, delete payments/ledger.txt and payments/keys.txt as if the provider had never seen the request. Resume it with --resend. How many lines does the ledger have afterwards, and why is that the honest result?
  2. Crash a run and resume it with --resend, but edit provider.py first so it ignores idempotency_key. Count the lines in the ledger.
  3. A run is waiting on the manager's answer, held by the manager's library, when its machine restarts. Is that a crash in the sense of this chapter? Try it in 3-manager-answers-tomorrow: what does flow runs say, and what does the run need before it can go on?