Skip to content

Commit

Permalink
create_symref: write reflog while holding lock
Browse files Browse the repository at this point in the history
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 <mhagger@alum.mit.edu>
Signed-off-by: Jeff King <peff@peff.net>
Reviewed-by: Michael Haggerty <mhagger@alum.mit.edu>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
Jeff King authored and Junio C Hamano committed Dec 29, 2015
1 parent 370e5ad commit 396da8f
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion refs/files-backend.c
Original file line number Diff line number Diff line change
Expand Up @@ -2850,12 +2850,13 @@ static int create_symref_locked(struct ref_lock *lock, const char *refname,
return error("unable to fdopen %s: %s",
lock->lk->tempfile.filename.buf, strerror(errno));

update_symref_reflog(lock, refname, target, logmsg);

/* no error check; commit_ref will check ferror */
fprintf(lock->lk->tempfile.fp, "ref: %s\n", target);
if (commit_ref(lock) < 0)
return error("unable to write symref for %s: %s", refname,
strerror(errno));
update_symref_reflog(lock, refname, target, logmsg);
return 0;
}

Expand Down

0 comments on commit 396da8f

Please sign in to comment.