Bug #21884 Instance Manager: PID file is created in non-atomic way
Submitted: 28 Aug 2006 18:33 Modified: 5 Oct 2007 15:17
Reporter: Alexander Nozdrin Email Updates:
Status: Unsupported Impact on me:
None 
Category:Instance Manager Severity:S3 (Non-critical)
Version:5.0.25BK OS:Any
Assigned to: CPU Architecture:Any

[28 Aug 2006 18:33] Alexander Nozdrin
Description:
PID file is created by three operations:
  - open file;
  - write PID;
  - close file.

That leads to the race condition if some other application waits
for Instance Manager to start. The problem is that the application
waits for the presence of the pid file. So, when the file is available,
the application reads it, but the file can be empty.

According to the code, the same problem should also affect MySQL server.

How to repeat:
This bug is hard to repeat because it is a race condition.
However it can be reproduced on slow hosts with slow filesystems.
In order to repeat it, one should start something like
'while true; do ls -l <path-to-im-pid-file>; done'
in one terminal and start Instance Manager in another.

The problem is that sometimes, there 'ls -l' will show
file with zero length.

Suggested fix:
Create PID file atomically: create temporary file and then
rename it to the PID file.
[28 Aug 2006 19:32] Konstantin Osipov
Of course atomic create is very robust, but I'd say the pattern when one assumes that the file is not empty once it exists is broken too.
I wonder if any other UNIX application does atomic creation of its pid file.