notes: do not drop notes written by a concurrent process
PATCH 1/1 notes: do not drop notes written by a concurrent process
"git notes add" and the other commands that modify notes read the notes ref, update the notes tree in memory, and then commit it on top of whatever the ref points to by then. If another process updated the ref in the meantime, its notes are silently lost.
The fix is to remember the commit the notes were read from, and use it as both the parent of the new notes commit and the expected old value of the ref update. A concurrent update now makes the command fail with "cannot lock ref" instead.
---
I found this with Rewind, a deterministic VM I am building that replays a program under many thread schedules until one fails. Two "git notes add" commands run at once on different commits lost one of the two notes in 15 of 32 schedules, while both exited 0. It is not specific to
Rewind: on a plain Linux machine 25 of 30 runs lost a note. How Rewind narrowed it down to commit_notes() is written up at:
https://fzakaria.com/2026/10/28/reading-the-code-is-the-hard-part
With this patch no notes are lost; when two commands race, one of them fails with "cannot lock ref" and can be retried.
--- notes-utils.c | 15 +++++++++++++-- notes.c | 8 ++++++-- notes.h | 3 ++- t/t3301-notes.sh | 18 ++++++++++++++++++ 4 files changed, 39 insertions(+), 5 deletions(-)
notes-utils.c +13 -2
@@ -46,6 +46,7 @@ void create_notes_commit(struct repository *r,void commit_notes (struct repository *r,struct notes_tree *t,const char *msg) {struct strbuf buf =STRBUF_INIT ; +struct commit_list *parents =NULL ;struct object_id commit_oid;if (!t)
@@ -59,12 +60,22 @@ void commit_notes(struct repository *r, struct notes_tree *t, const char *msg)strbuf_addstr (&buf, msg);strbuf_complete_line (&buf); -create_notes_commit (r, t,NULL , buf.buf, buf.len, &commit_oid); +/* Build on the commit the notes were loaded from */ +if (!is_null_oid (&t->base_oid)) { +struct commit *parent =lookup_commit (r, &t->base_oid); +if (repo_parse_commit (r, parent)) +die ("Failed to find/parse commit %s" , t->ref); +commit_list_insert (parent, &parents); + } + +create_notes_commit (r, t, parents, buf.buf, buf.len, &commit_oid);strbuf_insertstr (&buf,0 ,"notes: " );refs_update_ref (get_main_ref_store (the_repository), buf.buf, - t->update_ref, &commit_oid,NULL ,0 , + t->update_ref, &commit_oid, &t->base_oid,0 ,UPDATE_REFS_DIE_ON_ERR ); +oidcpy (&t->base_oid, &commit_oid); +commit_list_free (parents);strbuf_release (&buf); }
notes.c +6 -2
@@ -1039,12 +1039,16 @@ void init_notes(struct notes_tree *t, const char *notes_ref, t->combine_notes = combine_notes; t->initialized =1 ; t->dirty =0 ; +oidclr (&t->base_oid, the_repository->hash_algo);if (flags &NOTES_INIT_EMPTY ||repo_get_oid_treeish (the_repository, notes_ref, &object_oid)) goto out; -if (flags &NOTES_INIT_WRITABLE &&refs_read_ref (get_main_ref_store (the_repository), notes_ref, &object_oid)) -die ("Cannot use notes ref %s" , notes_ref); +if (flags &NOTES_INIT_WRITABLE ) { +if (refs_read_ref (get_main_ref_store (the_repository), notes_ref, &object_oid)) +die ("Cannot use notes ref %s" , notes_ref); +oidcpy (&t->base_oid, &object_oid); + }if (get_tree_entry (the_repository, &object_oid,"" , &oid, &mode))die ("Failed to read notes tree referenced by %s (%s)" , notes_ref,oid_to_hex (&object_oid));
notes.h +2 -1
@@ -1,9 +1,9 @@#ifndef NOTES_H #define NOTES_H +#include "hash.h" #include "string-list.h" -struct object_id ;struct repository ;struct strbuf ;
@@ -54,6 +54,7 @@ extern struct notes_tree {struct non_note *first_non_note, *prev_non_note;char *ref;char *update_ref; +struct object_id base_oid;combine_notes_fn combine_notes;int initialized;int dirty;
t/t3301-notes.sh +18 -0
@@ -1629,4 +1629,22 @@ test_expect_success 'git notes append aborts when editor fails with -e' ' test_must_fail git notes show ' +test_expect_success 'git notes add does not drop notes added while it runs' ' + test_commit 23rd && + test_commit 24th && + + # The editor adds a note to another commit while the outer + # "git notes add" is waiting for it, as a concurrent process would. + write_script racing_editor <<-\EOF && + git notes add -m "added meanwhile" 23rd && + echo "edited" >"$1" + EOF + test_must_fail env GIT_EDITOR=./racing_editor git notes add 24th && + + echo "added meanwhile" >expect && + git notes show 23rd >actual && + test_cmp expect actual && + test_must_fail git notes show 24th +' + test_done
---
base-commit: 8d1b215c01512b9658d4525c54ff9b9245f9a7e8
change-id: 20261028-fz-notes-concurrent-write-4ee590954ece
Quoted 4 lines · Farid Zakaria
"git notes add" and the other commands that modify notes read the notes ref, update the notes tree in memory, and then commit it on top of whatever the ref points to by then. If another process updated the ref in the meantime, its notes are silently lost.
This looks like a really nice find.
Quoted 5 lines
The fix is to remember the commit the notes were read from, and use it as both the parent of the new notes commit and the expected old value of the ref update. A concurrent update now makes the command fail with "cannot lock ref" instead.
This makes sense and is how I would have assumed git notes add and similar to work. Because you have:
git update-ref <ref> <new-oid> [<old-oid>
Exactly so that you can try to update a ref with the assumption that nothing else intervenes, because it will fail if <old-oid> does not become the previous OID.
So I thought that the code that git-notes(1) uses would work the same way.
Quoted 4 lines
Signed-off-by Farid Zakaria <[email protected]>---
[snip]
So if this change is:
• Before it silently dropped the change
• Now it loudly fails
Then that is a pure improvement.
However, thinking beyond this patch. Let’s say one program wants to add a note on master. But another one wants to add a note on feature1. One will fail. But should that happen in a perfect world? Let’s say that master wins. feature1 is not trying to “clobber” anything relative to the master program; it wants to add a note to a different commit. And does the feature1 program care if the commit corresponding to the note is a child of the commit when it first started the process? I don’t think so. It does matter for two programs that want to update a branch, but here the commit structure is a kind of implementation detail, how Notes are implemented so that you get snapshot of the notes as well as the whole edit history. As an example:
commit A, commit B, commit C, commit D...
It could just be shuffled.
Instead, maybe this would happen in an ideal world:
1. master and feature1 race to commit a note
2. master wins
3. feature1 notices that there is a new note commit (from master)
4. It checks whether that commit changed the commit that *it* (feature1) wanted to update
• If so: fail
• If not: continue
5. feature1 retries (go to (1))
Maybe this is too much logic for a “porcelain” command (interactive use), but there is no plumbing Notes command.
Quoted 11 lines · Kristoffer Haugsbakk
Instead, maybe this would happen in an ideal world:
1.
masterandfeature1race to commit a note
2.masterwins
3.feature1notices that there is a new note commit (frommaster)
4. It checks whether that commit changed the commit that *it* (feature1) wanted to update
• If so: fail
• If not: continue
5.feature1retries (go to (1))
Quoted 2 lines
Maybe this is too much logic for a “porcelain” command (interactive use), but there is no plumbing Notes command.
Or possibly
1. two races. Both remembers what commit was at the tip of the notes tree (call it commit #1).
2. each creates a new note commit on top of #1. Call them commit #2master and #2feature1.
3. 'master' one wins the race. As #2master is a fast-forward from #1, the notes tree gets #2master at its tip.
4. 'feature1' loses the race. It merges #2feature1 into, or rebases #2feature1 on top of, #2master and updates the notes tree.
;-)
Quoted 24 lines · Junio C Hamano
Quoted 11 lines · Kristoffer Haugsbakk
Instead, maybe this would happen in an ideal world:
1.
masterandfeature1race to commit a note
2.masterwins
3.feature1notices that there is a new note commit (frommaster)
4. It checks whether that commit changed the commit that *it* (feature1) wanted to update
• If so: fail
• If not: continue
5.feature1retries (go to (1))Quoted 2 lines
Maybe this is too much logic for a “porcelain” command (interactive use), but there is no plumbing Notes command.
Or possibly
1. two races. Both remembers what commit was at the tip of the notes tree (call it commit #1).
2. each creates a new note commit on top of #1. Call them commit #2master and #2feature1.
Quoted 8 lines
3. 'master' one wins the race. As #2master is a fast-forward from #1, the notes tree gets #2master at its tip.
4. 'feature1' loses the race. It merges #2feature1 into, or rebases #2feature1 on top of, #2master and updates the notes tree.
;-)
Your proposals for retries when possible make sense but I may leave that to you to implement.
I felt confident reviewing the current patch at it's current state (AI-assisted) and I do concur that it's a net-positive change in the interim.
(I guess I could try to implement the retry logic - AI assisted if you like)
Would it be OK if I also suggested the other two bugs (+small fixes) I found on the mailing list (with the same tool)?
They are:
* Similar race-type bug for credential-cache leaving a stale daemon+credential
* fsmonitor daemon keeps running after its worktree is deleted or moved
Both patches are of similar size and complexity to this one, with a test case.
Quoted 3 lines · Farid Zakaria
I felt confident reviewing the current patch at it's current state (AI-assisted) and I do concur that it's a net-positive change in the interim.
Sorry, but I find the above very confusing, and I cannot figure out who is reviewing whose patch, and who is concuring whose statement that said what.
Quoted 9 lines · Junio C Hamano
Quoted 3 lines · Farid Zakaria
I felt confident reviewing the current patch at it's current state (AI-assisted) and I do concur that it's a net-positive change in the interim.
Sorry, but I find the above very confusing, and I cannot figure out who is reviewing whose patch, and who is concuring whose statement that said what.
It's because I bottom-posted; I thought it might be easier to read that way -- I guess not.
I was just trying to say that I felt confident in my comprehension of this current patch, and agreed with Kristoffer that it's a positive change as it stands, although the algorithm could be improved to handle cases with different branches as mentioned.
(hopefully that's a little more concise and clear)
Quoted 14 lines · Farid Zakaria
Quoted 9 lines · Junio C Hamano
Quoted 3 lines · Farid Zakaria
I felt confident reviewing the current patch at it's current state (AI-assisted) and I do concur that it's a net-positive change in the interim.
Sorry, but I find the above very confusing, and I cannot figure out who is reviewing whose patch, and who is concuring whose statement that said what.
It's because I bottom-posted; I thought it might be easier to read that way -- I guess not.
Quoted 6 lines
I was just trying to say that I felt confident in my comprehension of this current patch, and agreed with Kristoffer that it's a positive change as it stands, although the algorithm could be improved to handle cases with different branches as mentioned.
(hopefully that's a little more concise and clear)
Are we talking to a meat proxy here?
Patrick
Quoted 6 lines · Farid Zakaria
Quoted 5 lines · Junio C Hamano
Quoted 1 line · Kristoffer Haugsbakk
[snip]
Or possibly
Quoted 8 lines
Quoted 1 line
[snip]
Your proposals for retries when possible make sense but I may leave that to you to implement.
I felt confident reviewing the current patch at it's current state (AI-assisted) and I do concur that it's a net-positive change in the interim.
Use of GenAI should be disclosed up front.Documentation/SubmittingPatches, [[ai]].
Quoted 13 lines
(I guess I could try to implement the retry logic - AI assisted if you like)
Would it be OK if I also suggested the other two bugs (+small fixes) I found on the mailing list (with the same tool)?
They are:
* Similar race-type bug for credential-cache leaving a stale daemon+credential
* fsmonitor daemon keeps running after its worktree is deleted or movedBoth patches are of similar size and complexity to this one, with a test case.
Quoted 12 lines · Kristoffer Haugsbakk
Quoted 11 lines · Farid Zakaria
Quoted 6 lines · Junio C Hamano
Quoted 1 line · Kristoffer Haugsbakk
[snip]
Or possibly
[snip]
Your proposals for retries when possible make sense but I may leave that to you to implement.
Quoted 7 lines
Quoted 3 lines
I felt confident reviewing the current patch at it's current state (AI-assisted) and I do concur that it's a net-positive change in the interim.
Use of GenAI should be disclosed up front.
Documentation/SubmittingPatches, [[ai]].
Please consider the thread my disclosure.
FWIW I did read https://git-scm.com/docs/SubmittingPatches as of today and I don't see any specific mention of disclosure. It might be a good idea to make that explicit.
I see no mention of special trailer or mention to disclosure although it does mention rejecting anything that looks like "slop" which I hope this is not -- I went through the patch and the failure.
If I was looking at the wrong document, please point me to the right one.
Quoted 13 lines
Quoted 13 lines
(I guess I could try to implement the retry logic - AI assisted if you like)
Would it be OK if I also suggested the other two bugs (+small fixes) I found on the mailing list (with the same tool)?
They are:
* Similar race-type bug for credential-cache leaving a stale daemon+credential
* fsmonitor daemon keeps running after its worktree is deleted or movedBoth patches are of similar size and complexity to this one, with a test case.
Quoted 8 lines · Farid Zakaria
Quoted 6 lines · Kristoffer Haugsbakk
Quoted 1 line · Farid Zakaria
Quoted 1 line
Quoted 1 line
[snip]
Use of GenAI should be disclosed up front.
Documentation/SubmittingPatches, [[ai]].
Quoted 5 lines
Please consider the thread my disclosure.
FWIW I did read https://git-scm.com/docs/SubmittingPatches as of today and I don't see any specific mention of disclosure. It might be a good idea to make that explicit.
I guess I, uh, had a false memory.
Quoted 8 lines
I see no mention of special trailer or mention to disclosure although it does mention rejecting anything that looks like "slop" which I hope this is not -- I went through the patch and the failure.
If I was looking at the wrong document, please point me to the right one.
Quoted 1 line
Quoted 1 line
[snip]
Quoted 24 lines · Junio C Hamano
Quoted 11 lines · Kristoffer Haugsbakk
Instead, maybe this would happen in an ideal world:
1.
masterandfeature1race to commit a note
2.masterwins
3.feature1notices that there is a new note commit (frommaster)
4. It checks whether that commit changed the commit that *it* (feature1) wanted to update
• If so: fail
• If not: continue
5.feature1retries (go to (1))Quoted 2 lines
Maybe this is too much logic for a “porcelain” command (interactive use), but there is no plumbing Notes command.
Or possibly
1. two races. Both remembers what commit was at the tip of the notes tree (call it commit #1).
2. each creates a new note commit on top of #1. Call them commit #2master and #2feature1.
Quoted 8 lines
3. 'master' one wins the race. As #2master is a fast-forward from #1, the notes tree gets #2master at its tip.
4. 'feature1' loses the race. It merges #2feature1 into, or rebases #2feature1 on top of, #2master and updates the notes tree.
;-)
That would be the most elegant. But I think doing a three-way merge would go too far. Say you have another case where two processes try to update the note for the same commit C, but the first process edits the start and the second one edits the end. These are far enough apart that they merge successfully. But I think that such a race concluding with a note merge would be wrong in general.