Skip to main content

Watching the library folders

Not released yet (comes after 1.7.0)

New photos appear without a scan by hand (user guide). The idea in one sentence: a file event is only a hint that names a folder; a task then compares that folder with the library and starts the normal scan with exactly the files that differ.

So nothing depends on the order, the number or the kind of events. A repeated event costs a compare, a lost one is made good by the next look at the whole folder, and an event for a file LibrePhotos wrote itself finds nothing to do. There is no second way to import: the scan, its groups (RAW and JPEG, Live Photo, XMP, Takeout JSON), its moves and its drive rules are the ones of the button.

The parts​

PartWhereWhat it does
Eventsapi/runtime/folder_events.pyOne recursive watch per library folder: ReadDirectoryChangesW, FSEvents or inotify, through watchdog. Calls changed(path), came_or_went(path) or lost()
Batchapi/directory_watcher/event_batch.pyTurns paths into folders (folders: their files; trees: everything below) and waits for quiet
Watchapi/directory_watcher/folder_watch.pyOne thread. Follows the users, their folders and the drives, starts dead watches again, plans the looks, hands a batch over
Placesapi/directory_watcher/event_places.pyWhether events can be trusted where a folder is
Checkapi/directory_watcher/library_check.pyThe task check_library(user, folders, trees): compare, then queue scan_photos(scan_files=...)
Surveyapi/directory_watcher/library_survey.pyWhat is on the disk, from one directory listing per folder, with the rules of the scan's walk

One watch per installation​

The thread runs in the pool process of manage.py run_workers. Pool.watch_folders starts it, and starts it again within 10 seconds when it ended. There is one pool per installation, in Docker and in the Windows and Mac program, and it is the process that does not serve requests. There is no database lock for it on purpose: PgBouncer breaks session locks, and a second watch would only queue the same compare twice.

folder_watch is imported when the thread starts. With the switch off the thread reads the switch once a minute and keeps its database connection (close_if_unusable_or_obsolete), so an installation with the switch off pays one small query a minute.

From events to a check​

A batch is handed over when nothing happened for 10 seconds, or after 5 minutes of steady work, so a copy of thousands of photos becomes a few scans. It holds at most 1,000 places; after that it is the whole library folder. It is not handed over while a check of that user waits or while a scan, a folder change, a content check or the emptying of the trash runs. It keeps collecting then. The check of a file that is still being written does not count (see below), so a recording that is never done holds up nothing else.

Events that cannot matter are dropped at once: hidden files, files with other extensions, the program's own data, the subfolders a user left out.

The check​

check_library lists the folders with os.scandir, which gives name, size and time without opening a file, and compares them with the record of the last read (File.indexed_mtime_ns and indexed_sidecar_mtimes, see Scan grouping). The groups that differ go into one job Scan photos, at most 10,000 files, and a second check is queued in the same transaction to run after it. That check finds the rest, and what changed while the scan ran.

  • Still being copied. A file that changed in the last 10 seconds, or that another program has open for writing (Windows), is left for a later check of its folder: after 15 seconds, then 30, then every minute. That check has a key of its own for each folder (unsettled_key) and is not a waiting check (check_waiting). A check asks for it only when none of that folder is queued (waiting_dedupe_keys): one that is already at work may have listed the folder before the last write. A check that starts a scan asks for none; the check after the scan does, so a long copy keeps one chain of checks.
  • Our own writes. record_own_write updates the record in the same step as the write, so a rating written into a file compares as unchanged.
  • Broken files. A file that a scan has tried and that still differs is put into LibraryLook.given_up by its name, size and time, and is tried again when one of them changes. So is a scan the user cancelled. Without this, one broken picture would start scans for ever. A look at the whole library tries them again once a day (LibraryLook.retried_at), so a file on a share that never comes back as a drive is not given up for good.
  • A drive that was away. A scan that ran while a drive was unplugged proves nothing about its files. LibraryLook.back_at is the last return of a drive; what was tried before it does not count, and the list is cleared. Only a folder whose own drive changed is watched again: drive_places() is compared entry by entry (drive_entry), so plugging in a card, or a Time Machine snapshot that macOS mounts under /Volumes, leaves the other drives alone.
  • A folder that is not there. Each library folder that holds a place of the check is judged for itself, with OtherDrives and library_root_problem; the others are not asked, so a check of a local folder neither wakes a NAS nor waits for a dead mapped drive. One that is unplugged, unmounted or on another drive is left out; a new photo in a folder that is there comes in all the same.
  • Only in the cloud. Files with FILE_ATTRIBUTE_RECALL_ON_DATA_ACCESS, RECALL_ON_OPEN or OFFLINE (Windows) or SF_DATALESS (macOS) are passed by. A scan by hand reads them, and the cloud program then downloads them.
  • Left out. Hidden folders, the skip patterns, the subfolders a user left out, the program's own data, and in the desktop program ~/Library and AppData.

A deleted file is not marked missing here. That is done by the scan of the whole library.

Looks at a whole folder​

Events say nothing about the time LibrePhotos was closed. So 60 seconds after a watch started, the whole folder is compared once. It waits for a computer that is less than half busy, but no longer than 10 minutes. Then:

PlaceHow it is knownEventsLook
locala fixed or removable disk; on Linux a known local file systemyesonce a day, for an external drive only when it comes back
virtualanything else, such as fuse.*, 9p or virtiofs of Docker Desktop, or a share mounted inside a local libraryyes, not trustedevery hour
networka remote drive or UNC path, cifs, nfs, ...noevery 6 hours
limitedinotify refused (ENOSPC, EMFILE)noevery hour, and the watch is tried again every 5 minutes; library_watch_limited shows the notice in Site settings
awaythe folder is not therenonone; tried again every minute

A library with linked folders gets a look every hour, because events do not follow links. A network share is not asked between two looks, so a NAS can sleep.

inotify can also run out after the start, for a new folder such as a camera import. watchdog then only logs Unable to watch and goes on without that folder. OutOfWatches in folder_events.py is a filter on that logger: it takes the line (it names a path), ends the watch, and the watch starts again as limited, with the notice. A logger filter only sees what the logger's level lets through, and LOG_LEVEL (CRITICAL in the test settings) can be above WARNING, so HeardWarnings lets that logger through WARNING while a watch holds it and puts its level back when the last watch ends. The handlers keep their own level, so nothing more is written to the log.

Ejecting a drive on Windows​

An open directory handle blocks "Safely remove hardware". api/runtime/windows_eject.py asks Windows for the notice (CM_Register_Notification on the handle) and ends the watch when the drive is to be removed or its volume is to be locked. When the notice cannot be had, the drive is not watched but looked at every hour. The watch tries again after a minute, so a removal that failed does no harm. macOS and Linux need nothing: FSEvents holds no file open, and a removed inotify watch does not keep a file system busy.

Which library​

Compared in December 2026, both for Python 3.14:

watchdog 7.1.1watchfiles 1.3.0
LicenceApache-2.0MIT
WheelsWindows x64 and arm64, macOS arm64 and x64, manylinux x64 and arm64; about 130 KBthe same; 260 to 450 KB
Lost eventsreported on all three systemsnot passed on
A name that is not valid Unicodeworksends the watch
A removed library folderreporteddepends on the system
Typesincludedincluded

Lost events decided it. Neither was installed before. watchdog is used without its observer and dispatcher threads: the three emitter classes are subclassed, so each watch is one thread (two on Windows) that calls the hints directly. Nuitka follows the import, and the Wolfi image takes the manylinux wheel.

What it costs​

Measured on a developer machine (Windows 11, NVMe disk) and in Ubuntu 26.04 on WSL:

WhatResult
10,000 folders on Linuxwatches set in 0.9 s, 7 MB of memory, no measurable CPU while idle
One library folder on Windows, any number of folderswatch set in 0.01 s, 0.3 MB, no measurable CPU in 20 idle seconds
The pool process with the watch and what it imports11 MB more
5,000 files of 200 KB copied in 9 s on Windows15,000 events, none lost, 2 s of CPU
Look at 100,000 unchanged files in 1,000 folders on Windows, database included12 s of one CPU, up to 80 MB while it runs

Up to 1.7.0 the walk of a scan asked Windows three times about every file; it now uses what the listing already said. That made the walk of these 100,000 files four times as fast (27 s before, 7 s now) for every scan.

Tests and the model​

verification/scanning/LibraryWatch.tla checks that every change is in the library in the end and that it comes to rest, with writes at any moment, lost events, an unplugged drive and a kill at any step. It has two kinds of checks that may run at the same time, the one of a hand-over or after a scan and the one of a file still being written, and another folder whose file is never done. Nine configurations break one rule each and fail.

api/tests/infra/test_folder_events.py makes, renames and deletes real files. .github/workflows/folder-events.yml runs it on Windows, macOS and Linux, on x64 and arm64, and on Windows also ejects a virtual disk that is being watched.