git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: 'git stash push' isn't atomic when Ctrl-C is pressed

From
Junio C Hamano <gitster@pobox.com>
Date
Jan 26, 2022, 19:58 UTC
Message-ID
<xmqqlez2qnfi.fsf@gitster.g>
In-Reply-To
<220126.86bkzyfw3q.gmgdl@evledraar.gmail.com>
Ævar Arnfjörð Bjarmason <avarab@gmail.com> writes:
Show 23 quoted lines
> On Tue, Jan 25 2022, Yuri wrote:
>
>> Ctrl-C was pressed in the middle. git creates the stash record and
>> didn't update the files.
>>
>>
>> Expected behavior: Ctrl-C should cleanly roll back the operation.
>
> Yes, you're right. It really should be fixed.
>
> It's a known issue with builtin/stash.c being written in C, but really
> only still being a faithful conversion of the code we had in a
> git-stash.sh shellscript until relatively recently.
>
> (No fault of those doing the conversion, that's always the logical first
> step).
>
> So it modifies various refs, reflogs etc., but does so mostly via
> shelling out to other git commands, whereas it really should be moved to
> using the ref transaction API.
>
> Ig you or anyone else is interested in would be a most welcome project
> to work on...
I must be missing something.
If I understand the problem description correctly, the user does
	git stash push
which
 * bundles the local changes by recording a commit (with trees and
   blobs) that represents the new stash entry
 * removes the local modification from the working tree files.

And if the user kills the process while the second step is running, there will be files that are restored to HEAD and other files that are left unrestored, because the process was killed before it had a chance to do so. At that point, we probably do not even know which ones have been restored to be "rolled back"---that knowledge is lost when the process got killed.

My take on it is that it is not something that we can call "_should_ be fixed". It is in the same category as "yes, it would be nice if the world worked that way, and it would be nice if we had moon, too".

And it has nothing to do with the command being written in C or shell, and it does not have much to do with the existence of ref transaction API. If you want atomic working tree update, you'd need to snapshot the working tree state, record the fact that you are about to muck with the working tree in a secure place and make sure that hits the disk platter, perform the "stash push" operation including the working tree update, and then remove the record. The record will help you discover that your earlier attempt for doing so failed for whatever reason (e.g. ^C, kill -9, power failure). Then you'd need to arrange that the state gets restored, and possibly redo what you were doing.

Which theoretically can be done. But it would be not practically cheap enough to use in a day-to-day operation. It certainly would be too much to expect a new person to be able to "work on".

And the "theoretically" part is important, in that it draws the line between what is realistic and unrealistic. The thing written in C or shell would not make much of a dent and the existence of ref transaction API would not have much effect on partial working tree updates not being restored. They are red-herrings.

I suspect that the untold thinking behind your statement was that we should try not to discourage new users from asking, and I agree with the sentiment to a certain degree. But at the same time, I think it is simply irresponsible to do so without distinguising between asking for something realistic and unrealistic.

Previous: Ævar Arnfjörð BjarmasonNext: Ævar Arnfjörð Bjarmason
Message 5 of 11 in “'git stash push' isn't atomic when Ctrl-C is pressed”
  1. YuriJan 25, 2022
  2. Ævar Arnfjörð BjarmasonJan 26, 2022
  3. John CaiJan 26, 2022
  4. Ævar Arnfjörð BjarmasonJan 26, 2022
  5. Junio C HamanoJan 26, 2022
  6. Ævar Arnfjörð BjarmasonJan 26, 2022
  7. Junio C HamanoJan 26, 2022
  8. Ævar Arnfjörð BjarmasonJan 27, 2022
  9. Junio C HamanoJan 27, 2022
  10. John CaiJan 27, 2022
  11. Junio C HamanoJan 26, 2022

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.