[launchd-dev] Reading WatchPaths file into Virtual Memory attempts to spawn Program

Jerry Krinock jerry at ieee.org
Sun May 8 08:24:50 PDT 2011


I discovered behavior which causes *some* of the "sleepiness" I've reported in launchd WatchPaths. [1]

Consider a launchd agent which has WatchPaths.  If the program indicated by its ProgramArguments *reads* one of the watched files by invoking -[NSData dataWithContentsOfFile:options:error:] with options = NSMappedRead (aka NSDataReadingMapped), this file *reading* causes launchd to immediately try and re-launch the program.  Since the program is already running (I guess), it logs one of these:

com.apple.launchd.peruser.501  (MyJobLabel) Throttling respawn: Will start in <N> seconds

where <N> is a random number between the job's (ThrottleInterval - 60) and ThrottleInterval. [2]

I don't know why I was using NSMappedRead; it was some very old code.  NSMappedRead is "A hint indicating the file should be mapped into virtual memory, if possible".  When I changed that option to 0, the respawn no longer occurs.    

This is apparently one reason why launchd's WatchPaths sometimes goes to sleep on me after the job is run; it's been throttled.  But I think I've also seen WatchPaths go to sleep indefinitely when my ThrottleInterval was only 5 minutes, so there's probably more to the story.

The documentation for launchd's WatchPath states "This optional key causes the job to be started if any one of the listed paths are modified."  I think that the word "modified" used in the context of a file is rather vague, since there are two data forks and different kinds of metadata.  Does mapping a file into virtual memory constitute "modified"?  Should I file a bug on either the documentation or this behavior?

Thanks,

Jerry Krinock

[1] http://lists.macosforge.org/pipermail/launchd-dev/2011-May/000916.html

[2] One would think that the random-ness might be related to the clock seconds when the read occurs, but it is not.  The "Throttling respawn" message performs as stated; the program is indeed relaunched in <N> seconds.  This message seems to be logged whenever one of the WatchPaths is touched while the ProgramArguments program is running.  But it is not logged if one of the WatchPaths is touched multiple times before the program is launched.  Rather strange.



More information about the launchd-dev mailing list