git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v2 3/5] refs.c: do not die if locking fails in `delete_pseudoref()`

From
MÅMartin Ågren <martin.agren@gmail.com>
Date
May 9, 2018, 20:55 UTC
Message-ID
<9446a4b48bab984c097dc391e4e3dc060a40415c.1525898125.git.martin.agren@gmail.com>
In-Reply-To
<cover.1525898125.git.martin.agren@gmail.com>

After taking the lock we check whether we got it and die otherwise. But since we take the lock using `LOCK_DIE_ON_ERROR`, we would already have died.

Considering the choice between dropping the dead code and dropping the flag, let's go for option number three: Drop the flag, write an error instead of dying, then return -1. This function already returns -1 for another error, so the caller (or rather, its callers) should be able to handle this. There is some inconsistency around how we handle errors in this function and elsewhere in this file, but let's take this small step towards gentle error-reporting now and leave the rest for another time.

While at it, make the lock non-static and reduce its scope. (Placing `struct lock_file`s on the stack used to be a bad idea, because the temp- and lockfile-machinery would keep a pointer into the struct. But after 076aa2cbd (tempfile: auto-allocate tempfiles on heap, 2017-09-05), we can safely have lockfiles on the stack.)

Signed-off-by: Martin Ågren <martin.agren@gmail.com>
---
 refs.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
diff --git a/refs.c b/refs.c
index 8c50b8b139..c006004bcd 100644
--- a/refs.c
+++ b/refs.c
@@ -689,20 +689,23 @@ static int write_pseudoref(const char *pseudoref, const struct object_id *oid,
 
 static int delete_pseudoref(const char *pseudoref, const struct object_id *old_oid)
 {
-	static struct lock_file lock;
 	const char *filename;
 
 	filename = git_path("%s", pseudoref);
 
 	if (old_oid && !is_null_oid(old_oid)) {
+		struct lock_file lock = LOCK_INIT;
 		int fd;
 		struct object_id actual_old_oid;
 
 		fd = hold_lock_file_for_update_timeout(
-				&lock, filename, LOCK_DIE_ON_ERROR,
+				&lock, filename, 0,
 				get_files_ref_lock_timeout_ms());
-		if (fd < 0)
-			die_errno(_("Could not open '%s' for writing"), filename);
+		if (fd < 0) {
+			error_errno(_("could not open '%s' for writing"),
+				    filename);
+			return -1;
+		}
 		if (read_ref(pseudoref, &actual_old_oid))
 			die("could not read ref '%s'", pseudoref);
 		if (oidcmp(&actual_old_oid, old_oid)) {
-- 
2.17.0.411.g9fd64c8e46
Previous: Martin ÅgrenNext: Martin Ågren
Message 6 of 11 in “getting rid of most "static struct lock_file"s”
  1. 0/5 getting rid of most "static struct lock_file"sMartin Ågren, May 6, 2018
  2. Jeff KingMay 8, 2018
  3. 0/5 getting rid of most "static struct lock_file"sMartin Ågren, May 9, 2018
  4. 1/5 t/helper/test-write-cache: clean up lock-handlingMartin Ågren, May 9, 2018
  5. 2/5 refs.c: do not die if locking fails in `write_pseudoref()`Martin Ågren, May 9, 2018
  6. 3/5 refs.c: do not die if locking fails in `delete_pseudoref()`Martin Ågren, May 9, 2018
  7. 4/5 lock_file: make function-local locks non-staticMartin Ågren, May 9, 2018
  8. 5/5 lock_file: move static locks into functionsMartin Ågren, May 9, 2018
  9. Jeff KingMay 10, 2018
  10. Junio C HamanoMay 10, 2018
  11. Martin ÅgrenMay 10, 2018

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.