Skip to content

Commit da5267f

Browse files
mhaggergitster
authored andcommitted
files_transaction_prepare(): fix handling of ref lock failure
Since dc39e09 (files_ref_store: use a transaction to update packed refs, 2017-09-08), failure to lock a reference has been handled incorrectly by `files_transaction_prepare()`. If `lock_ref_for_update()` fails in the lock-acquisition loop of that function, it sets `ret` then breaks out of that loop. Prior to dc39e09, that was OK, because the only thing following the loop was the cleanup code. But dc39e09 added another blurb of code between the loop and the cleanup. That blurb sometimes resets `ret` to zero, making the cleanup code think that the locking was successful. Specifically, whenever * One or more reference deletions have been processed successfully in the lock-acquisition loop. (Processing the first such reference causes a packed-ref transaction to be initialized.) * Then `lock_ref_for_update()` fails for a subsequent reference. Such a failure can happen for a number of reasons, such as the old SHA-1 not being correct, lock contention, etc. This causes a `break` out of the lock-acquisition loop. * The `packed-refs` lock is acquired successfully and `ref_transaction_prepare()` succeeds for the packed-ref transaction. This has the effect of resetting `ret` back to 0, and making the cleanup code think that lock acquisition was successful. In that case, any reference updates that were processed prior to breaking out of the loop would be carried out (loose and packed), but the reference that couldn't be locked and any subsequent references would silently be ignored. This can easily cause data loss if, for example, the user was trying to push a new name for an existing branch while deleting the old name. After the push, the branch could be left unreachable, and could even subsequently be garbage-collected. This problem was noticed in the context of deleting one reference and creating another in a single transaction, when the two references D/F conflict with each other, like git update-ref --stdin <<EOF delete refs/foo create refs/foo/bar HEAD EOF This triggers the above bug because the deletion is processed successfully for `refs/foo`, then the D/F conflict causes `lock_ref_for_update()` to fail when `refs/foo/bar` is processed. In this case the transaction *should* fail, but instead it causes `refs/foo` to be deleted without creating `refs/foo`. This could easily result in data loss. The fix is simple: instead of just breaking out of the loop, jump directly to the cleanup code. This fixes some tests in t1404 that were added in the previous commit. Signed-off-by: Michael Haggerty <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 2e9de01 commit da5267f

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

refs/files-backend.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2527,7 +2527,7 @@ static int files_transaction_prepare(struct ref_store *ref_store,
25272527
ret = lock_ref_for_update(refs, update, transaction,
25282528
head_ref, &affected_refnames, err);
25292529
if (ret)
2530-
break;
2530+
goto cleanup;
25312531

25322532
if (update->flags & REF_DELETING &&
25332533
!(update->flags & REF_LOG_ONLY) &&

t/t1404-update-ref-errors.sh

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -271,11 +271,11 @@ test_expect_success 'D/F conflict prevents add short + delete long' '
271271
df_test refs/df-as-dl --add-del foo foo/bar
272272
'
273273

274-
test_expect_failure 'D/F conflict prevents delete long + add short' '
274+
test_expect_success 'D/F conflict prevents delete long + add short' '
275275
df_test refs/df-dl-as --del-add foo/bar foo
276276
'
277277

278-
test_expect_failure 'D/F conflict prevents delete short + add long' '
278+
test_expect_success 'D/F conflict prevents delete short + add long' '
279279
df_test refs/df-ds-al --del-add foo foo/bar
280280
'
281281

@@ -287,17 +287,17 @@ test_expect_success 'D/F conflict prevents add short + delete long packed' '
287287
df_test refs/df-as-dlp --pack --add-del foo foo/bar
288288
'
289289

290-
test_expect_failure 'D/F conflict prevents delete long packed + add short' '
290+
test_expect_success 'D/F conflict prevents delete long packed + add short' '
291291
df_test refs/df-dlp-as --pack --del-add foo/bar foo
292292
'
293293

294-
test_expect_failure 'D/F conflict prevents delete short packed + add long' '
294+
test_expect_success 'D/F conflict prevents delete short packed + add long' '
295295
df_test refs/df-dsp-al --pack --del-add foo foo/bar
296296
'
297297

298298
# Try some combinations involving symbolic refs...
299299

300-
test_expect_failure 'D/F conflict prevents indirect add long + delete short' '
300+
test_expect_success 'D/F conflict prevents indirect add long + delete short' '
301301
df_test refs/df-ial-ds --sym-add --add-del foo/bar foo
302302
'
303303

@@ -309,11 +309,11 @@ test_expect_success 'D/F conflict prevents indirect add short + indirect delete
309309
df_test refs/df-ias-idl --sym-add --sym-del --add-del foo foo/bar
310310
'
311311

312-
test_expect_failure 'D/F conflict prevents indirect delete long + indirect add short' '
312+
test_expect_success 'D/F conflict prevents indirect delete long + indirect add short' '
313313
df_test refs/df-idl-ias --sym-add --sym-del --del-add foo/bar foo
314314
'
315315

316-
test_expect_failure 'D/F conflict prevents indirect add long + delete short packed' '
316+
test_expect_success 'D/F conflict prevents indirect add long + delete short packed' '
317317
df_test refs/df-ial-dsp --sym-add --pack --add-del foo/bar foo
318318
'
319319

@@ -325,7 +325,7 @@ test_expect_success 'D/F conflict prevents add long + indirect delete short pack
325325
df_test refs/df-al-idsp --sym-del --pack --add-del foo/bar foo
326326
'
327327

328-
test_expect_failure 'D/F conflict prevents indirect delete long packed + indirect add short' '
328+
test_expect_success 'D/F conflict prevents indirect delete long packed + indirect add short' '
329329
df_test refs/df-idlp-ias --sym-add --sym-del --pack --del-add foo/bar foo
330330
'
331331

0 commit comments

Comments
 (0)