Skip to content

Commit 6c34492

Browse files
mhaggergitster
authored andcommitted
ref_transaction_commit(): fix atomicity and avoid fd exhaustion
The old code was roughly for update in updates: acquire locks and check old_sha for update in updates: if changing value: write_ref_to_lockfile() commit_ref_update() for update in updates: if deleting value: unlink() rewrite packed-refs file for update in updates: if reference still locked: unlock_ref() This has two problems. Non-atomic updates ================== The atomicity of the reference transaction depends on all pre-checks being done in the first loop, before any changes have started being committed in the second loop. The problem is that write_ref_to_lockfile() (previously part of write_ref_sha1()), which is called from the second loop, contains two more checks: * It verifies that new_sha1 is a valid object * If the reference being updated is a branch, it verifies that new_sha1 points at a commit object (as opposed to a tag, tree, or blob). If either of these checks fails, the "transaction" is aborted during the second loop. But this might happen after some reference updates have already been permanently committed. In other words, the all-or-nothing promise of "git update-ref --stdin" could be violated. So these checks have to be moved to the first loop. File descriptor exhaustion ========================== The old code locked all of the references in the first loop, leaving all of the lockfiles open until later loops. Since we might be updating a lot of references, this could result in file descriptor exhaustion. The solution ============ After this patch, the code looks like for update in updates: acquire locks and check old_sha if changing value: write_ref_to_lockfile() else: close_ref() for update in updates: if changing value: commit_ref_update() for update in updates: if deleting value: unlink() rewrite packed-refs file for update in updates: if reference still locked: unlock_ref() This fixes both problems: 1. The pre-checks in write_ref_to_lockfile() are now done in the first loop, before any changes have been committed. If any of the checks fails, the whole transaction can now be rolled back correctly. 2. All lockfiles are closed in the first loop immediately after they are created (either by write_ref_to_lockfile() or by close_ref()). This means that there is never more than one open lockfile at a time, preventing file descriptor exhaustion. To simplify the bookkeeping across loops, add a new REF_NEEDS_COMMIT bit to update->flags, which keeps track of whether the corresponding lockfile needs to be committed, as opposed to just unlocked. (Since "struct ref_update" is internal to the refs module, this change is not visible to external callers.) This change fixes two tests in t1400. Signed-off-by: Michael Haggerty <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 805cf6e commit 6c34492

File tree

2 files changed

+47
-12
lines changed

2 files changed

+47
-12
lines changed

refs.c

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ static unsigned char refname_disposition[256] = {
3030
* pruned.
3131
*/
3232
#define REF_ISPRUNING 0x0100
33+
34+
/*
35+
* Used as a flag in ref_update::flags when the lockfile needs to be
36+
* committed.
37+
*/
38+
#define REF_NEEDS_COMMIT 0x0200
39+
3340
/*
3441
* Try to read one refname component from the front of refname.
3542
* Return the length of the component found, or -1 if the component is
@@ -3799,7 +3806,12 @@ int ref_transaction_commit(struct ref_transaction *transaction,
37993806
goto cleanup;
38003807
}
38013808

3802-
/* Acquire all locks while verifying old values */
3809+
/*
3810+
* Acquire all locks, verify old values if provided, check
3811+
* that new values are valid, and write new values to the
3812+
* lockfiles, ready to be activated. Only keep one lockfile
3813+
* open at a time to avoid running out of file descriptors.
3814+
*/
38033815
for (i = 0; i < n; i++) {
38043816
struct ref_update *update = updates[i];
38053817

@@ -3820,26 +3832,49 @@ int ref_transaction_commit(struct ref_transaction *transaction,
38203832
update->refname);
38213833
goto cleanup;
38223834
}
3835+
if (!(update->flags & REF_DELETING) &&
3836+
(update->lock->force_write ||
3837+
hashcmp(update->lock->old_sha1, update->new_sha1))) {
3838+
if (write_ref_to_lockfile(update->lock, update->new_sha1)) {
3839+
/*
3840+
* The lock was freed upon failure of
3841+
* write_ref_to_lockfile():
3842+
*/
3843+
update->lock = NULL;
3844+
strbuf_addf(err, "Cannot update the ref '%s'.",
3845+
update->refname);
3846+
ret = TRANSACTION_GENERIC_ERROR;
3847+
goto cleanup;
3848+
}
3849+
update->flags |= REF_NEEDS_COMMIT;
3850+
} else {
3851+
/*
3852+
* We didn't have to write anything to the lockfile.
3853+
* Close it to free up the file descriptor:
3854+
*/
3855+
if (close_ref(update->lock)) {
3856+
strbuf_addf(err, "Couldn't close %s.lock",
3857+
update->refname);
3858+
goto cleanup;
3859+
}
3860+
}
38233861
}
38243862

38253863
/* Perform updates first so live commits remain referenced */
38263864
for (i = 0; i < n; i++) {
38273865
struct ref_update *update = updates[i];
38283866

3829-
if (!(update->flags & REF_DELETING)) {
3830-
if (!update->lock->force_write &&
3831-
!hashcmp(update->lock->old_sha1, update->new_sha1)) {
3832-
unlock_ref(update->lock);
3867+
if (update->flags & REF_NEEDS_COMMIT) {
3868+
if (commit_ref_update(update->lock,
3869+
update->new_sha1, update->msg)) {
3870+
/* The lock was freed by commit_ref_update(): */
38333871
update->lock = NULL;
3834-
} else if (write_ref_to_lockfile(update->lock, update->new_sha1) ||
3835-
commit_ref_update(update->lock, update->new_sha1, update->msg)) {
3836-
update->lock = NULL; /* freed by the above calls */
38373872
strbuf_addf(err, "Cannot update the ref '%s'.",
38383873
update->refname);
38393874
ret = TRANSACTION_GENERIC_ERROR;
38403875
goto cleanup;
38413876
} else {
3842-
/* freed by the above calls: */
3877+
/* freed by the above call: */
38433878
update->lock = NULL;
38443879
}
38453880
}
@@ -3849,7 +3884,7 @@ int ref_transaction_commit(struct ref_transaction *transaction,
38493884
for (i = 0; i < n; i++) {
38503885
struct ref_update *update = updates[i];
38513886

3852-
if (update->lock) {
3887+
if (update->flags & REF_DELETING) {
38533888
if (delete_ref_loose(update->lock, update->type, err)) {
38543889
ret = TRANSACTION_GENERIC_ERROR;
38553890
goto cleanup;

t/t1400-update-ref.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -979,7 +979,7 @@ run_with_limited_open_files () {
979979

980980
test_lazy_prereq ULIMIT_FILE_DESCRIPTORS 'run_with_limited_open_files true'
981981

982-
test_expect_failure ULIMIT_FILE_DESCRIPTORS 'large transaction creating branches does not burst open file limit' '
982+
test_expect_success ULIMIT_FILE_DESCRIPTORS 'large transaction creating branches does not burst open file limit' '
983983
(
984984
for i in $(test_seq 33)
985985
do
@@ -990,7 +990,7 @@ test_expect_failure ULIMIT_FILE_DESCRIPTORS 'large transaction creating branches
990990
)
991991
'
992992

993-
test_expect_failure ULIMIT_FILE_DESCRIPTORS 'large transaction deleting branches does not burst open file limit' '
993+
test_expect_success ULIMIT_FILE_DESCRIPTORS 'large transaction deleting branches does not burst open file limit' '
994994
(
995995
for i in $(test_seq 33)
996996
do

0 commit comments

Comments
 (0)