comparison mercurial/cmdutil.py @ 19868:0532c8f8e911

cmdutil.service: move pidfile writing to the parent in daemon mode There is a potential race here, which I suspect I've spotted in the wild, where something reads the pid file after the parent exits but before the child has had a chance to write to it. Moving writing the file to the parent causes this to no longer be an issue.
author Siddharth Agarwal <sid0@fb.com>
date Wed, 02 Oct 2013 15:20:49 -0700
parents edce20ebe1f3
children df91e2df6ba3
comparison
equal deleted inserted replaced
19867:edce20ebe1f3 19868:0532c8f8e911
495 def condfn(): 495 def condfn():
496 return not os.path.exists(lockpath) 496 return not os.path.exists(lockpath)
497 pid = util.rundetached(runargs, condfn) 497 pid = util.rundetached(runargs, condfn)
498 if pid < 0: 498 if pid < 0:
499 raise util.Abort(_('child process failed to start')) 499 raise util.Abort(_('child process failed to start'))
500 writepid(pid)
500 finally: 501 finally:
501 try: 502 try:
502 os.unlink(lockpath) 503 os.unlink(lockpath)
503 except OSError, e: 504 except OSError, e:
504 if e.errno != errno.ENOENT: 505 if e.errno != errno.ENOENT:
509 return 510 return
510 511
511 if initfn: 512 if initfn:
512 initfn() 513 initfn()
513 514
514 writepid(os.getpid()) 515 if not opts['daemon']:
516 writepid(os.getpid())
515 517
516 if opts['daemon_pipefds']: 518 if opts['daemon_pipefds']:
517 lockpath = opts['daemon_pipefds'] 519 lockpath = opts['daemon_pipefds']
518 try: 520 try:
519 os.setsid() 521 os.setsid()