Subject: Re: File locks for data directory lockfile in the context of Linux namespaces

From: Dmitry Dolgov <[email-masked]>
Date: Fri, 10 Jul 2026 12:20:13 +0200

> On Mon, Jul 06, 2026 at 03:07:35PM -0700, Zsolt Parragi wrote:
> +		if (errno == EAGAIN)
> +			ereport(FATAL,
> +					(errcode(ERRCODE_LOCK_FILE_EXISTS),
> 
> According to fcntl.2, this should handle both EACCESS and EAGAIN:
> 
> ERRORS
>        EACCES or EAGAIN
>               Operation is prohibited by locks held by other processes.

Good point. From what I see after a cursory look at fcntl is that it
normally returns EAGAIN, but filesystems are allowed to implement a
custom lock operation, so it makes sense to be prepared.

> +static int
> +OFDLockFile(int fd, const char *filename)
> +...
> +	else
> +		return dup(fd);
> 
> Isn't this missing an FD_CLOEXEC, so that launched processes doesn't
> inherit it and keep the lock open possibly longer than needed?

This is an interesting question. I haven't thought about this
originally, but now I think the current approach (no FD_CLOEXEC) is what
is actually needed. We want to keep the lock as long as any existing
process may access the data directory, thus the lock lifetime must be
equal to the lifetime of a longest living process. Currently the lock
file is created by the bootstrap process and the postmaster, which I
think fits the picture.

> Also, shouldn't the code verify the result of dup? (!= -1 / errno)