Skip to content

Commit 5034e92

Browse files
committed
Fix: quickly adding and removing a page crashed the server
Because commits are queued, and if remotes are slow, they can stack up. This makes it possible to add a page and remove one, before it is committed. In result, when the commit is actually being executed, it tried to add (or remove) a file that no longer exists. Solve this by simply ignoring these events, are they will be fixed by the next commit in the queue. Sadly, it does mean we lose a bit of history, but .. yeah, not much we can do about that.
1 parent 2630741 commit 5034e92

1 file changed

Lines changed: 23 additions & 2 deletions

File tree

truewiki/storage/git.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,18 @@ def __init__(self, git_commiter, folder, ssh_command):
2727
self._git = git.Repo(self._folder)
2828

2929
def commit(self, git_author, commit_message, files_added, files_changed, files_removed):
30+
# We run this in a separate process, because GitPython is blocking.
31+
# And something like "git push" can take 4+ seconds. This would be
32+
# annoying for the end-user. But by delegating it to its own process,
33+
# we have to take care of some race-conditions. For example, it can
34+
# happen that a user makes an unrelated change, then makes a page and
35+
# removes it directly after. As the unrelated change is taking 4+
36+
# seconds, the next commit tries to add a page that is already removed.
37+
# In that case, we simply ignore the change, as there is nothing else
38+
# we can do.
39+
# It has to be noted, it is rather unlike that a human makes this
40+
# happen, but it is happening a lot with our end-to-end tests.
41+
3042
if not files_added and not files_removed:
3143
# If there is no diff for any of these items, the user reverted back
3244
# to the original state. In this case, do not make a commit, as it
@@ -36,9 +48,18 @@ def commit(self, git_author, commit_message, files_added, files_changed, files_r
3648

3749
# Update the index with the added/removed files.
3850
for filename in files_added + files_changed:
39-
self._git.index.add(filename)
51+
try:
52+
self._git.index.add(filename)
53+
except FileNotFoundError:
54+
# Sadly, a newer change has removed the file. So this
55+
# change will be lost in history.
56+
pass
4057
for filename in files_removed:
41-
self._git.index.remove(filename)
58+
try:
59+
self._git.index.remove(filename)
60+
except git.exc.GitCommandError:
61+
# The file was already removed or never existed.
62+
pass
4263

4364
git_author = git.Actor(*git_author)
4465

0 commit comments

Comments
 (0)