Skip to content

Commit 38307bb

Browse files
bwijenGit for Windows Build Agent
authored and
Git for Windows Build Agent
committed
Make sure temporary file handles are not inherited by child processes
When testing a merge driver which spawns a merge server (for future merges) I got the following error: Rename from 'xxx/.git/index.lock' to 'xxx/.git/index' failed. Should I try again? (y/n) Only after I stop the merge server the lock is released. This is caused by windows handle inheritance. Starting childs with bInheritHandles==FALSE does not work, because no file handles would be inherited, not even the hStdXxx handles in STARTUPINFO. Opening every file with O_NOINHERIT does not work, Since it is used by git-upload-pack for example, which expects inherited handles. This leaves us with only creating temp files with the O_NOINHERIT flag. Which (currently) only used by lock_file which is exactly what we want. Signed-off-by: Ben Wijen <[email protected]>
1 parent e34aade commit 38307bb

File tree

2 files changed

+5
-1
lines changed

2 files changed

+5
-1
lines changed

compat/mingw.h

+4
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ typedef int pid_t;
6767
#define F_SETFD 2
6868
#define FD_CLOEXEC 0x1
6969

70+
#if !defined O_CLOEXEC && defined O_NOINHERIT
71+
#define O_CLOEXEC O_NOINHERIT
72+
#endif
73+
7074
#ifndef EAFNOSUPPORT
7175
#define EAFNOSUPPORT WSAEAFNOSUPPORT
7276
#endif

tempfile.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ int create_tempfile(struct tempfile *tempfile, const char *path)
120120
prepare_tempfile_object(tempfile);
121121

122122
strbuf_add_absolute_path(&tempfile->filename, path);
123-
tempfile->fd = open(tempfile->filename.buf, O_RDWR | O_CREAT | O_EXCL, 0666);
123+
tempfile->fd = open(tempfile->filename.buf, O_RDWR | O_CREAT | O_EXCL | O_CLOEXEC, 0666);
124124
if (tempfile->fd < 0) {
125125
strbuf_reset(&tempfile->filename);
126126
return -1;

0 commit comments

Comments
 (0)