Book a call

Field notes ยท lab reproduction

Your File Copy Has an Audience

Nothing fails. The copy just gets slower with every process that watches the folder above it.

Picture a job on a Windows server that copies an application folder into a shadow folder every few minutes. The next step of the job starts only when the copy has finished, so the copy sets the pace. Other processes on the server watch the folder tree that holds the shadow folder, the way sync and indexing tools do, so they can react when a file changes. Over the months the copies get slower. Nothing fails and the logs stay clean. The usual suspects are the antivirus, the file system and the job's own code, and none of them explains why only one folder is slow.

The job is hypothetical; our lab is real. It ran on Windows 11 build 26300 with .NET 8.0.34, and Microsoft Defender was on in every run. In the lab, a copier is a thread that copies files one at a time with plain CopyFileW calls. A watcher is one request to Windows, made with ReadDirectoryChangesW, to be told about changes in a folder and its subfolders; when Windows answers, the watcher renews the request at once. We copied the same files into two folders on the same volume: INSIDE, a folder under the watched folder, and OUTSIDE, a folder outside it. We used 1, 8 or 32 copiers and up to 470 watchers.

The short version

  • Copies into the watched folder slow down. With 32 copiers, adding 470 watchers raised the median time to copy one file into the watched folder from 0.8 ms to 4.6 ms. Copies into the folder outside it went from 0.7 ms to 0.9 ms.
  • The copies queue for a lock. In one run we traced, the copier threads spent about 94% of their time waiting for a lock on the path NTFS uses to report directory changes. In 97% of that waiting time, the copier was released by a watcher renewing its request.
  • Defender appears in the waits, but we have not proven it is the cause. We did not run the lab with Defender off.

Copies into the watched folder slow down

With no watchers, copies into both folders took about the same time: the INSIDE median was between 0.9 and 1.2 times the OUTSIDE median. With 1, 8 or 32 copiers alike, each step up in watchers made INSIDE copies slower. OUTSIDE copies stayed between 0.27 and 0.92 ms per file.

median copy time per file, ms (32 copiers)INSIDE the watched folderOUTSIDE, same volumestraight-line projection, not measured024681002004006008001,000watchers on the watched foldermeasuredprojected0.92 ms if it stays flat4.64 ms at 470 watchers4.8 ms at 5008.9 ms at 1,000
Figure 1. Median time to copy one file with 32 copiers, into the watched folder (INSIDE) and outside it (OUTSIDE). Solid lines join measured points, each the mean of two runs. The dashed lines are not measurements: the INSIDE line extends to 1,000 watchers the straight line that best fits the four measured points with watchers, and the OUTSIDE line assumes the value at 470 stays flat. Nothing past 470 watchers was measured.

With 470 watchers, the median time to copy one file was (mean of two runs):

CopiersINSIDE (ms)OUTSIDE (ms)INSIDE is slower by
11.210.314.0x
82.400.793.0x
324.640.925.0x

The whole copy followed the median with 1 and 8 copiers. With 470 watchers, one copier needed 2.8 s for its 2,000 files inside the watched folder and 0.8 s outside it. Eight copiers needed 6.1 s inside and 2.5 s outside. With 32 copiers on 12 processors, the total did not follow the median: with 470 watchers, the copy outside the watched folder took longer, 19.1 s against 13.4 s for the copy inside it (mean of two runs). We did not find out why.

INSIDE median divided by OUTSIDE median1 copier8 copiers32 copiers0123451.0: both destinations take the same time050150300470watchers on the watched folder4.01 copier3.08 copiers5.032 copiers
Figure 3. How many times slower INSIDE copies were than OUTSIDE copies, as watchers are added, for 1, 8 and 32 copiers. Each median is the mean of two runs. A ratio of 1.0 means equal times.

The copies wait for the watchers

To see what the copies wait for, we recorded an ETW trace with stacks of one run: 8 copiers and 470 watchers, each watcher in its own process. The copier threads spent 94.1% of their time waiting for a fast mutex, a kind of kernel lock, always at the same place in the code. The wait sits at the end of one call chain: NTFS reports a directory change (NtfsReportDirNotify), the report goes to FsRtlNotifyFilterReportChangeLiteEx, and that function waits in ExAcquireFastMutexUnsafe.

Four NTFS operations led to that wait, each with about a quarter of the lock wait time: creating a file or folder, setting its size (end of file), setting its timestamps and attributes (basic information), and the cleanup when a handle closed. Each completed copy waited a little over twice on average: the trace holds 14,167 of these waits inside CopyFileW for 6,261 completed copies. In 97% of the lock wait time, the thread that released the copier was a watcher renewing its request.

Microsoft's driver documentation for FsRtlNotifyFilterReportChange describes one notify list per volume, guarded by a synchronization object. Our reading of the stacks is that each of those file operations reports its change to that list under one lock, and that each watcher takes the same lock when it renews its request. That the reports and the renewals compete for this lock is our inference.

one copier thread, 470 watchers: 120 ms of the trace, to scalecreate10.8 msset size11.0 msset times10.9 msset times10.6 mscleanup10.6 mscreate10.7 msset times10.6 mscreate10.8 msset size10.9 msset times13.2 ms0 ms20406080100120 mswaiting for the notify lockeverything elsea watcher renews its request and releases the lockone whole copy, median, on the same time scale (8 copiers)0.73 ms0 watchers2.40 ms470 watchers, ten per process (the table)12.8 ms470 watchers, one process each (single run)23.1 msthe same, while tracing (the run above)
Figure 2. 120 ms of one copier thread in the traced run, to scale. Each shaded block is one wait for the notify lock, from the moment the thread stops to the moment it runs again. Blocks wide enough carry the NTFS operation that waited and the length of the wait. The last block continues past the end of the excerpt. We chose a typical excerpt: of the 444 windows of this thread that start at a file-creation wait, this one has the median share of lock wait. The bars below compare, on the same scale, the median time of one whole copy in different runs.

Defender sits on the path

In the traced run, Defender's file system filter driver, WdFilter.sys, was on the stack of 3,564 lock waits, 24.9% of the lock wait time. All of them were cleanup waits. In 3,563 of them, a handle was closed while the copier thread was still inside its own call to open a file (NtCreateFile), with WdFilter.sys on the stack. We did not identify which handle was closed. The copier threads waited inside Defender's driver itself only 25 times, for 0.5 ms in total: the waiting happened at the notify lock.

Defender also used more CPU as watchers were added, and only for INSIDE copies. With 32 copiers, the CPU time of Defender's process during the copy rose from 82.4 s with no watchers to 114.1 s with 470 (mean of two runs). For OUTSIDE copies it stayed between 79.6 and 85.2 s.

That is as far as the lab goes. We did not run it with Defender off, so two questions stay open: how much of the slowdown is Defender's, and whether Defender's own file operations caused the extra change reports.

Two traps when you reproduce this

  • With many watchers and copiers, most answers are overflows. Microsoft documents that an answer with zero bytes, or the error ERROR_NOTIFY_ENUM_DIR, means the buffer could not hold all the changes, and that the program should then list the folder to find out what changed. Our watchers used a 4 KB buffer, renewed at once and never listed the folder. With 470 watchers and 32 copiers, about 550,000 of the roughly 663,000 answers per INSIDE run were overflows (mean of two runs). With one copier there were almost none.
  • How you spread the watchers over processes changes the numbers. With 200 watchers and 32 copiers, the median copy took 8.6 ms with each watcher in its own process, 2.1 ms with ten watchers per process, and 1.1 ms with all 200 in one process (one run each; the first stopped at the 20 s limit). The runs in the table used ten watchers per process; the traced run used one.

Check your own services

If you suspect this on your own servers, measure three things before you change anything, with as many parallel copies as your service makes:

  1. The time of the whole copy and the median time to copy one file, with and without the watching processes. Measure both: in the lab, with 32 copiers, the median showed the slowdown and the total did not. The 95th percentile hid it too, staying between 14.9 and 19.7 ms whatever the number of watchers (mean of two runs).
  2. The CPU time of Defender's process, MsMpEng.exe, during the same copy.
  3. What the copy threads wait for. Record an ETW trace with stacks using Windows Performance Recorder and look for waits under NtfsReportDirNotify.

The lab measured the copy and nothing else, so first check that the copy is the slow part of your service.

How we ran this

Each watcher made overlapped ReadDirectoryChangesW calls on the watched folder and its subfolders, with a 4 KB buffer and a notify filter of 0xFDB, and renewed the call as soon as it completed. Each watcher process received its answers through one I/O completion port. Each copier copied all 2,000 generated files, 2 to 8 KB each, creating folders with CreateDirectoryW and copying files with CopyFileW. We ran every combination of 0, 50, 150, 300 and 470 watchers, 1, 8 and 32 copiers and the two destinations, twice each. A run stopped after 20 s if it had not finished. Every lab process was limited to 12 logical processors, ran at BelowNormal priority and used the workstation garbage collector (DOTNET_gcServer=0).

Limits of this lab

  • The lab was a console application that only copied and watched. A real service does more.
  • Defender stayed on, and we did not vary its settings or record its exclusions. Its CPU time covers the whole MsMpEng.exe process while the copier ran, including anything else Defender did in that time. Microsoft Defender for Endpoint's sensor, MsSense.exe, also ran during the traced run, and we did not measure its effect.
  • The INSIDE folder sat two levels deeper than the OUTSIDE one. Other programs on the machine may also have watched the volume; we did not control them, so "no watchers" means none of ours.
  • We traced a single run, with 8 copiers and one process per watcher, and both the trace and that layout slowed it. Its median copy took 23 ms, against 12.8 ms for the same layout without the trace (a single run) and 2.4 ms in the table above, where watchers ran ten per process. Read the trace for where the threads wait, and the table for how long the copies take. The run stopped at its 20 s limit with 6,261 of its 16,000 copies done.
  • The numbers apply to this lab setup only.