Skip to content

Commit

Permalink
Tweak 868584d so agent waits first, then does the work
Browse files Browse the repository at this point in the history
  • Loading branch information
hschmidt committed Jan 8, 2017
1 parent 210d351 commit 56a2e8c
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions EnvAgent/EnvAgent.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@
int main( int argc, const char **argv )
{
NSLog( @"Started agent %s (%u)", argv[ 0 ], getpid() );
/*
* Work around weird issue with launchd starting the agent a second time if it finishes within
* 10 seconds, the default ThrottleInterval. We reduce the ThrottleInterval to 1s in the plist
* and wait a little longer here to avoid hitting that condition. We wait first, before doing
* any real work in order to consolidate potential bursts of changes to the environment plist.
*
* But let's not kid ourselves, this is still racy (a flaw inherent to launchd's WatchPaths
* mechanism) and we could miss updates if they happen after the plist is read and before
* launchd recognizes the agent's termination.
*/
[NSThread sleepForTimeInterval: 1.1];

NSError *error = nil;
NSFileManager *fileManager = [NSFileManager defaultManager];
Expand Down Expand Up @@ -93,10 +104,6 @@ int main( int argc, const char **argv )
NSLog( @"Failed to unload agent (%@)", agentLabel );
}
}
// Work around weird issue with launchd starting the agent a second time if it finishes within
// 10 seconds, the default ThrottleInterval. We reduce the ThrottleInterval to 1s in the plist
// and wait a little longer here to avoid hitting that condition.
[NSThread sleepForTimeInterval: 1.1];
NSLog( @"Exiting agent %s (PID %u)", argv[ 0 ], getpid() );
return 0;
}
Expand Down

0 comments on commit 56a2e8c

Please sign in to comment.