Well, I mention this every time I see flock() being used. You introduce a race condition here. Please take a look at nodes
(I had got cut off so used three nodes, sorry). Personally, I would change the subroutine to use a semaphore (sentinal, or whatever you like to call it) file to avoid any race conditions.