As long as cwd is open()ed after locking, it should be close()d before unlocking.  Not that this is likely to be a problem, but resource lifetimes should be nested.