# lockfiles & fork()

4 messages from 2006-10-28 to 2006-10-28. Participants: Johannes Schindelin, Junio C Hamano, Linus Torvalds, Florian Weimer.
Thread: https://gitlist.dev/t/43180

## Johannes Schindelin, 2006-10-28 01:21

Subject: lockfiles & fork()
Message-ID: <Pine.LNX.4.63.0610280319250.26682@wbgn013.biozentrum.uni-wuerzburg.de>
URL: https://gitlist.dev/e/Pine.LNX.4.63.0610280319250.26682%40wbgn013.biozentrum.uni-wuerzburg.de

```
Hi,

when you setup a lockfile, and then fork(), you could easily end up with 
atexit() kicking in to remove the lockfile, before the main process has a 
chance to commit. (Yes, I need to hold the lock long before the fork()).

Any ideas how to solve the problem?

Ciao,
Dscho

```

## Junio C Hamano, 2006-10-28 01:41

Subject: Re: lockfiles & fork()
Message-ID: <7v64e5cj39.fsf@assigned-by-dhcp.cox.net>
URL: https://gitlist.dev/e/7v64e5cj39.fsf%40assigned-by-dhcp.cox.net
In-Reply-To: <Pine.LNX.4.63.0610280319250.26682@wbgn013.biozentrum.uni-wuerzburg.de>

```
Johannes Schindelin <Johannes.Schindelin@gmx.de> writes:

> when you setup a lockfile, and then fork(), you could easily end up with 
> atexit() kicking in to remove the lockfile, before the main process has a 
> chance to commit. (Yes, I need to hold the lock long before the fork()).
>
> Any ideas how to solve the problem?

Depends on the arrangement between the parent and child
regarding who is responsible for unlocking.  If child expects to
work under the safety of the lock the parent prepared, and also
relies on the parent to wait for it and then unlock to ensure
that safety for the child, then I think the problem is simple.

How about defining a function in lockfile.c to assign NULL to
lock_file_list and call that immediately after fork() in the
child?

```

## Linus Torvalds, 2006-10-28 01:52

Subject: Re: lockfiles & fork()
Message-ID: <Pine.LNX.4.64.0610271851130.3849@g5.osdl.org>
URL: https://gitlist.dev/e/Pine.LNX.4.64.0610271851130.3849%40g5.osdl.org
In-Reply-To: <Pine.LNX.4.63.0610280319250.26682@wbgn013.biozentrum.uni-wuerzburg.de>

```


On Sat, 28 Oct 2006, Johannes Schindelin wrote:
> 
> when you setup a lockfile, and then fork(), you could easily end up with 
> atexit() kicking in to remove the lockfile, before the main process has a 
> chance to commit. (Yes, I need to hold the lock long before the fork()).

The easiest way to handle this would be for the lockfile logic to just 
save the pid associated with each lockfile... And for the atexit function 
to just ignore it if the pid doesn't match. 


```

## Florian Weimer, 2006-10-28 13:30

Subject: Re: lockfiles & fork()
Message-ID: <87fyd8ftzg.fsf@mid.deneb.enyo.de>
URL: https://gitlist.dev/e/87fyd8ftzg.fsf%40mid.deneb.enyo.de
In-Reply-To: <Pine.LNX.4.63.0610280319250.26682@wbgn013.biozentrum.uni-wuerzburg.de>

```
* Johannes Schindelin:

> when you setup a lockfile, and then fork(), you could easily end up with 
> atexit() kicking in to remove the lockfile, before the main process has a 
> chance to commit. (Yes, I need to hold the lock long before the fork()).
>
> Any ideas how to solve the problem?

I think you should call _exit in the child instead of exit.

```
