Sessions and resume
A sync in irsync is a session, not a connection. The session is the job: the
plan of which files to send and how far each one got. A connection is only the
current way of moving it forward. When the connection drops, the session
waits; when the process dies, the session stays on disk; the next connection,
or the next run of the same command, continues it. This page explains what a
session holds, how it is found again, and what a resumed session skips.
What a session is
A session has a random 128-bit id, which the daemon assigns in its HelloAck,
a plan and a record of progress. Both sides keep the plan and the progress in
the index of the root they work on, under state_dir (see
Files and state), and write
them at most every 2 seconds and once more at the end:
- the plan: each file the receiver decided to request, with its action, size, hash and listing entry, and once the listing is over, a flag that the plan is complete, with the directories and the errors found on the way;
- the progress: the files acknowledged as done, the bytes transferred over all
connections, and the deletions so far, which count against
--max-delete.
Separately from the session, the receiver records for each partly received
file which byte ranges of its .part file it has verified, and how far it got
into chunks that did not finish. That record is written after every 64 newly
verified chunks or every 2 seconds, when the connection drops, and belongs to
the file, not to the session. The exact messages are in
Protocol.
A finished session’s record is deleted.
When the connection drops
The client dials again and sends Hello with the same session id. Both sides
load what they stored and carry on. A file acknowledged as done is not touched
again. A file that was partly received continues from its verified chunks: the
receiver reads each recorded chunk of the .part back, checks it against its
hash, and requests only the rest. Chunks that were written but not yet
recorded are requested again.
A chunk is up to 1 MiB, and it travels in pieces of 64 KiB. The receiver
writes each piece into the .part as it arrives, so a chunk that was cut off
continues after its last piece: the request says how many of its bytes the
receiver holds, and the sender sends only the rest. Once the chunk is whole,
the receiver checks it against its hash; if the check fails, it asks for the
whole chunk again. So a link that drops before a whole chunk crosses still
moves the file forward, one connection at a time.
The client redials with exponential backoff: 1 second, doubling up to 60
seconds, plus up to a quarter of random jitter, so that many clients do not
redial in step. It keeps going for --reconnect, 24 hours by default, measured
from the first dropped connection since the session last made progress: however
long a connection worked before it dropped, the client dials again at least
once. A connection that the daemon
accepts but that moves no data and places no file does not count as progress,
so a daemon that accepts and drops every connection cannot keep a client
retrying forever. --reconnect 0 gives up at once, as rsync does.
Some answers end the session instead of a retry: an Error from the daemon
(the key may not use the path, the export is read-only, another protocol
version), and close codes 1 to 3 (see
Protocol). A daemon that no longer knows
the session answers BAD_SESSION, and the client starts a fresh session at
once.
When the process dies
The client stores the session under a key derived from what the command means,
not how it was typed: the daemon’s EndpointId (so a server name and the raw id
are the same), both locations (a local one by its canonical path, a remote one
as typed, each with its trailing-slash rule),
the preserve flags, -c, -u, --inplace, --append or --append-verify,
the deletion mode with --delete-excluded, -n, and the filter rules in order.
Flags that do not change the result, such as --parallel, --bwlimit or
-v, are not part of the key.
The next run of the same command computes the same key, finds the stored
session and prints resuming session <id> from <time>, with the first eight
hex digits of the id and the time of the last write. A command that differs in
any part of the key starts its own session. There is one stored session per
key: a new one replaces an old one.
--no-resume ignores the stored session and plans afresh. Partly received
files are still picked up, since their records belong to the files: a .part
whose record names the same expected size and hash continues from its verified
chunks.
What the daemon checks
The daemon keys its copy of the session by the client’s EndpointId, the
export and the path inside it as resolved, the direction, the trailing-slash
rule, and the receiver-side options. A resume must match all of them.
Only the key that started a session may resume it. A Hello with the id of a
session that another key started, that is finished, or that the daemon has
collected, gets BAD_SESSION, with no hint which of these it was.
When a second connection resumes a session that an older connection still holds, as when the same command runs twice at once, the daemon closes the older one as superseded, waits for it to let go, and continues with the newer. The older client stops with an error rather than taking the session back.
What a resumed session skips
On a resume with a complete plan, neither side lists again: the receiver recreates the planned directories where they are missing and requests the files not yet done, and nothing else. This is what makes a resume after an interruption cheap for a large tree. Two cases list afresh even then: a run with deletions, because the stored plan holds no deletion candidates, and a dry run, which stores nothing worth resuming. They still skip the files the progress marks as done.
A resume with an incomplete plan lists both sides again and plans the files that are not done yet, using the index to skip unchanged ones.
How long a session waits
A session that nobody resumes is dormant. Garbage collection removes dormant
sessions and their partial files after dormant_session_timeout, 7 days by
default, on the daemon, and after 7 days on the client. A partial file that a
stored session still plans to write is kept. When collection runs is in
Clean up state.
The daemon also limits how many dormant sessions one client key may leave in
an export: max_dormant_sessions_per_key, 25 by default. Each time a session
of that key ends, the daemon counts the key’s dormant sessions in that export
and drops the ones that went longest without progress until the limit holds.
A session that a connection is using is never dropped and does not count. A
client that comes back for a dropped session gets BAD_SESSION and plans
afresh. The partial files stay, so that run still continues the files it had
begun. The client has no such limit for its own records.
Timeouts
Three timers decide when a connection is given up on, and each leads to a redial, not to the end of the session:
- QUIC’s idle timeout, 60 seconds, with keep-alives every 5 seconds: the path is gone. See Connections.
- The daemon’s
idle_timeout, 30 minutes by default: nothing has happened on the session for that long. - The client’s
--idle-timeout, oridle_timeoutunder[defaults], off by default: no control message, no data and no local progress for that long. It also bounds the wait for the daemon’s reply toHello.
Local progress (hashing, writing, finalizing, deleting) counts as activity, so
that a side busy with a large file does not look dead. The busy side tells the
other with a Heartbeat every 500 ms in which it made progress; an idle side
sends none. Since heartbeats come every half second, an --idle-timeout below a few
seconds is unreliable.