Subject: bug#60996: 29.0.60; Native compile fails to remove temp file for trampoline

From: Eli Zaretskii <[email-masked]>
Date: Tue, 24 Jan 2023 14:56:00 +0200

> From: Andy Moreton <[email-masked]>
> Date: Tue, 24 Jan 2023 01:18:01 +0000
> 
> >>       /* If in this session there was ever a file loaded with this
> >> 	 name, rename it before loading, to make sure we always get a
> >> 	 new handle!  */
> >>       Lisp_Object tmp_filename =
> >> 	Fmake_temp_file_internal (filename, Qnil, build_string (".eln.tmp"),
> >> 				  Qnil);
> >>       if (NILP (Ffile_writable_p (tmp_filename)))
> >> 	comp_u->handle = dynlib_open_for_eln (SSDATA (encoded_filename));
> >>       else
> >> 	{
> >> 	  Frename_file (filename, tmp_filename, Qt);
> >> 	  comp_u->handle = dynlib_open_for_eln (SSDATA (ENCODE_FILE (tmp_filename)));
> >> 	  Frename_file (tmp_filename, filename, Qnil);
> >> 	}
> >> 
> >> The renaming results in the ".eln.tmp" suffixed name having an open
> >> handle. That handle is still open when other code tries to delete
> >> the renamed ".eln" file, which fails.
> >
> > The question is: why is that .eln.tmp file still open when we are
> > trying to delete it?  I hoped we'd be able to establish that, so that
> > we could decide what to do about it.  But I don't yet see the
> > information which would explain why the file is still open.
> 
> The file handle is returned from dynlib_open_for_eln (which came from a
> LoadLibrary call in dylib_open). More below.
> 
> > Do these *.eln.tmp file remain in %TEMP% after Emacs startup?  If they
> > remain there, then what happens if you exit Emacs and restart it? do
> > these files remain there or are they deleted?  IOW, if we just ignore
> > these errors (e.g., by ignore-error), would the problem be fixed, or
> > will there be left-overs?
> 
> The ".eln.tmp" suffixed files are only renamed temporarily, and the files
> only fleetingly have that name. The ".eln" suffixed file names persist
> after the failed delete, so will accumulate unless removed manually.

So we are trying to delete a .eln file that is still being loaded into
this same Emacs session?  That sounds like a bug to me.

Andrea, could you see if this situation can happen?  IIUC, it should
also happen on Unix when we compile a trampoline on the flight
(perhaps when a previous outdated version of the trampoline exists?),
except that on Unix the deletion silently succeeds and the file is
removed when the session ends, which doesn't happen on Windows.  On
Windows, we need to unload the .eln file first, but can we do that
with a trampoline?

But first, I'd like to understand whether indeed it could happen that
we are deleting a temporary .eln file for a trampoline we just
compiled when we are still using that .eln file.  If this happens,
we'd need to find a way to delay the deletion till Emacs is about to
exit or something.