Subject: Re: bug#13079: 24.3.50; Emacs cannot create subprocess

From: Eli Zaretskii <[email-masked]>
Date: Thu, 06 Dec 2012 21:04:54 +0200

> Date: Thu, 6 Dec 2012 16:18:10 +0800
> From: Li Zhai <[email-masked]>
> Cc: [email-masked]
> 
> I've tried another method, using the flag FILE_FLAG_DELETE_ON_CLOSE
> while open the files.
> 
> Create a file by passed the flag FILE_FLAG_DELETE_ON_CLOSE to
> CreateFile, if all the handles are closed, the file will be deleted
> automatically. I wrote a very simple program to test that feature. It
> seems works.
> 
> #include <fcntl.h>
> #include <sys/types.h>
> #include <sys/stat.h>
> #include <io.h>
> #include <stdio.h>
> #include <windows.h>
> 
> void main( void )
> {
>    HANDLE fh1, fh2;
>    char buf[100];
>    DWORD dwReadSize;
>    DWORD dwWriteSize;
> 
>    fh1 = CreateFile("temp.txt", GENERIC_READ | GENERIC_WRITE,
>                     FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
>                     NULL, CREATE_ALWAYS,
>                     FILE_ATTRIBUTE_NORMAL | FILE_FLAG_DELETE_ON_CLOSE, NULL);
> 
>    WriteFile(fh1, "hello", 5, &dwWriteSize, NULL);
> 
>    fh2 = CreateFile("temp.txt", GENERIC_READ | GENERIC_WRITE,
>                     FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
>                     NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
> 
>    if (fh2 == INVALID_HANDLE_VALUE)
>        perror( "Open failed on fh2" );
> 
>    printf("Close the first file handle...\n");
>    CloseHandle(fh1);
> 
>    printf("Read file...\n");
>    ReadFile(fh2, buf, 5, &dwReadSize, NULL);
>    buf[5] = 0;
>    printf("File content:\n %s\n", buf);
> 
>    _getch();
> 
>    printf("Close the second file handle...The file should be deleted\n");
>    CloseHandle(fh2);
> }
> 
> 
> But this method has some limit. Firstly, all the files must
> create/open by Win32 API, the codes are totally unportable. Secondly,
> the file must be opened with a flag FILE_SHARE_DELETE, or the file
> will not be deleted.
> 
> And I don't know how to integrate the Win32 API with the emacs codes.
> Emacs codes use POSIX functions(_open, _close, _read...), and the
> delete_on_close feature required WIN32 API. I've tried using
> _O_TEMPORARY flags with _open function, but not succeeded.

Yes, this technique will not help, for all the reasons you mention,
and then some.  For starters, in our case the problem is not with
Emacs holding the file open -- the call to write-region within
call-process-region closes the file before it returns -- but with the
child process that has the file connected to its standard input.  So
another process is involved here, not just Emacs.

Also, Emacs always uses emacs_open to open any files, which means it
doesn't know which files are temporary and which aren't.  Obviously,
we cannot open all files with FILE_FLAG_DELETE_ON_CLOSE or
_O_TEMPORARY, because most files Emacs writes should _not_ be deleted
when Emacs closes them.

Anyway, I think the solution to this should be to somehow retry the
deletion later, when the child process exits.  Nothing else will
resolve this bug.