Skip to content

Commit 396da8f

Browse files
peffgitster
authored andcommitted
create_symref: write reflog while holding lock
We generally hold a lock on the matching ref while writing to its reflog; this prevents two simultaneous writers from clobbering each other's reflog lines (it does not even have to be two symref updates; because we don't hold the lock, we could race with somebody writing to the pointed-to ref via HEAD, for example). We can fix this by writing the reflog before we commit the lockfile. This runs the risk of writing the reflog but failing the final rename(), but at least we now err on the same side as the rest of the ref code. Noticed-by: Michael Haggerty <[email protected]> Signed-off-by: Jeff King <[email protected]> Reviewed-by: Michael Haggerty <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 370e5ad commit 396da8f

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

refs/files-backend.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2850,12 +2850,13 @@ static int create_symref_locked(struct ref_lock *lock, const char *refname,
28502850
return error("unable to fdopen %s: %s",
28512851
lock->lk->tempfile.filename.buf, strerror(errno));
28522852

2853+
update_symref_reflog(lock, refname, target, logmsg);
2854+
28532855
/* no error check; commit_ref will check ferror */
28542856
fprintf(lock->lk->tempfile.fp, "ref: %s\n", target);
28552857
if (commit_ref(lock) < 0)
28562858
return error("unable to write symref for %s: %s", refname,
28572859
strerror(errno));
2858-
update_symref_reflog(lock, refname, target, logmsg);
28592860
return 0;
28602861
}
28612862

0 commit comments

Comments
 (0)