Skip to content

Frequent GC when dealing with a large number of small files #10868

Open

Description

@zz950115

What happened?

Frequent GC when dealing with a large number of small files

Syncthing version

v2.1.3

Platform & operating system

Linux arm

Browser version

No response

Relevant log output

INF GC was interrupted due to exceeding time limit (processed=141 runtime=5m0.40455622s folder=mjwtm-pthx5 fdb=folder.0002-w6raqcpf.db table=blocks rows=45542293 chunks=455 log.pkg=db/sqlite)

Activity

  1. added theissue type on Aug 24, 2026
  2. added
    bug
    needs-triage
    on Aug 24, 2026
  3. jhf commented on Oct 5, 2026

    @jhf

    Related, not the same hang. #10868 is GC hitting the time limit on a huge blocks table. On a 2.2M-file macOS sendreceive folder we also saw that GC log, but the pull starvation we actually measured was the deleted/ignored scan doing a SQLite virtual-mtime lookup on every local path.

    sample of the hung process (10s, non-destructive):

    folder.Serve
      scanTimerFired
        scanSubdirs
          scanSubdirsDeletedAndIgnored
            osutil.IsDeleted
              mtimeFS.load / sqlite GetMtime
    

    A second thread was in sqlite3_step / sqlite3BtreeTableMoveto the whole sample. Folder state=scanning, needFiles=2, peer connected, CPU ~1% after 66 minutes. Pulls never ran because that pass never finished.

    IsDeleted only needs existence/type/symlink. It was going through mtimeFS.Lstat, which loads the virtual mtime from SQLite on every path.

    Patch: fs.LstatExists skips only the virtual-mtime database and keeps case-conflict detection in the call path; osutil.IsDeleted and TraversesSymlink use it.

    Live on the same folder, patched v2.1.5-based build:

    15:32:11  scanning (~80-100% CPU)
    15:38:30  Completed initial scan (6.3 min, 1472 changes)
    15:38:33  Synced file roundtrip-121708.txt
    then idle, needFiles=0, stayed idle, no folder restart
    

    Unit tests: TestLstatExistsSkipsMtimeDatabase, TestIsDeletedSkipsMtimeDatabase.

    Branch on current main: https://github.com/jhf/syncthing/tree/fix/fs-skip-mtime-on-exists (1d2ed2f). Happy to open a PR if wanted. CONTRIBUTING says not to send unsolicited ones from new contributors, so leaving the branch here.

  4. jhf commented on Oct 5, 2026

    @jhf

    Follow-up: the pull starvation we measured is a different bottleneck than this GC time-limit. I opened a dedicated issue for the deleted/ignored scan doing a SQLite virtual-mtime lookup per file: #10909

    Branch for that scan-path change: https://github.com/jhf/syncthing/tree/fix/fs-skip-mtime-on-exists

    This folder also logs the GC interrupt from this issue (blocks ≈ 2.5M rows, 25 chunks, a few chunks in 5–13 minutes). That is still open and separate from #10909.

  5. jhf commented on Oct 5, 2026

    @jhf

    Related follow-up on the GC time-limit itself (the GC was interrupted due to exceeding time limit log), not the deleted/ignored scan hang in #10909.

    What it looks like on a real folder

    This folder today: ~1.4M blocklists, ~2.5M blocks, 7.3 GB folder.db. The actual orphan set measured standalone was 46 block rows and 1 blocklist. That is what GC is trying to find, and it does not get there:

    2026-10-02 04:50:35 INF GC was interrupted due to exceeding time limit (processed=11 runtime=5m1.590965083s  table=blocks rows=2520729 chunks=25)
    2026-10-02 13:19:17 INF GC was interrupted due to exceeding time limit (processed=6  runtime=5m11.690797916s table=blocks rows=2520683 chunks=25)
    2026-10-02 21:58:06 INF GC was interrupted due to exceeding time limit (processed=17 runtime=5m51.047097709s table=blocks rows=2521570 chunks=25)
    2026-10-03 06:43:29 INF GC was interrupted due to exceeding time limit (processed=10 runtime=5m7.603171042s  table=blocks rows=2521989 chunks=25)
    2026-10-03 15:10:22 INF GC was interrupted due to exceeding time limit (processed=11 runtime=5m26.332166417s table=blocks rows=2521989 chunks=25)
    2026-10-04 08:50:25 INF GC was interrupted due to exceeding time limit (processed=14 runtime=5m17.53278425s  table=blocks rows=2519592 chunks=25)
    2026-10-05 10:30:45 INF GC was interrupted due to exceeding time limit (processed=2  runtime=13m19.162883708s table=blocks rows=2518093 chunks=25)
    

    Two problems in those numbers: the 5 minute cap fires after 2–17 of 25 chunks, and the one transaction is rolled back, so the next run starts at zero.

    Standalone, the same predicate over the same index finishes quickly: orphan DELETE of 46 block rows in 13s, 1 blocklist row in 1s.

    Change

    File replace already knew the old blocklist_hash (INSERT OR REPLACE on files_device_name) and threw it away. The follow-up records those hashes in gc_retired_blocklists from Update/Drop, then GC only considers that set (still checking files so a hash used by another name or device is kept). Orphan blocklists are deleted first, then blocks by those hashes. Empty queue + seq moved skips the live-table scan. One full scan remains for databases that already had a GC sequence before this schema.

    SQLite has no FOR EACH STATEMENT trigger (only FOR EACH ROW), so this is done in the same Update() transaction rather than a trigger. The schema already noted per-row triggers were too expensive.

    Measured

    • TestRetiredGCCheaperThanFullScanPlan (400 blocklists): queue-bounded lookup 292µs vs full NOT EXISTS scan 2.86ms, ~10x.
    • Cost at write time is one indexed SELECT on files_device_name per updated file, plus a 32-byte INSERT OR IGNORE.
    • go test ./internal/db/sqlite green, including the original TestBlocklistGarbageCollection.

    Still a plan-scale claim, not a 2.5M-row live A/B: the retirement path is bounded by the queue size rather than by blocks/blocklists row count, which is the property that removes the time-limit failure mode. I can produce the live before/after once this has run a few GC cycles on the 7.3 GB index.

    Branches (current main)

    fix/scan-and-gc-clean is the branch to look at. It is current main plus the three
    commits above plus the LstatExists scan fix, with nothing else mixed in.

    The similarly named fix/scan-and-gc on that fork is only the local build branch:
    it adds one more commit pinning a fork of github.com/syncthing/notify to work
    around #10894, which is unrelated to this issue and not for review.

    One more fix that came out of testing this

    While verifying the queue on the real 7.3 GB index I found that the one-time
    full scan returned success even when the 5 minute limit stopped it, and the
    caller then cleared the "needs full scan" flag unconditionally. On a folder too
    large to finish that scan in one run, that silently stranded the orphans that
    predate the queue and never retried. The same failure mode this issue is about,
    reintroduced as a false success. The scan now reports whether it finished and
    the flag is only cleared when it did, with a test
    (TestBlocklistGCFullScanInterruptedKeepsFlag) verified to fail against the
    previous behaviour.

    Happy to open a PR, or split these into two, if wanted. CONTRIBUTING says not to send unsolicited ones.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugneeds-triage

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions