{"thread":{"id":"4411","subject":"[REGRESSION] Interrupted clone/fetch leaves .lock files around","startedAt":"2006-06-06T18:51:48Z","lastAt":"2006-06-06T21:58:46Z","messageCount":4,"participants":["Jonas Fonseca","Junio C Hamano"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"21336","messageId":"20060606185148.GA15521@diku.dk","threadId":"4411","inReplyTo":null,"subject":"[REGRESSION] Interrupted clone/fetch leaves .lock files around","fromName":"Jonas Fonseca","fromEmail":"fonseca@diku.dk","sentAt":"2006-06-06T18:51:48Z","receivedAt":"2006-06-06T18:51:48Z","isPatch":false,"sender":{"key":"fonseca@diku.dk","avatar":"https://gravatar.com/avatar/f82f3ad698717c51873b020c750a92438c820a24056dc39fe4d07baa10a92264?d=mp&s=160"},"body":"Hi,\n\nIt used to be possible to continue an interrupted clone when using\ncg-clone, by cding into the partial repo and run cg-fetch. However, it\nseems that the recent changes in the ref locking code ends up leaving\n.lock files around when interrupted.\n\n$ cg clone http://elinks.cz/elinks.git\ndefaulting to local storage area\nFetching head...\nFetching objects...\n...\nGetting pack c0e265dab40fa34912c3ee6e02ba29686ab84a7b\n which contains 16f85ec5043966c69e2142198c52e12494dcfc76\nprogress: 22 objects, 939754 bytes, now fetching c0e265dab40f... (657678 bytes)\ncg-clone: interrupted\n$ cd elinks\n$ cg fetch\nRecovering from a previously interrupted initial clone...\nFetching head...\nFetching objects...\nerror: Couldn't open lock file .git/refs/heads/origin.lock: File exists\nerror: Can't lock ref heads/origin\nprogress: 0 objects, 0 bytes\ncg-fetch: objects fetch failed\n\nBelow is my feeble attempt at a (tested) fix.\n\ndiff --git a/fetch.c b/fetch.c\nindex e040ef9..861dc60 100644\n--- a/fetch.c\n+++ b/fetch.c\n@@ -1,3 +1,5 @@\n+#include <signal.h>\n+\n #include \"fetch.h\"\n \n #include \"cache.h\"\n@@ -214,9 +216,19 @@ static int mark_complete(const char *pat\n \treturn 0;\n }\n \n+static struct ref_lock *lock = NULL;\n+\n+static void remove_lockfile_on_signal(int signo)\n+{\n+\tif (lock)\n+\t\tunlock_ref(lock);\n+\tlock = NULL;\n+\tsignal(SIGINT, SIG_DFL);\n+\traise(signo);\n+}\n+\n int pull(char *target)\n {\n-\tstruct ref_lock *lock = NULL;\n \tunsigned char sha1[20];\n \tchar *msg;\n \tint ret;\n@@ -229,6 +241,7 @@ int pull(char *target)\n \t\t\terror(\"Can't lock ref %s\", write_ref);\n \t\t\treturn -1;\n \t\t}\n+\t\tsignal(SIGINT, remove_lockfile_on_signal);\n \t}\n \n \tif (!get_recover)\n@@ -236,22 +249,11 @@ int pull(char *target)\n \n \tif (interpret_target(target, sha1)) {\n \t\terror(\"Could not interpret %s as something to pull\", target);\n-\t\tif (lock)\n-\t\t\tunlock_ref(lock);\n-\t\treturn -1;\n-\t}\n-\tif (process(lookup_unknown_object(sha1))) {\n-\t\tif (lock)\n-\t\t\tunlock_ref(lock);\n-\t\treturn -1;\n-\t}\n-\tif (loop()) {\n-\t\tif (lock)\n-\t\t\tunlock_ref(lock);\n-\t\treturn -1;\n-\t}\n \n-\tif (write_ref) {\n+\t} else if (process(lookup_unknown_object(sha1)) || loop()) {\n+\t\t; /* unlock */\n+\n+\t} else if (write_ref) {\n \t\tif (write_ref_log_details) {\n \t\t\tmsg = xmalloc(strlen(write_ref_log_details) + 12);\n \t\t\tsprintf(msg, \"fetch from %s\", write_ref_log_details);\n@@ -261,6 +263,10 @@ int pull(char *target)\n \t\tif (msg)\n \t\t\tfree(msg);\n \t\treturn ret;\n+\t} else {\n+\t\treturn 0;\n \t}\n-\treturn 0;\n+\n+\tremove_lockfile_on_signal(0);\n+\treturn -1;\n }\n\n-- \nJonas Fonseca\n"},{"id":"21337","messageId":"20060606190255.GB15521@diku.dk","threadId":"4411","inReplyTo":"20060606185148.GA15521@diku.dk","subject":"Re: [REGRESSION] Interrupted clone/fetch leaves .lock files around","fromName":"Jonas Fonseca","fromEmail":"fonseca@diku.dk","sentAt":"2006-06-06T19:02:55Z","receivedAt":"2006-06-06T19:02:55Z","isPatch":false,"sender":{"key":"fonseca@diku.dk","avatar":"https://gravatar.com/avatar/f82f3ad698717c51873b020c750a92438c820a24056dc39fe4d07baa10a92264?d=mp&s=160"},"body":"Jonas Fonseca <fonseca@diku.dk> wrote Tue, Jun 06, 2006:\n> Below is my feeble attempt at a (tested) fix.\n\nOk, so maybe I didn't test it so well, other than continuously\ninterrupting the fetch. ;)\n \n> diff --git a/fetch.c b/fetch.c\n> index e040ef9..861dc60 100644\n> --- a/fetch.c\n> +++ b/fetch.c\n> @@ -214,9 +216,19 @@ static int mark_complete(const char *pat\n>  \treturn 0;\n>  }\n>  \n> +static struct ref_lock *lock = NULL;\n> +\n> +static void remove_lockfile_on_signal(int signo)\n> +{\n> +\tif (lock)\n> +\t\tunlock_ref(lock);\n> +\tlock = NULL;\n> +\tsignal(SIGINT, SIG_DFL);\n> +\traise(signo);\n> +}\n> +\n>  int pull(char *target)\n>  {\n> -\tstruct ref_lock *lock = NULL;\n>  \tunsigned char sha1[20];\n>  \tchar *msg;\n>  \tint ret;\n...\n> @@ -261,6 +263,10 @@ int pull(char *target)\n>  \t\tif (msg)\n>  \t\t\tfree(msg);\n>  \t\treturn ret;\n> +\t} else {\n> +\t\treturn 0;\n>  \t}\n> -\treturn 0;\n> +\n> +\tremove_lockfile_on_signal(0);\n\nThis will end up calling raise().\n\n> +\treturn -1;\n>  }\n\n-- \nJonas Fonseca\n"},{"id":"21338","messageId":"7vmzcqp0cn.fsf@assigned-by-dhcp.cox.net","threadId":"4411","inReplyTo":"20060606185148.GA15521@diku.dk","subject":"Re: [REGRESSION] Interrupted clone/fetch leaves .lock files around","fromName":"Junio C Hamano","fromEmail":"junkio@cox.net","sentAt":"2006-06-06T19:09:44Z","receivedAt":"2006-06-06T19:09:44Z","isPatch":false,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Jonas Fonseca <fonseca@diku.dk> writes:\n\n> Below is my feeble attempt at a (tested) fix.\n>\n> diff --git a/fetch.c b/fetch.c\n> index e040ef9..861dc60 100644\n> --- a/fetch.c\n> +++ b/fetch.c\n> @@ -1,3 +1,5 @@\n> +#include <signal.h>\n> +\n>  #include \"fetch.h\"\n\nI suspect you could do something similar to what we already do\nfor index updates using atexit().  Let me take a look.\n"},{"id":"21347","messageId":"7v1wu2ndyh.fsf@assigned-by-dhcp.cox.net","threadId":"4411","inReplyTo":"7vmzcqp0cn.fsf@assigned-by-dhcp.cox.net","subject":"Re: [REGRESSION] Interrupted clone/fetch leaves .lock files around","fromName":"Junio C Hamano","fromEmail":"junkio@cox.net","sentAt":"2006-06-06T21:58:46Z","receivedAt":"2006-06-06T21:58:46Z","isPatch":false,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Junio C Hamano <junkio@cox.net> writes:\n\n> Jonas Fonseca <fonseca@diku.dk> writes:\n>\n>> Below is my feeble attempt at a (tested) fix.\n>>\n>> diff --git a/fetch.c b/fetch.c\n>> index e040ef9..861dc60 100644\n>> --- a/fetch.c\n>> +++ b/fetch.c\n>> @@ -1,3 +1,5 @@\n>> +#include <signal.h>\n>> +\n>>  #include \"fetch.h\"\n>\n> I suspect you could do something similar to what we already do\n> for index updates using atexit().  Let me take a look.\n\nIndeed it turns out that the signal work Pasky did in index.c is\nexactly suitable for this.  I've pushed out three patches in\n\"next\" -- a few more eyeballs are appreciated on this one.\n"}]}