Good point.. I got briefly confused.

Okay, my theory is that the final write operation is still in progress when the program begins trying to lock the pages again. Dunno why, but nothing else explains it nicely.

So, put a sleep(1) there instead of the fsync().

And modify the lockpage error message to actually dump out what it read back, instead of just blindly complaining.

Cheers