There isn't a way to open and lock a file at the same time as far as these primitives are concerned (although there are file sharing modes which are different concepts).

I don't see how it can result in a race condition:
* File opening/creation is atomic. If the file does not exist, it is created.
* Opening a file will succeed even when the file is locked by another process.
* Locking a file will succeed even when the file is opened by another process.
* The blocking will occur on the `.lock()` call.