pidfile: use "with" to close file immediately