{"thread":{"id":"47407","subject":"[PATCH] refs: drop \"clear packed-refs while locked\" assertion","startedAt":"2017-12-08T11:22:30Z","lastAt":"2018-01-01T15:33:16Z","messageCount":2,"participants":["Jeff King","Michael Haggerty"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"334413","messageId":"20171208112222.GA6094@sigill.intra.peff.net","threadId":"47407","inReplyTo":null,"subject":"[PATCH] refs: drop \"clear packed-refs while locked\" assertion","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2017-12-08T11:22:22Z","receivedAt":"2017-12-08T11:22:30Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"This patch fixes a regression in v2.14.0. It's actually fixed already in\nv2.15.0 because all of the packed-ref code there was rewritten. So\nthere's no point in applying this on \"master\" or even \"maint\". But I\nfigured it was worth sharing here in case somebody else runs across it,\nand in case we ever do a v2.14.4 release.\n\n-- >8 --\nIn clear_packed_ref_cache(), we assert that we're not\ncurrently holding the packed-refs lock. But in each of the\nthree code paths that can hit this, the assertion is either\na noop or actively does the wrong thing:\n\n 1. in rollback_packed_refs(), we will have just released\n    the lock before calling the function, and so the\n    assertion can never trigger.\n\n 2. get_packed_ref_cache() can reach this assertion via\n    validate_packed_ref_cache(). But it calls the validate\n    function only when it knows that we're not holding the\n    lock, so again, the assertion can never trigger.\n\n 3. lock_packed_refs() also calls validate_packed_ref_cache().\n    In this case we're _always_ holding the lock, which\n    means any time the validate function has to clear the\n    cache, we'll trigger this assertion and die.\n\n    This doesn't happen often in practice because the\n    validate function clears the cache only if we find that\n    somebody else has racily rewritten the packed-refs file\n    between the time we read it and the time we took the lock.\n\n    So most of the time we don't reach the assertion at all\n    (nobody has racily written the file so there's no need\n    to clear the cache). And when we do, it is not actually\n    indicative of a bug; clearing the cache while holding\n    the lock is the right thing to do here.\n\nThis final case is relatively new, being triggerd by the\nextra validation added in fed6ebebf1 (lock_packed_refs():\nfix cache validity check, 2017-06-12).\n\nSigned-off-by: Jeff King <peff@peff.net>\n---\n refs/files-backend.c | 2 --\n 1 file changed, 2 deletions(-)\n\ndiff --git a/refs/files-backend.c b/refs/files-backend.c\nindex f21a954ce7..dd41e1d382 100644\n--- a/refs/files-backend.c\n+++ b/refs/files-backend.c\n@@ -99,8 +99,6 @@ static void clear_packed_ref_cache(struct files_ref_store *refs)\n \tif (refs->packed) {\n \t\tstruct packed_ref_cache *packed_refs = refs->packed;\n \n-\t\tif (is_lock_file_locked(&refs->packed_refs_lock))\n-\t\t\tdie(\"BUG: packed-ref cache cleared while locked\");\n \t\trefs->packed = NULL;\n \t\trelease_packed_ref_cache(packed_refs);\n \t}\n-- \n2.15.1.659.g8bd2eae3ea\n"},{"id":"335591","messageId":"CAMy9T_HiK-DkJRrNUCK+9ffcOcEgd9qc_F2taO8--jv9gQrs5A@mail.gmail.com","threadId":"47407","inReplyTo":"20171208112222.GA6094@sigill.intra.peff.net","subject":"Re: [PATCH] refs: drop \"clear packed-refs while locked\" assertion","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2018-01-01T15:26:02Z","receivedAt":"2018-01-01T15:33:16Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"On Fri, Dec 8, 2017 at 12:22 PM, Jeff King <peff@peff.net> wrote:\n> This patch fixes a regression in v2.14.0. It's actually fixed already in\n> v2.15.0 because all of the packed-ref code there was rewritten. So\n> there's no point in applying this on \"master\" or even \"maint\". But I\n> figured it was worth sharing here in case somebody else runs across it,\n> and in case we ever do a v2.14.4 release.\n\nI forgot to respond to this. +1\n\nReviewed-by: Michael Haggerty <mhagger@alum.mit.edu>\n\nMichael\n\n> -- >8 --\n> In clear_packed_ref_cache(), we assert that we're not\n> currently holding the packed-refs lock. But in each of the\n> three code paths that can hit this, the assertion is either\n> a noop or actively does the wrong thing:\n>\n>  1. in rollback_packed_refs(), we will have just released\n>     the lock before calling the function, and so the\n>     assertion can never trigger.\n>\n>  2. get_packed_ref_cache() can reach this assertion via\n>     validate_packed_ref_cache(). But it calls the validate\n>     function only when it knows that we're not holding the\n>     lock, so again, the assertion can never trigger.\n>\n>  3. lock_packed_refs() also calls validate_packed_ref_cache().\n>     In this case we're _always_ holding the lock, which\n>     means any time the validate function has to clear the\n>     cache, we'll trigger this assertion and die.\n>\n>     This doesn't happen often in practice because the\n>     validate function clears the cache only if we find that\n>     somebody else has racily rewritten the packed-refs file\n>     between the time we read it and the time we took the lock.\n>\n>     So most of the time we don't reach the assertion at all\n>     (nobody has racily written the file so there's no need\n>     to clear the cache). And when we do, it is not actually\n>     indicative of a bug; clearing the cache while holding\n>     the lock is the right thing to do here.\n>\n> This final case is relatively new, being triggerd by the\n> extra validation added in fed6ebebf1 (lock_packed_refs():\n> fix cache validity check, 2017-06-12).\n>\n> Signed-off-by: Jeff King <peff@peff.net>\n> ---\n>  refs/files-backend.c | 2 --\n>  1 file changed, 2 deletions(-)\n>\n> diff --git a/refs/files-backend.c b/refs/files-backend.c\n> index f21a954ce7..dd41e1d382 100644\n> --- a/refs/files-backend.c\n> +++ b/refs/files-backend.c\n> @@ -99,8 +99,6 @@ static void clear_packed_ref_cache(struct files_ref_store *refs)\n>         if (refs->packed) {\n>                 struct packed_ref_cache *packed_refs = refs->packed;\n>\n> -               if (is_lock_file_locked(&refs->packed_refs_lock))\n> -                       die(\"BUG: packed-ref cache cleared while locked\");\n>                 refs->packed = NULL;\n>                 release_packed_ref_cache(packed_refs);\n>         }\n> --\n> 2.15.1.659.g8bd2eae3ea\n"}]}