{"thread":{"id":"66550","subject":"[PATCH 0/3] object-file: fix \".keep\" lockfile ownership in ODB transactions","startedAt":"2026-10-16T02:28:58Z","lastAt":"2026-10-27T18:32:28Z","messageCount":40,"participants":["Justin Tobler","Patrick Steinhardt","Junio C Hamano","Karthik Nayak"],"isPatch":true,"patchVersion":1,"patchTotal":3},"messages":[{"id":"555201","messageId":"20261016022901.3838402-1-jltobler@gmail.com","threadId":"66550","inReplyTo":null,"subject":"[PATCH 0/3] object-file: fix \".keep\" lockfile ownership in ODB transactions","fromName":"Justin Tobler","fromEmail":"jltobler@gmail.com","sentAt":"2026-10-16T02:28:58Z","receivedAt":"2026-10-16T02:28:58Z","isPatch":true,"sender":{"key":"jltobler@gmail.com","avatar":"https://avatars.githubusercontent.com/u/53454972?v=4"},"body":"Greetings,\n\nThis series addresses a bug report[1] regarding a git-receive-pack(1)\nprocess deleting \".keep\" lockfiles of with the same name but from\nanother concurrent git-receive-pack(1) process receiving an identical\npackfile. Deleting the \".keep\" lockfile for another process leaves its\nnewly written packfile vulnerable to deletion by another concurrent\nhousekeeping process.\n\nThe root of the problem here is that the temporary \".keep\" lockfiles are\ngenerated and registered in the object quarantine prior to being written\nin the primary ODB. This means that even if the ODB transaction never\ncommits and exits early, the tempfile atexit(3) handler still runs and\ntries to delete the lockfile at its expected final location. This\nblindly deletes any file matching regardless of whether is was created\nby the process.\n\nThe first two patches are some preparatory changes so that\n`odb_transaction_files_write_pack()` is able to start explicitly\ncreating the lockfiles without relying on git-index-pack(1). Patch three\nmigrates lockfile generation to happen during ODB transaction commit to\nensure the lockfile is actually able to be written before registering it\nas a tempfile.\n\nThanks,\n-Justin\n\n[1]: <932e8e425aecfbd33c1e5caf66c80a0226abacba.1789385483.git.gitgitgadget@gmail.com>\n\nJustin Tobler (3):\n  pack-write: lift and expose index-pack report parsing\n  object-file: explicitly write \".keep\" lockfile\n  object-file: write \".keep\" lockfiles during transaction commit\n\n object-file.c              | 117 +++++++++++++++++++++++++++++--------\n pack-write.c               |  69 ++++++++++++++++------\n pack.h                     |  10 ++++\n t/t5547-push-quarantine.sh |  29 ++++++++-\n 4 files changed, 181 insertions(+), 44 deletions(-)\n\n\nbase-commit: f57c9a73884400380bddd172acc88b569834f909\n-- \n2.55.0.424.g13c7afec21\n\n\n"},{"id":"555202","messageId":"20261016022901.3838402-2-jltobler@gmail.com","threadId":"66550","inReplyTo":"20261016022901.3838402-1-jltobler@gmail.com","subject":"[PATCH 1/3] pack-write: lift and expose index-pack report parsing","fromName":"Justin Tobler","fromEmail":"jltobler@gmail.com","sentAt":"2026-10-16T02:28:59Z","receivedAt":"2026-10-16T02:28:59Z","isPatch":true,"sender":{"key":"jltobler@gmail.com","avatar":"https://avatars.githubusercontent.com/u/53454972?v=4"},"body":"Once git-index-pack(1) finishes writing a packfile, it reports the\nhexadecimal name of that packfile on stdout prefixed with either \"pack\"\nor \"keep\" depending on whether a \".keep\" file was also created alongside\nit. Callers that are required to manage the lifecycle of \".keep\" files\nparse this report via `index_pack_lockfile()`, which provides the path\nto the temporary \".keep\" file.\n\nIn a subsequent commit, the \"files\" ODB transaction begins staging\npackfiles in a transaction without eagerly creating a \".keep\" file and\nmust instead read git-index-pack(1) \"pack\" output to get the packfile\nname. Extract the generic parsing logic for the git-index-pack(1) output\ninto `read_index_pack_report()` in preparation accordingly.\n\nSigned-off-by: Justin Tobler <jltobler@gmail.com>\n---\n pack-write.c | 69 +++++++++++++++++++++++++++++++++++++++-------------\n pack.h       | 10 ++++++++\n 2 files changed, 62 insertions(+), 17 deletions(-)\n\ndiff --git a/pack-write.c b/pack-write.c\nindex 85674e4b72..5f5bb894a5 100644\n--- a/pack-write.c\n+++ b/pack-write.c\n@@ -469,33 +469,68 @@ void fixup_pack_header_footer(const struct git_hash_algo *hash_algo,\n \tfsync_component_or_die(FSYNC_COMPONENT_PACK, pack_fd, pack_name);\n }\n \n-char *index_pack_lockfile(struct odb_source *source, int ip_out,\n-\t\t\t  int *is_well_formed)\n+int read_index_pack_report(struct repository *repo, int ip_out,\n+\t\t\t   struct strbuf *pack_name, int *is_keep)\n {\n+\tconst int len = repo->hash_algo->hexsz + 6;\n \tchar packname[GIT_MAX_HEXSZ + 6];\n-\tconst int len = source->odb->repo->hash_algo->hexsz + 6;\n+\tconst char *name;\n+\tint keep;\n \n \t/*\n-\t * The first thing we expect from index-pack's output\n-\t * is \"pack\\t%40s\\n\" or \"keep\\t%40s\\n\" (46 bytes) where\n-\t * %40s is the newly created pack SHA1 name.  In the \"keep\"\n-\t * case, we need it to remove the corresponding .keep file\n-\t * later on.  If we don't get that then tough luck with it.\n+\t * The first thing we expect from index-pack's output is\n+\t * \"pack\\t<name>\\n\" or \"keep\\t<name>\\n\", where <name> is the hexadecimal\n+\t * name of the packfile it has just written. The line is therefore five\n+\t * bytes of prefix, `hexsz` bytes of name, and a trailing newline. A\n+\t * \"keep\" prefix tells us that index-pack has also created the\n+\t * corresponding \".keep\" file.\n \t */\n-\tif (read_in_full(ip_out, packname, len) == len && packname[len-1] == '\\n') {\n-\t\tconst char *name;\n+\tif (read_in_full(ip_out, packname, len) != len || packname[len-1] != '\\n')\n+\t\treturn -1;\n+\n+\tpackname[len-1] = 0;\n+\tif (skip_prefix(packname, \"pack\\t\", &name))\n+\t\tkeep = 0;\n+\telse if (skip_prefix(packname, \"keep\\t\", &name))\n+\t\tkeep = 1;\n+\telse\n+\t\treturn -1;\n+\n+\tstrbuf_addstr(pack_name, name);\n+\tif (is_keep)\n+\t\t*is_keep = keep;\n+\n+\treturn 0;\n+}\n+\n+char *index_pack_lockfile(struct odb_source *source, int ip_out,\n+\t\t\t  int *is_well_formed)\n+{\n+\tstruct strbuf pack_name = STRBUF_INIT;\n+\tchar *lockfile = NULL;\n+\tint is_keep = 0;\n \n+\tif (read_index_pack_report(source->odb->repo, ip_out, &pack_name, &is_keep)) {\n \t\tif (is_well_formed)\n-\t\t\t*is_well_formed = 1;\n-\t\tpackname[len-1] = 0;\n-\t\tif (skip_prefix(packname, \"keep\\t\", &name))\n-\t\t\treturn xstrfmt(\"%s/pack/pack-%s.keep\",\n-\t\t\t\t       source->path, name);\n+\t\t\t*is_well_formed = 0;\n \t\treturn NULL;\n \t}\n+\n \tif (is_well_formed)\n-\t\t*is_well_formed = 0;\n-\treturn NULL;\n+\t\t*is_well_formed = 1;\n+\n+\t/*\n+\t * If there is no \".keep\" file, then there is no lockfile to return.\n+\t */\n+\tif (!is_keep) {\n+\t\tstrbuf_release(&pack_name);\n+\t\treturn NULL;\n+\t}\n+\n+\tlockfile = xstrfmt(\"%s/pack/pack-%s.keep\", source->path, pack_name.buf);\n+\n+\tstrbuf_release(&pack_name);\n+\treturn lockfile;\n }\n \n /*\ndiff --git a/pack.h b/pack.h\nindex ada506b5c5..ddf2d52934 100644\n--- a/pack.h\n+++ b/pack.h\n@@ -106,6 +106,16 @@ off_t write_pack_header(struct hashfile *f, uint32_t);\n void fixup_pack_header_footer(const struct git_hash_algo *, int,\n \t\t\t      unsigned char *, const char *, uint32_t,\n \t\t\t      unsigned char *, off_t);\n+\n+/*\n+ * Read the report that git-index-pack(1) writes to its standard output after\n+ * the packfile is written. The hexadecimal name of the packfile is appended to\n+ * `pack_name` and, if `is_keep` is non-NULL, set it to indicate whether a\n+ * \".keep\" file was written. Return 0 on success and a negative error code on\n+ * failure.\n+ */\n+int read_index_pack_report(struct repository *repo, int ip_out,\n+\t\t\t   struct strbuf *pack_name, int *is_keep);\n char *index_pack_lockfile(struct odb_source *source, int fd,\n \t\t\t  int *is_well_formed);\n \n-- \n2.55.0.424.g13c7afec21\n\n\n"},{"id":"555203","messageId":"20261016022901.3838402-3-jltobler@gmail.com","threadId":"66550","inReplyTo":"20261016022901.3838402-1-jltobler@gmail.com","subject":"[PATCH 2/3] object-file: explicitly write \".keep\" lockfile","fromName":"Justin Tobler","fromEmail":"jltobler@gmail.com","sentAt":"2026-10-16T02:29:00Z","receivedAt":"2026-10-16T02:29:00Z","isPatch":true,"sender":{"key":"jltobler@gmail.com","avatar":"https://avatars.githubusercontent.com/u/53454972?v=4"},"body":"The `odb_transaction_write_pack()` interface for the \"files\" backend\nrelies on git-index-pack(1) to write the specified packfile into an ODB\ntransaction as a packfile. To prevent a concurrent housekeeping\noperation from deleting the newly written packfile prior to the\nreferences being updated, a \".keep\" lockfile is also generated by\ngit-index-pack(1) via the `--keep` option. Consequently, the \".keep\"\nlockfile is written into the ODB transaction alongside the packfile and\nlater migrated together to the primary ODB during transaction commit.\n\nIn a subsequent commit, writing the lockfile is deferred until\ntransaction commit and thus cannot rely on git-index-pack(1) to write\nit. In preparation for this, update `odb_transaction_files_write_pack()`\nto explicitly generate the same \".keep\" lockfile itself.\n\nSigned-off-by: Justin Tobler <jltobler@gmail.com>\n---\n object-file.c | 99 ++++++++++++++++++++++++++++++++++++++++-----------\n 1 file changed, 79 insertions(+), 20 deletions(-)\n\ndiff --git a/object-file.c b/object-file.c\nindex 210984f825..ebe46d35de 100644\n--- a/object-file.c\n+++ b/object-file.c\n@@ -1256,6 +1256,43 @@ int read_loose_object(struct repository *repo,\n \treturn ret;\n }\n \n+static int write_pack_lock(struct odb_source *source, const char *pack_name,\n+\t\t\t   const char *keep_msg)\n+{\n+\tstruct strbuf lock_path = STRBUF_INIT;\n+\tstruct strbuf contents = STRBUF_INIT;\n+\tint ret = -1;\n+\tint fd;\n+\n+\tstrbuf_addf(&lock_path, \"%s/pack/pack-%s.keep\",\n+\t\t    source->path, pack_name);\n+\tstrbuf_addf(&contents, \"%s\\n\", keep_msg);\n+\n+\tfd = safe_create_file_with_leading_directories(source->odb->repo,\n+\t\t\t\t\t\t       lock_path.buf);\n+\tif (fd < 0)\n+\t\tgoto out;\n+\n+\tif (write_in_full(fd, contents.buf, contents.len) < 0) {\n+\t\tclose(fd);\n+\t\tunlink(lock_path.buf);\n+\t\tgoto out;\n+\t}\n+\n+\tif (close(fd)) {\n+\t\tunlink(lock_path.buf);\n+\t\tgoto out;\n+\t}\n+\n+\tret = 0;\n+\n+out:\n+\tstrbuf_release(&lock_path);\n+\tstrbuf_release(&contents);\n+\n+\treturn ret;\n+}\n+\n static int odb_transaction_files_commit(struct odb_transaction *base)\n {\n \tstruct odb_transaction_files *transaction =\n@@ -1389,18 +1426,17 @@ static int odb_transaction_files_write_pack(struct odb_transaction *base,\n \t\t\treturn -1;\n \t\t}\n \t} else {\n+\t\tstruct strbuf final_path = STRBUF_INIT;\n+\t\tstruct strbuf pack_name = STRBUF_INIT;\n+\t\tstruct strbuf keep_msg = STRBUF_INIT;\n \t\tchar hostname[HOST_NAME_MAX + 1];\n-\t\tchar *lockfile;\n+\t\tint ret;\n \n \t\tstrvec_pushl(&child.args, \"index-pack\", \"--stdin\", NULL);\n \t\tpush_header_arg(&child.args, &hdr);\n \n \t\tif (xgethostname(hostname, sizeof(hostname)))\n \t\t\txsnprintf(hostname, sizeof(hostname), \"localhost\");\n-\t\tstrvec_pushf(&child.args,\n-\t\t\t     \"--keep=receive-pack %\"PRIuMAX\" on %s\",\n-\t\t\t     (uintmax_t)getpid(),\n-\t\t\t     hostname);\n \n \t\tif (!opts->quiet && err_fd)\n \t\t\tstrvec_push(&child.args, \"--show-resolving-progress\");\n@@ -1424,31 +1460,54 @@ static int odb_transaction_files_write_pack(struct odb_transaction *base,\n \t\t\treturn -1;\n \t\t}\n \n-\t\t/*\n-\t\t * The lockfile filepath is expected to be the final location of\n-\t\t * the \".keep\" file after being migrated to the main ODB source.\n-\t\t * This ensures the lockfile can be found and removed later\n-\t\t * after the ODB transaction has been committed.\n-\t\t */\n-\t\tlockfile = index_pack_lockfile(base->source, child.out, NULL);\n-\t\tif (lockfile) {\n-\t\t\tALLOC_GROW(transaction->pack_lockfiles,\n-\t\t\t\t   transaction->pack_lockfiles_nr + 1,\n-\t\t\t\t   transaction->pack_lockfiles_alloc);\n-\t\t\ttransaction->pack_lockfiles[transaction->pack_lockfiles_nr++] =\n-\t\t\t\tregister_tempfile(lockfile);\n-\t\t\tfree(lockfile);\n-\t\t}\n+\t\tret = read_index_pack_report(repo, child.out, &pack_name, NULL);\n+\n \t\tclose(child.out);\n \n \t\tstatus = finish_command(&child);\n \t\tif (status) {\n+\t\t\tstrbuf_release(&pack_name);\n \t\t\tstrbuf_addstr(err_msg, \"index-pack abnormal exit\");\n \t\t\treturn -1;\n \t\t}\n \n+\t\tif (ret) {\n+\t\t\tstrbuf_release(&pack_name);\n+\t\t\tstrbuf_addstr(err_msg, \"index-pack failed to report pack name\");\n+\t\t\treturn -1;\n+\t\t}\n+\n+\t\tstrbuf_addf(&keep_msg, \"receive-pack %\"PRIuMAX\" on %s\",\n+\t\t\t    (uintmax_t)getpid(), hostname);\n+\n+\t\tif (write_pack_lock(transaction->quarantine, pack_name.buf, keep_msg.buf)) {\n+\t\t\tstrbuf_release(&pack_name);\n+\t\t\tstrbuf_release(&keep_msg);\n+\t\t\tstrbuf_addstr(err_msg, \"lockfile write failed\");\n+\t\t\treturn -1;\n+\t\t}\n+\n+\t\tALLOC_GROW(transaction->pack_lockfiles,\n+\t\t\t   transaction->pack_lockfiles_nr + 1,\n+\t\t\t   transaction->pack_lockfiles_alloc);\n+\n+\t\t/*\n+\t\t * The lockfile filepath is expected to be the final location of\n+\t\t * the \".keep\" file after being migrated to the main ODB source.\n+\t\t * This ensures the lockfile can be found and removed later\n+\t\t * after the ODB transaction has been committed.\n+\t\t */\n+\t\tstrbuf_addf(&final_path, \"%s/pack/pack-%s.keep\",\n+\t\t\t    base->source->path, pack_name.buf);\n+\t\ttransaction->pack_lockfiles[transaction->pack_lockfiles_nr++] =\n+\t\t\tregister_tempfile(final_path.buf);\n+\n \t\todb_source_prepare(transaction->quarantine,\n \t\t\t\t   ODB_PREPARE_FLUSH_CACHES);\n+\n+\t\tstrbuf_release(&final_path);\n+\t\tstrbuf_release(&pack_name);\n+\t\tstrbuf_release(&keep_msg);\n \t}\n \n \treturn 0;\n-- \n2.55.0.424.g13c7afec21\n\n\n"},{"id":"555204","messageId":"20261016022901.3838402-4-jltobler@gmail.com","threadId":"66550","inReplyTo":"20261016022901.3838402-1-jltobler@gmail.com","subject":"[PATCH 3/3] object-file: write \".keep\" lockfiles during transaction commit","fromName":"Justin Tobler","fromEmail":"jltobler@gmail.com","sentAt":"2026-10-16T02:29:01Z","receivedAt":"2026-10-16T02:29:01Z","isPatch":true,"sender":{"key":"jltobler@gmail.com","avatar":"https://avatars.githubusercontent.com/u/53454972?v=4"},"body":"The \"files\" ODB backend writes a \".keep\" lockfile into the transaction\nquarantine directory via `odb_transaction_files_write_pack()`. When the\nODB transaction is committed, the \".keep\" lockfile is migrated to the\nprimary ODB alongside its packfile. Writing the temporary lockfile in\nthis manner has a couple of downsides:\n\n  - Failing to migrate the lockfile during transaction commit due to a\n    concurrent process already acquiring the same lock still allows the\n    rest of the transaction to be migrated into the primary ODB. This\n    dirties the primary ODB with new objects even though no references\n    were updated and the operation failed.\n\n  - Even more concerning, the \".keep\" tempfile atexit(3) handler tries\n    to remove any lockfile of the same name regardless of whether it was\n    created by the same process. If the \".keep\" lockfile of the same\n    name for a concurrent process gets deleted prematurely, its packfile\n    becomes at risk of being deleted by concurrent housekeeping.\n\nStop creating/registering the temporary lockfiles when writing the\npackfile to the transaction and instead do so when committing the\ntransaction. This ensures the lockfile is properly acquired prior to\nmigrating objects and that only the owning process can delete its own\nlockfiles.\n\nReported-by: Qin ShiCheng <qeesung@live.com>\nSigned-off-by: Justin Tobler <jltobler@gmail.com>\n---\n object-file.c              | 76 +++++++++++++++++++++-----------------\n t/t5547-push-quarantine.sh | 29 +++++++++++++--\n 2 files changed, 69 insertions(+), 36 deletions(-)\n\ndiff --git a/object-file.c b/object-file.c\nindex ebe46d35de..39e6299be1 100644\n--- a/object-file.c\n+++ b/object-file.c\n@@ -492,6 +492,7 @@ struct odb_transaction_files {\n \tstruct transaction_packfile packfile;\n \tconst char *prefix;\n \n+\tstruct strvec pack_names;\n \tstruct tempfile **pack_lockfiles;\n \tsize_t pack_lockfiles_nr;\n \tsize_t pack_lockfiles_alloc;\n@@ -1257,7 +1258,7 @@ int read_loose_object(struct repository *repo,\n }\n \n static int write_pack_lock(struct odb_source *source, const char *pack_name,\n-\t\t\t   const char *keep_msg)\n+\t\t\t   const char *keep_msg, struct tempfile **out)\n {\n \tstruct strbuf lock_path = STRBUF_INIT;\n \tstruct strbuf contents = STRBUF_INIT;\n@@ -1284,6 +1285,7 @@ static int write_pack_lock(struct odb_source *source, const char *pack_name,\n \t\tgoto out;\n \t}\n \n+\t*out = register_tempfile(lock_path.buf);\n \tret = 0;\n \n out:\n@@ -1293,6 +1295,40 @@ static int write_pack_lock(struct odb_source *source, const char *pack_name,\n \treturn ret;\n }\n \n+static int write_pack_locks(struct odb_transaction_files *transaction)\n+{\n+\tstruct strbuf keep_msg = STRBUF_INIT;\n+\tchar hostname[HOST_NAME_MAX + 1];\n+\tint ret = 0;\n+\n+\tif (!transaction->pack_names.nr)\n+\t\treturn 0;\n+\n+\tif (xgethostname(hostname, sizeof(hostname)))\n+\t\txsnprintf(hostname, sizeof(hostname), \"localhost\");\n+\tstrbuf_addf(&keep_msg, \"receive-pack %\"PRIuMAX\" on %s\",\n+\t\t    (uintmax_t)getpid(), hostname);\n+\n+\tfor (size_t i = 0; i < transaction->pack_names.nr; i++) {\n+\t\tstruct tempfile *lock;\n+\n+\t\tret = write_pack_lock(transaction->base.source,\n+\t\t\t\t      transaction->pack_names.v[i],\n+\t\t\t\t      keep_msg.buf, &lock);\n+\t\tif (ret < 0)\n+\t\t\tbreak;\n+\n+\t\tALLOC_GROW(transaction->pack_lockfiles,\n+\t\t\t   transaction->pack_lockfiles_nr + 1,\n+\t\t\t   transaction->pack_lockfiles_alloc);\n+\t\ttransaction->pack_lockfiles[transaction->pack_lockfiles_nr++] = lock;\n+\t}\n+\n+\tstrbuf_release(&keep_msg);\n+\n+\treturn ret;\n+}\n+\n static int odb_transaction_files_commit(struct odb_transaction *base)\n {\n \tstruct odb_transaction_files *transaction =\n@@ -1321,6 +1357,9 @@ static int odb_transaction_files_commit(struct odb_transaction *base)\n \t\tdelete_tempfile(&temp);\n \t\tstrbuf_release(&temp_path);\n \n+\t\tif (write_pack_locks(transaction))\n+\t\t\treturn error(_(\"unable to lock packfiles\"));\n+\n \t\t/*\n \t\t * Make the object files visible in the primary ODB after their data is\n \t\t * fully durable.\n@@ -1426,18 +1465,12 @@ static int odb_transaction_files_write_pack(struct odb_transaction *base,\n \t\t\treturn -1;\n \t\t}\n \t} else {\n-\t\tstruct strbuf final_path = STRBUF_INIT;\n \t\tstruct strbuf pack_name = STRBUF_INIT;\n-\t\tstruct strbuf keep_msg = STRBUF_INIT;\n-\t\tchar hostname[HOST_NAME_MAX + 1];\n \t\tint ret;\n \n \t\tstrvec_pushl(&child.args, \"index-pack\", \"--stdin\", NULL);\n \t\tpush_header_arg(&child.args, &hdr);\n \n-\t\tif (xgethostname(hostname, sizeof(hostname)))\n-\t\t\txsnprintf(hostname, sizeof(hostname), \"localhost\");\n-\n \t\tif (!opts->quiet && err_fd)\n \t\t\tstrvec_push(&child.args, \"--show-resolving-progress\");\n \t\tif (err_fd)\n@@ -1477,37 +1510,12 @@ static int odb_transaction_files_write_pack(struct odb_transaction *base,\n \t\t\treturn -1;\n \t\t}\n \n-\t\tstrbuf_addf(&keep_msg, \"receive-pack %\"PRIuMAX\" on %s\",\n-\t\t\t    (uintmax_t)getpid(), hostname);\n-\n-\t\tif (write_pack_lock(transaction->quarantine, pack_name.buf, keep_msg.buf)) {\n-\t\t\tstrbuf_release(&pack_name);\n-\t\t\tstrbuf_release(&keep_msg);\n-\t\t\tstrbuf_addstr(err_msg, \"lockfile write failed\");\n-\t\t\treturn -1;\n-\t\t}\n-\n-\t\tALLOC_GROW(transaction->pack_lockfiles,\n-\t\t\t   transaction->pack_lockfiles_nr + 1,\n-\t\t\t   transaction->pack_lockfiles_alloc);\n-\n-\t\t/*\n-\t\t * The lockfile filepath is expected to be the final location of\n-\t\t * the \".keep\" file after being migrated to the main ODB source.\n-\t\t * This ensures the lockfile can be found and removed later\n-\t\t * after the ODB transaction has been committed.\n-\t\t */\n-\t\tstrbuf_addf(&final_path, \"%s/pack/pack-%s.keep\",\n-\t\t\t    base->source->path, pack_name.buf);\n-\t\ttransaction->pack_lockfiles[transaction->pack_lockfiles_nr++] =\n-\t\t\tregister_tempfile(final_path.buf);\n+\t\tstrvec_push(&transaction->pack_names, pack_name.buf);\n \n \t\todb_source_prepare(transaction->quarantine,\n \t\t\t\t   ODB_PREPARE_FLUSH_CACHES);\n \n-\t\tstrbuf_release(&final_path);\n \t\tstrbuf_release(&pack_name);\n-\t\tstrbuf_release(&keep_msg);\n \t}\n \n \treturn 0;\n@@ -1523,6 +1531,7 @@ static int odb_transaction_files_finalize(struct odb_transaction *base)\n \t\tret |= delete_tempfile(&transaction->pack_lockfiles[i]);\n \n \tfree(transaction->pack_lockfiles);\n+\tstrvec_clear(&transaction->pack_names);\n \n \treturn ret;\n }\n@@ -1555,6 +1564,7 @@ int odb_transaction_files_begin(struct odb_source *source,\n \ttransaction->base.write_pack = odb_transaction_files_write_pack;\n \ttransaction->base.env = odb_transaction_files_env;\n \ttransaction->flags = flags;\n+\tstrvec_init(&transaction->pack_names);\n \n \ttransaction->prefix = \"bulk-fsync\";\n \tif (flags & ODB_TRANSACTION_RECEIVE) {\ndiff --git a/t/t5547-push-quarantine.sh b/t/t5547-push-quarantine.sh\nindex 1b7097179e..cf349eee2c 100755\n--- a/t/t5547-push-quarantine.sh\n+++ b/t/t5547-push-quarantine.sh\n@@ -77,10 +77,9 @@ test_expect_success '.keep file is removed after push' '\n \tgit -C keep.git config set receive.unpackLimit 0 &&\n \n \t# While incoming objects are still quarantined, validate that the\n-\t# \".keep\" lockfile is present in the quarantine directory.\n+\t# \".keep\" lockfile is not present in the quarantine directory.\n \ttest_hook -C keep.git pre-receive <<-\\EOF &&\n-\tkeep=\"$(ls \"$GIT_QUARANTINE_PATH\"/pack/pack-*.keep)\" &&\n-\ttest -f \"$keep\"\n+\ttest ! -e \"$GIT_QUARANTINE_PATH\"/pack/pack-*.keep\n \tEOF\n \n \t# After quarantined objects are migrated, validate that the \".keep\"\n@@ -101,4 +100,28 @@ test_expect_success '.keep file is removed after push' '\n \ttest_path_is_missing \"$keep\"\n '\n \n+test_expect_success '.keep file created outside of process is not removed' '\n+\ttest_when_finished rm -rf keep.git &&\n+\tgit init --bare keep.git &&\n+\n+\tgit -C keep.git config set receive.unpackLimit 0 &&\n+\n+\t# Create a \".keep\" lockfile matching the packfile to simulate a\n+\t# concurrent git-receive-pack(1) that was already written its lockfile.\n+\ttest_hook -C keep.git pre-receive <<-\\EOF &&\n+\tpack=\"$(ls \"$GIT_QUARANTINE_PATH\"/pack/pack-*.pack)\" &&\n+\tpack=\"${pack##*/}\" &&\n+\techo \"baz\" >\"objects/pack/${pack%.pack}.keep\"\n+\tEOF\n+\n+\ttest_commit bar &&\n+\ttest_must_fail git push keep.git HEAD &&\n+\n+\t# The \".keep\" lockfile of the concurrent git-receive-pack(1) should be\n+\t# left in its place.\n+\tkeep=\"$(ls keep.git/objects/pack/pack-*.keep)\" &&\n+\ttest_path_is_file \"$keep\" &&\n+\ttest_grep \"baz\" \"$keep\"\n+'\n+\n test_done\n-- \n2.55.0.424.g13c7afec21\n\n\n"},{"id":"555211","messageId":"atHLBS0GGxf_cPmT@pks.im","threadId":"66550","inReplyTo":"20261016022901.3838402-2-jltobler@gmail.com","subject":"Re: [PATCH 1/3] pack-write: lift and expose index-pack report parsing","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-10-16T06:58:13Z","receivedAt":"2026-10-16T06:58:13Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"On Thu, Oct 15, 2026 at 09:28:59PM -0500, Justin Tobler wrote:\n> Once git-index-pack(1) finishes writing a packfile, it reports the\n> hexadecimal name of that packfile on stdout prefixed with either \"pack\"\n> or \"keep\" depending on whether a \".keep\" file was also created alongside\n> it. Callers that are required to manage the lifecycle of \".keep\" files\n> parse this report via `index_pack_lockfile()`, which provides the path\n> to the temporary \".keep\" file.\n> \n> In a subsequent commit, the \"files\" ODB transaction begins staging\n> packfiles in a transaction without eagerly creating a \".keep\" file and\n> must instead read git-index-pack(1) \"pack\" output to get the packfile\n> name. Extract the generic parsing logic for the git-index-pack(1) output\n> into `read_index_pack_report()` in preparation accordingly.\n\nSo if I understand correctly, the intent is to eventually stop creating\nthe \".keep\" file in the quarantine directory? I assume that instead,\nyou'll then manually create that file in the final location before\nmoving around the pack itself.\n\nIf so that makes sense -- it's kind of pointless to create \".keep\" files\nin quarantines anyway, nobody but the current process should be touching\nit anyway.\n\n> diff --git a/pack-write.c b/pack-write.c\n> index 85674e4b72..5f5bb894a5 100644\n> --- a/pack-write.c\n> +++ b/pack-write.c\n> @@ -469,33 +469,68 @@ void fixup_pack_header_footer(const struct git_hash_algo *hash_algo,\n>  \tfsync_component_or_die(FSYNC_COMPONENT_PACK, pack_fd, pack_name);\n>  }\n>  \n> -char *index_pack_lockfile(struct odb_source *source, int ip_out,\n> -\t\t\t  int *is_well_formed)\n> +int read_index_pack_report(struct repository *repo, int ip_out,\n> +\t\t\t   struct strbuf *pack_name, int *is_keep)\n>  {\n> +\tconst int len = repo->hash_algo->hexsz + 6;\n>  \tchar packname[GIT_MAX_HEXSZ + 6];\n> -\tconst int len = source->odb->repo->hash_algo->hexsz + 6;\n> +\tconst char *name;\n> +\tint keep;\n\nSuggestion, please feel free to ignore:\n\n    int is_keep_ignored;\n\n    if (!is_keep)\n        is_keep = &is_keep_ignored;\n\nThen you can unconditionally use `is_keep` further down, which I think\nis a bit less awkward.\n\n>  \t/*\n> -\t * The first thing we expect from index-pack's output\n> -\t * is \"pack\\t%40s\\n\" or \"keep\\t%40s\\n\" (46 bytes) where\n> -\t * %40s is the newly created pack SHA1 name.  In the \"keep\"\n> -\t * case, we need it to remove the corresponding .keep file\n> -\t * later on.  If we don't get that then tough luck with it.\n> +\t * The first thing we expect from index-pack's output is\n> +\t * \"pack\\t<name>\\n\" or \"keep\\t<name>\\n\", where <name> is the hexadecimal\n> +\t * name of the packfile it has just written. The line is therefore five\n> +\t * bytes of prefix, `hexsz` bytes of name, and a trailing newline. A\n> +\t * \"keep\" prefix tells us that index-pack has also created the\n> +\t * corresponding \".keep\" file.\n>  \t */\n\nOkay. I originally thought you only reflow this comment, but you fix it\nto no longer assume that packfile hashes use SHA1. I would have either\npointed this out in the commit message or done this as a preparatory\ncommit.\n\n> -\tif (read_in_full(ip_out, packname, len) == len && packname[len-1] == '\\n') {\n> -\t\tconst char *name;\n> +\tif (read_in_full(ip_out, packname, len) != len || packname[len-1] != '\\n')\n> +\t\treturn -1;\n> +\n> +\tpackname[len-1] = 0;\n\nMissing spaces around `-`.\n\n> +\tif (skip_prefix(packname, \"pack\\t\", &name))\n> +\t\tkeep = 0;\n> +\telse if (skip_prefix(packname, \"keep\\t\", &name))\n> +\t\tkeep = 1;\n> +\telse\n> +\t\treturn -1;\n> +\n> +\tstrbuf_addstr(pack_name, name);\n> +\tif (is_keep)\n> +\t\t*is_keep = keep;\n> +\n> +\treturn 0;\n> +}\n> +\n> +char *index_pack_lockfile(struct odb_source *source, int ip_out,\n> +\t\t\t  int *is_well_formed)\n> +{\n> +\tstruct strbuf pack_name = STRBUF_INIT;\n> +\tchar *lockfile = NULL;\n> +\tint is_keep = 0;\n>  \n> +\tif (read_index_pack_report(source->odb->repo, ip_out, &pack_name, &is_keep)) {\n>  \t\tif (is_well_formed)\n> -\t\t\t*is_well_formed = 1;\n> -\t\tpackname[len-1] = 0;\n> -\t\tif (skip_prefix(packname, \"keep\\t\", &name))\n> -\t\t\treturn xstrfmt(\"%s/pack/pack-%s.keep\",\n> -\t\t\t\t       source->path, name);\n> +\t\t\t*is_well_formed = 0;\n>  \t\treturn NULL;\n>  \t}\n> +\n>  \tif (is_well_formed)\n> -\t\t*is_well_formed = 0;\n> -\treturn NULL;\n> +\t\t*is_well_formed = 1;\n> +\n> +\t/*\n> +\t * If there is no \".keep\" file, then there is no lockfile to return.\n> +\t */\n> +\tif (!is_keep) {\n> +\t\tstrbuf_release(&pack_name);\n> +\t\treturn NULL;\n> +\t}\n\nCan we maybe have a single exit path responsible for releasing\n`pack_name`?\n\n    /* If there is no \".keep\" file, then there is no lockfile to return. */\n    if (is_keep)\n        lockfile = xstrfmt(\"%s/pack/pack-%s.keep\", source->path, pack_name.buf);\n\n    strbuf_release(&pack_name);\n    return lockfile;\n\n> diff --git a/pack.h b/pack.h\n> index ada506b5c5..ddf2d52934 100644\n> --- a/pack.h\n> +++ b/pack.h\n> @@ -106,6 +106,16 @@ off_t write_pack_header(struct hashfile *f, uint32_t);\n>  void fixup_pack_header_footer(const struct git_hash_algo *, int,\n>  \t\t\t      unsigned char *, const char *, uint32_t,\n>  \t\t\t      unsigned char *, off_t);\n> +\n> +/*\n> + * Read the report that git-index-pack(1) writes to its standard output after\n> + * the packfile is written. The hexadecimal name of the packfile is appended to\n> + * `pack_name` and, if `is_keep` is non-NULL, set it to indicate whether a\n\ns/set it to/it is set to/\n\n> + * \".keep\" file was written. Return 0 on success and a negative error code on\n> + * failure.\n> + */\n> +int read_index_pack_report(struct repository *repo, int ip_out,\n> +\t\t\t   struct strbuf *pack_name, int *is_keep);\n>  char *index_pack_lockfile(struct odb_source *source, int fd,\n>  \t\t\t  int *is_well_formed);\n\nPatrick\n\n"},{"id":"555212","messageId":"atHLDysZmYGyMiKk@pks.im","threadId":"66550","inReplyTo":"20261016022901.3838402-3-jltobler@gmail.com","subject":"Re: [PATCH 2/3] object-file: explicitly write \".keep\" lockfile","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-10-16T06:58:23Z","receivedAt":"2026-10-16T06:58:23Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"On Thu, Oct 15, 2026 at 09:29:00PM -0500, Justin Tobler wrote:\n> diff --git a/object-file.c b/object-file.c\n> index 210984f825..ebe46d35de 100644\n> --- a/object-file.c\n> +++ b/object-file.c\n> @@ -1256,6 +1256,43 @@ int read_loose_object(struct repository *repo,\n>  \treturn ret;\n>  }\n>  \n> +static int write_pack_lock(struct odb_source *source, const char *pack_name,\n> +\t\t\t   const char *keep_msg)\n\nNit: I'd have called this `write_pack_keep()`, but I see that we refer\nto the files as \"lock\" in `index_pack_lockfile(), too. Should we align\nthose two functions and call this `write_pack_lockfile()`?\n\n> +{\n> +\tstruct strbuf lock_path = STRBUF_INIT;\n> +\tstruct strbuf contents = STRBUF_INIT;\n> +\tint ret = -1;\n> +\tint fd;\n> +\n> +\tstrbuf_addf(&lock_path, \"%s/pack/pack-%s.keep\",\n> +\t\t    source->path, pack_name);\n> +\tstrbuf_addf(&contents, \"%s\\n\", keep_msg);\n> +\n> +\tfd = safe_create_file_with_leading_directories(source->odb->repo,\n> +\t\t\t\t\t\t       lock_path.buf);\n\nThe command has just written its packfile, so can't we assume that\nleading directories have already been created?\n\n> +\tif (fd < 0)\n> +\t\tgoto out;\n> +\n> +\tif (write_in_full(fd, contents.buf, contents.len) < 0) {\n> +\t\tclose(fd);\n> +\t\tunlink(lock_path.buf);\n> +\t\tgoto out;\n> +\t}\n> +\n> +\tif (close(fd)) {\n\n`if (close(fd) < 0)`, just to be consistent.\n\n> @@ -1389,18 +1426,17 @@ static int odb_transaction_files_write_pack(struct odb_transaction *base,\n>  \t\t\treturn -1;\n>  \t\t}\n>  \t} else {\n> +\t\tstruct strbuf final_path = STRBUF_INIT;\n> +\t\tstruct strbuf pack_name = STRBUF_INIT;\n> +\t\tstruct strbuf keep_msg = STRBUF_INIT;\n>  \t\tchar hostname[HOST_NAME_MAX + 1];\n> -\t\tchar *lockfile;\n> +\t\tint ret;\n>  \n>  \t\tstrvec_pushl(&child.args, \"index-pack\", \"--stdin\", NULL);\n>  \t\tpush_header_arg(&child.args, &hdr);\n>  \n>  \t\tif (xgethostname(hostname, sizeof(hostname)))\n>  \t\t\txsnprintf(hostname, sizeof(hostname), \"localhost\");\n> -\t\tstrvec_pushf(&child.args,\n> -\t\t\t     \"--keep=receive-pack %\"PRIuMAX\" on %s\",\n> -\t\t\t     (uintmax_t)getpid(),\n> -\t\t\t     hostname);\n\nOkay. So instead of asking git-index-pack(1) to write the \".keep\" file,\nwe now...\n\n> @@ -1424,31 +1460,54 @@ static int odb_transaction_files_write_pack(struct odb_transaction *base,\n>  \t\t\treturn -1;\n>  \t\t}\n>  \n> -\t\t/*\n> -\t\t * The lockfile filepath is expected to be the final location of\n> -\t\t * the \".keep\" file after being migrated to the main ODB source.\n> -\t\t * This ensures the lockfile can be found and removed later\n> -\t\t * after the ODB transaction has been committed.\n> -\t\t */\n> -\t\tlockfile = index_pack_lockfile(base->source, child.out, NULL);\n> -\t\tif (lockfile) {\n> -\t\t\tALLOC_GROW(transaction->pack_lockfiles,\n> -\t\t\t\t   transaction->pack_lockfiles_nr + 1,\n> -\t\t\t\t   transaction->pack_lockfiles_alloc);\n> -\t\t\ttransaction->pack_lockfiles[transaction->pack_lockfiles_nr++] =\n> -\t\t\t\tregister_tempfile(lockfile);\n> -\t\t\tfree(lockfile);\n> -\t\t}\n> +\t\tret = read_index_pack_report(repo, child.out, &pack_name, NULL);\n> +\n\n... read its stdout to figure out where the packfile was written to\nand...\n\n>  \t\tstatus = finish_command(&child);\n>  \t\tif (status) {\n> +\t\t\tstrbuf_release(&pack_name);\n>  \t\t\tstrbuf_addstr(err_msg, \"index-pack abnormal exit\");\n>  \t\t\treturn -1;\n>  \t\t}\n>  \n> +\t\tif (ret) {\n> +\t\t\tstrbuf_release(&pack_name);\n> +\t\t\tstrbuf_addstr(err_msg, \"index-pack failed to report pack name\");\n> +\t\t\treturn -1;\n> +\t\t}\n> +\n> +\t\tstrbuf_addf(&keep_msg, \"receive-pack %\"PRIuMAX\" on %s\",\n> +\t\t\t    (uintmax_t)getpid(), hostname);\n> +\n> +\t\tif (write_pack_lock(transaction->quarantine, pack_name.buf, keep_msg.buf)) {\n> +\t\t\tstrbuf_release(&pack_name);\n> +\t\t\tstrbuf_release(&keep_msg);\n> +\t\t\tstrbuf_addstr(err_msg, \"lockfile write failed\");\n> +\t\t\treturn -1;\n> +\t\t}\n\n... then write the lockfile ourselves.\n\nBy the way, the error handling here starts to become unwieldy. Can we\nmaybe have a common exit path that releases all resources for us?\n\n> +\t\tALLOC_GROW(transaction->pack_lockfiles,\n> +\t\t\t   transaction->pack_lockfiles_nr + 1,\n> +\t\t\t   transaction->pack_lockfiles_alloc);\n> +\n> +\t\t/*\n> +\t\t * The lockfile filepath is expected to be the final location of\n> +\t\t * the \".keep\" file after being migrated to the main ODB source.\n> +\t\t * This ensures the lockfile can be found and removed later\n> +\t\t * after the ODB transaction has been committed.\n> +\t\t */\n> +\t\tstrbuf_addf(&final_path, \"%s/pack/pack-%s.keep\",\n> +\t\t\t    base->source->path, pack_name.buf);\n> +\t\ttransaction->pack_lockfiles[transaction->pack_lockfiles_nr++] =\n> +\t\t\tregister_tempfile(final_path.buf);\n\nSo here we retain the status quo, which is to record the lockfile in the\nfinal location even though it doesn't exist there yet. And that can lead\nto us pruning a pack lockfile from a different process.\n\nPatrick\n\n"},{"id":"555213","messageId":"atHLFIp8MI729ZkP@pks.im","threadId":"66550","inReplyTo":"20261016022901.3838402-4-jltobler@gmail.com","subject":"Re: [PATCH 3/3] object-file: write \".keep\" lockfiles during transaction commit","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-10-16T06:58:28Z","receivedAt":"2026-10-16T06:58:28Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"On Thu, Oct 15, 2026 at 09:29:01PM -0500, Justin Tobler wrote:\n> The \"files\" ODB backend writes a \".keep\" lockfile into the transaction\n> quarantine directory via `odb_transaction_files_write_pack()`. When the\n> ODB transaction is committed, the \".keep\" lockfile is migrated to the\n> primary ODB alongside its packfile. Writing the temporary lockfile in\n> this manner has a couple of downsides:\n> \n>   - Failing to migrate the lockfile during transaction commit due to a\n>     concurrent process already acquiring the same lock still allows the\n>     rest of the transaction to be migrated into the primary ODB. This\n>     dirties the primary ODB with new objects even though no references\n>     were updated and the operation failed.\n\nI'm not fully sure I understand this downside. Are you saying that we\nfail to migrate the lockfile, then continue migrating everything else\nonly to then fail the whole transaction?\n\n... skipping ahead a bit...\n\nOh, I get it now. You propose to first acquire all lockfiles before\nmigrating anything so that we cannot conflict with a concurrent process\nthat aims to claim the same locks.\n\nThat will of course mean that we will fail the transaction for any\nconcurrent process that happens to write the same packfiles. But based\non what you claim that was already the case anyway, but now we notice\nearly and thus won't commit parts of the transaction. Makes sense.\n\n>   - Even more concerning, the \".keep\" tempfile atexit(3) handler tries\n>     to remove any lockfile of the same name regardless of whether it was\n>     created by the same process. If the \".keep\" lockfile of the same\n>     name for a concurrent process gets deleted prematurely, its packfile\n>     becomes at risk of being deleted by concurrent housekeeping.\n\nAnd this is the other issue that you've already been hinting at.\n\n> diff --git a/object-file.c b/object-file.c\n> index ebe46d35de..39e6299be1 100644\n> --- a/object-file.c\n> +++ b/object-file.c\n> @@ -1321,6 +1357,9 @@ static int odb_transaction_files_commit(struct odb_transaction *base)\n>  \t\tdelete_tempfile(&temp);\n>  \t\tstrbuf_release(&temp_path);\n>  \n> +\t\tif (write_pack_locks(transaction))\n> +\t\t\treturn error(_(\"unable to lock packfiles\"));\n\nOkay, we'll now write lockfiles before migrating any of the data. So if\nany of the files already happens to exist, we'll bail out here. It would\nhave been great to maybe also provide a proper failure message via errno\nfor example, but okay.\n\n> @@ -1477,37 +1510,12 @@ static int odb_transaction_files_write_pack(struct odb_transaction *base,\n>  \t\t\treturn -1;\n>  \t\t}\n>  \n> -\t\tstrbuf_addf(&keep_msg, \"receive-pack %\"PRIuMAX\" on %s\",\n> -\t\t\t    (uintmax_t)getpid(), hostname);\n> -\n> -\t\tif (write_pack_lock(transaction->quarantine, pack_name.buf, keep_msg.buf)) {\n> -\t\t\tstrbuf_release(&pack_name);\n> -\t\t\tstrbuf_release(&keep_msg);\n> -\t\t\tstrbuf_addstr(err_msg, \"lockfile write failed\");\n> -\t\t\treturn -1;\n> -\t\t}\n\nAnd here we stop writing lockfiles when writing the pack.\n\n> diff --git a/t/t5547-push-quarantine.sh b/t/t5547-push-quarantine.sh\n> index 1b7097179e..cf349eee2c 100755\n> --- a/t/t5547-push-quarantine.sh\n> +++ b/t/t5547-push-quarantine.sh\n> @@ -77,10 +77,9 @@ test_expect_success '.keep file is removed after push' '\n>  \tgit -C keep.git config set receive.unpackLimit 0 &&\n>  \n>  \t# While incoming objects are still quarantined, validate that the\n> -\t# \".keep\" lockfile is present in the quarantine directory.\n> +\t# \".keep\" lockfile is not present in the quarantine directory.\n>  \ttest_hook -C keep.git pre-receive <<-\\EOF &&\n> -\tkeep=\"$(ls \"$GIT_QUARANTINE_PATH\"/pack/pack-*.keep)\" &&\n> -\ttest -f \"$keep\"\n> +\ttest ! -e \"$GIT_QUARANTINE_PATH\"/pack/pack-*.keep\n>  \tEOF\n>  \n>  \t# After quarantined objects are migrated, validate that the \".keep\"\n> @@ -101,4 +100,28 @@ test_expect_success '.keep file is removed after push' '\n>  \ttest_path_is_missing \"$keep\"\n>  '\n>  \n> +test_expect_success '.keep file created outside of process is not removed' '\n> +\ttest_when_finished rm -rf keep.git &&\n> +\tgit init --bare keep.git &&\n> +\n> +\tgit -C keep.git config set receive.unpackLimit 0 &&\n> +\n> +\t# Create a \".keep\" lockfile matching the packfile to simulate a\n> +\t# concurrent git-receive-pack(1) that was already written its lockfile.\n> +\ttest_hook -C keep.git pre-receive <<-\\EOF &&\n> +\tpack=\"$(ls \"$GIT_QUARANTINE_PATH\"/pack/pack-*.pack)\" &&\n> +\tpack=\"${pack##*/}\" &&\n> +\techo \"baz\" >\"objects/pack/${pack%.pack}.keep\"\n> +\tEOF\n> +\n> +\ttest_commit bar &&\n> +\ttest_must_fail git push keep.git HEAD &&\n> +\n> +\t# The \".keep\" lockfile of the concurrent git-receive-pack(1) should be\n> +\t# left in its place.\n> +\tkeep=\"$(ls keep.git/objects/pack/pack-*.keep)\" &&\n> +\ttest_path_is_file \"$keep\" &&\n> +\ttest_grep \"baz\" \"$keep\"\n\nAlso, there should be no corresponding \".pack\" file, right? Do we want\nto assert that, too?\n\nThanks!\n\nPatrick\n\n"},{"id":"555255","messageId":"atJX3IyBTUCx0NWN@denethor","threadId":"66550","inReplyTo":"atHLBS0GGxf_cPmT@pks.im","subject":"Re: [PATCH 1/3] pack-write: lift and expose index-pack report parsing","fromName":"Justin Tobler","fromEmail":"jltobler@gmail.com","sentAt":"2026-10-16T17:30:59Z","receivedAt":"2026-10-16T17:30:59Z","isPatch":true,"sender":{"key":"jltobler@gmail.com","avatar":"https://avatars.githubusercontent.com/u/53454972?v=4"},"body":"On 26/10/16 08:58AM, Patrick Steinhardt wrote:\n> On Thu, Oct 15, 2026 at 09:28:59PM -0500, Justin Tobler wrote:\n> > Once git-index-pack(1) finishes writing a packfile, it reports the\n> > hexadecimal name of that packfile on stdout prefixed with either \"pack\"\n> > or \"keep\" depending on whether a \".keep\" file was also created alongside\n> > it. Callers that are required to manage the lifecycle of \".keep\" files\n> > parse this report via `index_pack_lockfile()`, which provides the path\n> > to the temporary \".keep\" file.\n> > \n> > In a subsequent commit, the \"files\" ODB transaction begins staging\n> > packfiles in a transaction without eagerly creating a \".keep\" file and\n> > must instead read git-index-pack(1) \"pack\" output to get the packfile\n> > name. Extract the generic parsing logic for the git-index-pack(1) output\n> > into `read_index_pack_report()` in preparation accordingly.\n> \n> So if I understand correctly, the intent is to eventually stop creating\n> the \".keep\" file in the quarantine directory? I assume that instead,\n> you'll then manually create that file in the final location before\n> moving around the pack itself.\n\nThat is correct. Instead of migrating the \".keep\" file in the quarantine\nwith all the other object files, the intent is for the \".keep\" file to\nbe manually created during transaction commit first. I'll update the\ncommit message here a bit to allude to this a bit more directly.\n\n> If so that makes sense -- it's kind of pointless to create \".keep\" files\n> in quarantines anyway, nobody but the current process should be touching\n> it anyway.\n\nYup, creating the lockfile in the quarantine doesn't really tell us\nanything. It also makes it a bit more complicated during transaction\ncommit to detect whether there is already a lockfile for the same pack\nthat exists.\n\n> > diff --git a/pack-write.c b/pack-write.c\n> > index 85674e4b72..5f5bb894a5 100644\n> > --- a/pack-write.c\n> > +++ b/pack-write.c\n> > @@ -469,33 +469,68 @@ void fixup_pack_header_footer(const struct git_hash_algo *hash_algo,\n> >  \tfsync_component_or_die(FSYNC_COMPONENT_PACK, pack_fd, pack_name);\n> >  }\n> >  \n> > -char *index_pack_lockfile(struct odb_source *source, int ip_out,\n> > -\t\t\t  int *is_well_formed)\n> > +int read_index_pack_report(struct repository *repo, int ip_out,\n> > +\t\t\t   struct strbuf *pack_name, int *is_keep)\n> >  {\n> > +\tconst int len = repo->hash_algo->hexsz + 6;\n> >  \tchar packname[GIT_MAX_HEXSZ + 6];\n> > -\tconst int len = source->odb->repo->hash_algo->hexsz + 6;\n> > +\tconst char *name;\n> > +\tint keep;\n> \n> Suggestion, please feel free to ignore:\n> \n>     int is_keep_ignored;\n> \n>     if (!is_keep)\n>         is_keep = &is_keep_ignored;\n> \n> Then you can unconditionally use `is_keep` further down, which I think\n> is a bit less awkward.\n\nGood suggestion. Will use in the next version.\n\n> >  \t/*\n> > -\t * The first thing we expect from index-pack's output\n> > -\t * is \"pack\\t%40s\\n\" or \"keep\\t%40s\\n\" (46 bytes) where\n> > -\t * %40s is the newly created pack SHA1 name.  In the \"keep\"\n> > -\t * case, we need it to remove the corresponding .keep file\n> > -\t * later on.  If we don't get that then tough luck with it.\n> > +\t * The first thing we expect from index-pack's output is\n> > +\t * \"pack\\t<name>\\n\" or \"keep\\t<name>\\n\", where <name> is the hexadecimal\n> > +\t * name of the packfile it has just written. The line is therefore five\n> > +\t * bytes of prefix, `hexsz` bytes of name, and a trailing newline. A\n> > +\t * \"keep\" prefix tells us that index-pack has also created the\n> > +\t * corresponding \".keep\" file.\n> >  \t */\n> \n> Okay. I originally thought you only reflow this comment, but you fix it\n> to no longer assume that packfile hashes use SHA1. I would have either\n> pointed this out in the commit message or done this as a preparatory\n> commit.\n\nWill split out the code comment change into a preparatory commit.\n\n> > -\tif (read_in_full(ip_out, packname, len) == len && packname[len-1] == '\\n') {\n> > -\t\tconst char *name;\n> > +\tif (read_in_full(ip_out, packname, len) != len || packname[len-1] != '\\n')\n> > +\t\treturn -1;\n> > +\n> > +\tpackname[len-1] = 0;\n> \n> Missing spaces around `-`.\n\nWill fix.\n\n> > +\tif (skip_prefix(packname, \"pack\\t\", &name))\n> > +\t\tkeep = 0;\n> > +\telse if (skip_prefix(packname, \"keep\\t\", &name))\n> > +\t\tkeep = 1;\n> > +\telse\n> > +\t\treturn -1;\n> > +\n> > +\tstrbuf_addstr(pack_name, name);\n> > +\tif (is_keep)\n> > +\t\t*is_keep = keep;\n> > +\n> > +\treturn 0;\n> > +}\n> > +\n> > +char *index_pack_lockfile(struct odb_source *source, int ip_out,\n> > +\t\t\t  int *is_well_formed)\n> > +{\n> > +\tstruct strbuf pack_name = STRBUF_INIT;\n> > +\tchar *lockfile = NULL;\n> > +\tint is_keep = 0;\n> >  \n> > +\tif (read_index_pack_report(source->odb->repo, ip_out, &pack_name, &is_keep)) {\n> >  \t\tif (is_well_formed)\n> > -\t\t\t*is_well_formed = 1;\n> > -\t\tpackname[len-1] = 0;\n> > -\t\tif (skip_prefix(packname, \"keep\\t\", &name))\n> > -\t\t\treturn xstrfmt(\"%s/pack/pack-%s.keep\",\n> > -\t\t\t\t       source->path, name);\n> > +\t\t\t*is_well_formed = 0;\n> >  \t\treturn NULL;\n> >  \t}\n> > +\n> >  \tif (is_well_formed)\n> > -\t\t*is_well_formed = 0;\n> > -\treturn NULL;\n> > +\t\t*is_well_formed = 1;\n> > +\n> > +\t/*\n> > +\t * If there is no \".keep\" file, then there is no lockfile to return.\n> > +\t */\n> > +\tif (!is_keep) {\n> > +\t\tstrbuf_release(&pack_name);\n> > +\t\treturn NULL;\n> > +\t}\n> \n> Can we maybe have a single exit path responsible for releasing\n> `pack_name`?\n> \n>     /* If there is no \".keep\" file, then there is no lockfile to return. */\n>     if (is_keep)\n>         lockfile = xstrfmt(\"%s/pack/pack-%s.keep\", source->path, pack_name.buf);\n> \n>     strbuf_release(&pack_name);\n>     return lockfile;\n\nThat is indeed much better. Will use in the next version. Thanks.\n\n> > diff --git a/pack.h b/pack.h\n> > index ada506b5c5..ddf2d52934 100644\n> > --- a/pack.h\n> > +++ b/pack.h\n> > @@ -106,6 +106,16 @@ off_t write_pack_header(struct hashfile *f, uint32_t);\n> >  void fixup_pack_header_footer(const struct git_hash_algo *, int,\n> >  \t\t\t      unsigned char *, const char *, uint32_t,\n> >  \t\t\t      unsigned char *, off_t);\n> > +\n> > +/*\n> > + * Read the report that git-index-pack(1) writes to its standard output after\n> > + * the packfile is written. The hexadecimal name of the packfile is appended to\n> > + * `pack_name` and, if `is_keep` is non-NULL, set it to indicate whether a\n> \n> s/set it to/it is set to/\n\nThanks for the careful eyes.\n\n-Justin\n\n"},{"id":"555263","messageId":"atJhElOsxd1KaZJ_@denethor","threadId":"66550","inReplyTo":"atHLDysZmYGyMiKk@pks.im","subject":"Re: [PATCH 2/3] object-file: explicitly write \".keep\" lockfile","fromName":"Justin Tobler","fromEmail":"jltobler@gmail.com","sentAt":"2026-10-16T18:37:50Z","receivedAt":"2026-10-16T18:37:50Z","isPatch":true,"sender":{"key":"jltobler@gmail.com","avatar":"https://avatars.githubusercontent.com/u/53454972?v=4"},"body":"On 26/10/16 08:58AM, Patrick Steinhardt wrote:\n> On Thu, Oct 15, 2026 at 09:29:00PM -0500, Justin Tobler wrote:\n> > diff --git a/object-file.c b/object-file.c\n> > index 210984f825..ebe46d35de 100644\n> > --- a/object-file.c\n> > +++ b/object-file.c\n> > @@ -1256,6 +1256,43 @@ int read_loose_object(struct repository *repo,\n> >  \treturn ret;\n> >  }\n> >  \n> > +static int write_pack_lock(struct odb_source *source, const char *pack_name,\n> > +\t\t\t   const char *keep_msg)\n> \n> Nit: I'd have called this `write_pack_keep()`, but I see that we refer\n> to the files as \"lock\" in `index_pack_lockfile(), too. Should we align\n> those two functions and call this `write_pack_lockfile()`?\n\nYa, that's probably a bit better. Will rename to `write_pack_lockfile()`\nin the next version.\n\n> > +{\n> > +\tstruct strbuf lock_path = STRBUF_INIT;\n> > +\tstruct strbuf contents = STRBUF_INIT;\n> > +\tint ret = -1;\n> > +\tint fd;\n> > +\n> > +\tstrbuf_addf(&lock_path, \"%s/pack/pack-%s.keep\",\n> > +\t\t    source->path, pack_name);\n> > +\tstrbuf_addf(&contents, \"%s\\n\", keep_msg);\n> > +\n> > +\tfd = safe_create_file_with_leading_directories(source->odb->repo,\n> > +\t\t\t\t\t\t       lock_path.buf);\n> \n> The command has just written its packfile, so can't we assume that\n> leading directories have already been created?\n\nYes, with how the function is used currently we can make that\nassumption. The lockfile is always being written into the quarantine\ndirectory after the packfile is written.\n\nI would like to keep this function a bit more generic though because it\ngets reused in the subsequent commit to write the lockfile directly into\nthe primary ODB during transaction commit and there I don't think we can\nassume that the leading directories have already been committed. Using\n`safe_create_file_with_leading_directories()` doesn't really cost us\nanything either because it always tries to immediately create the file\nfirst before creating the leading directories. Ultimately, this is just\na bit more defensive and I was hoping to reduce the changes overall.\n\nI'll leave it as-is here unless others feel strongly it should be moved\nto the last patch.\n\n> > +\tif (fd < 0)\n> > +\t\tgoto out;\n> > +\n> > +\tif (write_in_full(fd, contents.buf, contents.len) < 0) {\n> > +\t\tclose(fd);\n> > +\t\tunlink(lock_path.buf);\n> > +\t\tgoto out;\n> > +\t}\n> > +\n> > +\tif (close(fd)) {\n> \n> `if (close(fd) < 0)`, just to be consistent.\n\nWill update in next version.\n\n[snip]\n> >  \t\tstatus = finish_command(&child);\n> >  \t\tif (status) {\n> > +\t\t\tstrbuf_release(&pack_name);\n> >  \t\t\tstrbuf_addstr(err_msg, \"index-pack abnormal exit\");\n> >  \t\t\treturn -1;\n> >  \t\t}\n> >  \n> > +\t\tif (ret) {\n> > +\t\t\tstrbuf_release(&pack_name);\n> > +\t\t\tstrbuf_addstr(err_msg, \"index-pack failed to report pack name\");\n> > +\t\t\treturn -1;\n> > +\t\t}\n> > +\n> > +\t\tstrbuf_addf(&keep_msg, \"receive-pack %\"PRIuMAX\" on %s\",\n> > +\t\t\t    (uintmax_t)getpid(), hostname);\n> > +\n> > +\t\tif (write_pack_lock(transaction->quarantine, pack_name.buf, keep_msg.buf)) {\n> > +\t\t\tstrbuf_release(&pack_name);\n> > +\t\t\tstrbuf_release(&keep_msg);\n> > +\t\t\tstrbuf_addstr(err_msg, \"lockfile write failed\");\n> > +\t\t\treturn -1;\n> > +\t\t}\n> \n> ... then write the lockfile ourselves.\n> \n> By the way, the error handling here starts to become unwieldy. Can we\n> maybe have a common exit path that releases all resources for us?\n\nThat's fair. I'll clean this up a bit in the next version. Thanks.\n\n> > +\t\tALLOC_GROW(transaction->pack_lockfiles,\n> > +\t\t\t   transaction->pack_lockfiles_nr + 1,\n> > +\t\t\t   transaction->pack_lockfiles_alloc);\n> > +\n> > +\t\t/*\n> > +\t\t * The lockfile filepath is expected to be the final location of\n> > +\t\t * the \".keep\" file after being migrated to the main ODB source.\n> > +\t\t * This ensures the lockfile can be found and removed later\n> > +\t\t * after the ODB transaction has been committed.\n> > +\t\t */\n> > +\t\tstrbuf_addf(&final_path, \"%s/pack/pack-%s.keep\",\n> > +\t\t\t    base->source->path, pack_name.buf);\n> > +\t\ttransaction->pack_lockfiles[transaction->pack_lockfiles_nr++] =\n> > +\t\t\tregister_tempfile(final_path.buf);\n> \n> So here we retain the status quo, which is to record the lockfile in the\n> final location even though it doesn't exist there yet. And that can lead\n> to us pruning a pack lockfile from a different process.\n\nYup, that is exactly right. :)\n\n-Justin\n\n"},{"id":"555271","messageId":"atJxHsn_OxJ_HtWW@denethor","threadId":"66550","inReplyTo":"atHLFIp8MI729ZkP@pks.im","subject":"Re: [PATCH 3/3] object-file: write \".keep\" lockfiles during transaction commit","fromName":"Justin Tobler","fromEmail":"jltobler@gmail.com","sentAt":"2026-10-16T19:22:39Z","receivedAt":"2026-10-16T19:22:39Z","isPatch":true,"sender":{"key":"jltobler@gmail.com","avatar":"https://avatars.githubusercontent.com/u/53454972?v=4"},"body":"On 26/10/16 08:58AM, Patrick Steinhardt wrote:\n> On Thu, Oct 15, 2026 at 09:29:01PM -0500, Justin Tobler wrote:\n> > The \"files\" ODB backend writes a \".keep\" lockfile into the transaction\n> > quarantine directory via `odb_transaction_files_write_pack()`. When the\n> > ODB transaction is committed, the \".keep\" lockfile is migrated to the\n> > primary ODB alongside its packfile. Writing the temporary lockfile in\n> > this manner has a couple of downsides:\n> > \n> >   - Failing to migrate the lockfile during transaction commit due to a\n> >     concurrent process already acquiring the same lock still allows the\n> >     rest of the transaction to be migrated into the primary ODB. This\n> >     dirties the primary ODB with new objects even though no references\n> >     were updated and the operation failed.\n> \n> I'm not fully sure I understand this downside. Are you saying that we\n> fail to migrate the lockfile, then continue migrating everything else\n> only to then fail the whole transaction?\n> \n> ... skipping ahead a bit...\n> \n> Oh, I get it now. You propose to first acquire all lockfiles before\n> migrating anything so that we cannot conflict with a concurrent process\n> that aims to claim the same locks.\n> \n> That will of course mean that we will fail the transaction for any\n> concurrent process that happens to write the same packfiles. But based\n> on what you claim that was already the case anyway, but now we notice\n> early and thus won't commit parts of the transaction. Makes sense.\n\nThat is correct. Previously when migrating the quarantined objects, if a\nmatching \".keep\" file was already present in the primary ODB,\n`check_collision()` would detect the preexisting \".keep\" lockfile and\nbubble up the error to `tmp_objdir_migrate()`, but still let the\nmigration continue. This ultimately causes `odb_transaction_commit()` to\nalso return an error.\n\nFor git-receive-pack(1), an error during ODB transaction commit already\naborts the operation. The main change here is that by manually creating the\nlockfiles during transaction commit, we can avoid the migration step\nentirely if the lockfiles are not able to be created first.\n\nI'll try rewording the commit message here to be a bit more clear about\nthis.\n\n[snip]\n> > diff --git a/object-file.c b/object-file.c\n> > index ebe46d35de..39e6299be1 100644\n> > --- a/object-file.c\n> > +++ b/object-file.c\n> > @@ -1321,6 +1357,9 @@ static int odb_transaction_files_commit(struct odb_transaction *base)\n> >  \t\tdelete_tempfile(&temp);\n> >  \t\tstrbuf_release(&temp_path);\n> >  \n> > +\t\tif (write_pack_locks(transaction))\n> > +\t\t\treturn error(_(\"unable to lock packfiles\"));\n> \n> Okay, we'll now write lockfiles before migrating any of the data. So if\n> any of the files already happens to exist, we'll bail out here. It would\n> have been great to maybe also provide a proper failure message via errno\n> for example, but okay.\n\nYa, good suggestion. I think I'll update the `write_pack_lockfile()`\nhelper in patch 2 to provide this additional context. There I can also\nprint the specific lockfile causing the issue.\n\n[snip]\n> > @@ -101,4 +100,28 @@ test_expect_success '.keep file is removed after push' '\n> >  \ttest_path_is_missing \"$keep\"\n> >  '\n> >  \n> > +test_expect_success '.keep file created outside of process is not removed' '\n> > +\ttest_when_finished rm -rf keep.git &&\n> > +\tgit init --bare keep.git &&\n> > +\n> > +\tgit -C keep.git config set receive.unpackLimit 0 &&\n> > +\n> > +\t# Create a \".keep\" lockfile matching the packfile to simulate a\n> > +\t# concurrent git-receive-pack(1) that was already written its lockfile.\n> > +\ttest_hook -C keep.git pre-receive <<-\\EOF &&\n> > +\tpack=\"$(ls \"$GIT_QUARANTINE_PATH\"/pack/pack-*.pack)\" &&\n> > +\tpack=\"${pack##*/}\" &&\n> > +\techo \"baz\" >\"objects/pack/${pack%.pack}.keep\"\n> > +\tEOF\n> > +\n> > +\ttest_commit bar &&\n> > +\ttest_must_fail git push keep.git HEAD &&\n> > +\n> > +\t# The \".keep\" lockfile of the concurrent git-receive-pack(1) should be\n> > +\t# left in its place.\n> > +\tkeep=\"$(ls keep.git/objects/pack/pack-*.keep)\" &&\n> > +\ttest_path_is_file \"$keep\" &&\n> > +\ttest_grep \"baz\" \"$keep\"\n> \n> Also, there should be no corresponding \".pack\" file, right? Do we want\n> to assert that, too?\n\nThat is correct. I'll add an assertion in the next version to make sure\nthe packfile is not migrated too.\n\nThanks for the review,\n-Justin\n\n"},{"id":"555281","messageId":"20261016213716.593278-1-jltobler@gmail.com","threadId":"66550","inReplyTo":"20261016022901.3838402-1-jltobler@gmail.com","subject":"[PATCH v2 0/4] object-file: fix \".keep\" lockfile ownership in ODB transactions","fromName":"Justin Tobler","fromEmail":"jltobler@gmail.com","sentAt":"2026-10-16T21:37:12Z","receivedAt":"2026-10-16T21:37:12Z","isPatch":true,"sender":{"key":"jltobler@gmail.com","avatar":"https://avatars.githubusercontent.com/u/53454972?v=4"},"body":"Greetings,\n\nThis series addresses a bug report[1] regarding a git-receive-pack(1)\nprocess deleting \".keep\" lockfiles of with the same name but from\nanother concurrent git-receive-pack(1) process receiving an identical\npackfile. Deleting the \".keep\" lockfile for another process leaves its\nnewly written packfile vulnerable to deletion by another concurrent\nhousekeeping process.\n\nThe root of the problem here is that the temporary \".keep\" lockfiles are\ngenerated and registered in the object quarantine prior to being written\nin the primary ODB. This means that even if the ODB transaction never\ncommits and exits early, the tempfile atexit(3) handler still runs and\ntries to delete the lockfile at its expected final location. This\nblindly deletes any file matching regardless of whether is was created\nby the process.\n\nThe first three patches are some preparatory changes so that\n`odb_transaction_files_write_pack()` is able to start explicitly\ncreating the lockfiles without relying on git-index-pack(1). Patch four\nmigrates lockfile generation to happen during ODB transaction commit to\nensure the lockfile is actually able to be written before registering it\nas a tempfile.\n\nChanges since V1:\n  - Split out comment change into separate commit as suggested.\n  - Improved error handling and logic flow in a couple of functions.\n  - Renamed `write_pack_lock()` to `write_pack_lockfile()`.\n  - Added better error messages and errno reporting to\n    `write_pack_lockfile()` helper.\n  - Adapted tests case to assert that packfile is not migrated.\n  - Clarified some commit messages and fixed some typos.\n\nThanks,\n-Justin\n\n[1]: <932e8e425aecfbd33c1e5caf66c80a0226abacba.1789385483.git.gitgitgadget@gmail.com>\n\nJustin Tobler (4):\n  pack-write: make index-pack output comment hash agnostic\n  pack-write: lift and expose index-pack report parsing\n  object-file: explicitly write \".keep\" lockfile\n  object-file: write \".keep\" lockfiles during transaction commit\n\n object-file.c              | 127 +++++++++++++++++++++++++++++--------\n pack-write.c               |  64 ++++++++++++++-----\n pack.h                     |  10 +++\n t/t5547-push-quarantine.sh |  30 ++++++++-\n 4 files changed, 184 insertions(+), 47 deletions(-)\n\nRange-diff against v1:\n-:  ---------- > 1:  b9197cbf3b pack-write: make index-pack output comment hash agnostic\n1:  62521f3220 ! 2:  e27679ac16 pack-write: lift and expose index-pack report parsing\n    @@ Commit message\n         parse this report via `index_pack_lockfile()`, which provides the path\n         to the temporary \".keep\" file.\n     \n    -    In a subsequent commit, the \"files\" ODB transaction begins staging\n    -    packfiles in a transaction without eagerly creating a \".keep\" file and\n    -    must instead read git-index-pack(1) \"pack\" output to get the packfile\n    -    name. Extract the generic parsing logic for the git-index-pack(1) output\n    -    into `read_index_pack_report()` in preparation accordingly.\n    +    In a subsequent commit, the \"files\" ODB transaction begins creating the\n    +    pack lockfiles during transaction commit instead of eagerly staging them\n    +    in the transaction quarantine directory with the packfile. Extract the\n    +    generic parsing logic for the git-index-pack(1) output into\n    +    `read_index_pack_report()` in preparation for recording the names of\n    +    newly written packfiles.\n     \n         Signed-off-by: Justin Tobler <jltobler@gmail.com>\n     \n    @@ pack-write.c: void fixup_pack_header_footer(const struct git_hash_algo *hash_alg\n     +\tconst int len = repo->hash_algo->hexsz + 6;\n      \tchar packname[GIT_MAX_HEXSZ + 6];\n     -\tconst int len = source->odb->repo->hash_algo->hexsz + 6;\n    ++\tint is_keep_ignored;\n     +\tconst char *name;\n    -+\tint keep;\n    ++\n    ++\tif (!is_keep)\n    ++\t\tis_keep = &is_keep_ignored;\n      \n      \t/*\n    --\t * The first thing we expect from index-pack's output\n    --\t * is \"pack\\t%40s\\n\" or \"keep\\t%40s\\n\" (46 bytes) where\n    --\t * %40s is the newly created pack SHA1 name.  In the \"keep\"\n    --\t * case, we need it to remove the corresponding .keep file\n    --\t * later on.  If we don't get that then tough luck with it.\n    -+\t * The first thing we expect from index-pack's output is\n    -+\t * \"pack\\t<name>\\n\" or \"keep\\t<name>\\n\", where <name> is the hexadecimal\n    -+\t * name of the packfile it has just written. The line is therefore five\n    -+\t * bytes of prefix, `hexsz` bytes of name, and a trailing newline. A\n    -+\t * \"keep\" prefix tells us that index-pack has also created the\n    -+\t * corresponding \".keep\" file.\n    + \t * The first thing we expect from index-pack's output is\n    +@@ pack-write.c: char *index_pack_lockfile(struct odb_source *source, int ip_out,\n    + \t * bytes of prefix, `hexsz` bytes of name, and a trailing newline. A\n    + \t * \"keep\" prefix tells us that index-pack has also created the\n    + \t * corresponding \".keep\" file.\n    +-\t *\n    +-\t * In the \"keep\" case, we need it to remove the corresponding \".keep\"\n    +-\t * file later on. If we don't get that then tough luck with it.\n      \t */\n     -\tif (read_in_full(ip_out, packname, len) == len && packname[len-1] == '\\n') {\n     -\t\tconst char *name;\n    -+\tif (read_in_full(ip_out, packname, len) != len || packname[len-1] != '\\n')\n    ++\tif (read_in_full(ip_out, packname, len) != len || packname[len - 1] != '\\n')\n     +\t\treturn -1;\n     +\n    -+\tpackname[len-1] = 0;\n    ++\tpackname[len - 1] = 0;\n     +\tif (skip_prefix(packname, \"pack\\t\", &name))\n    -+\t\tkeep = 0;\n    ++\t\t*is_keep = 0;\n     +\telse if (skip_prefix(packname, \"keep\\t\", &name))\n    -+\t\tkeep = 1;\n    ++\t\t*is_keep = 1;\n     +\telse\n     +\t\treturn -1;\n     +\n     +\tstrbuf_addstr(pack_name, name);\n    -+\tif (is_keep)\n    -+\t\t*is_keep = keep;\n     +\n     +\treturn 0;\n     +}\n    -+\n    + \n     +char *index_pack_lockfile(struct odb_source *source, int ip_out,\n     +\t\t\t  int *is_well_formed)\n     +{\n     +\tstruct strbuf pack_name = STRBUF_INIT;\n     +\tchar *lockfile = NULL;\n     +\tint is_keep = 0;\n    - \n    ++\n     +\tif (read_index_pack_report(source->odb->repo, ip_out, &pack_name, &is_keep)) {\n      \t\tif (is_well_formed)\n     -\t\t\t*is_well_formed = 1;\n    @@ pack-write.c: void fixup_pack_header_footer(const struct git_hash_algo *hash_alg\n     -\treturn NULL;\n     +\t\t*is_well_formed = 1;\n     +\n    -+\t/*\n    -+\t * If there is no \".keep\" file, then there is no lockfile to return.\n    -+\t */\n    -+\tif (!is_keep) {\n    -+\t\tstrbuf_release(&pack_name);\n    -+\t\treturn NULL;\n    -+\t}\n    -+\n    -+\tlockfile = xstrfmt(\"%s/pack/pack-%s.keep\", source->path, pack_name.buf);\n    ++\t/* If there is no \".keep\" file, then there is no lockfile to return. */\n    ++\tif (is_keep)\n    ++\t\tlockfile = xstrfmt(\"%s/pack/pack-%s.keep\", source->path, pack_name.buf);\n     +\n     +\tstrbuf_release(&pack_name);\n     +\treturn lockfile;\n    @@ pack.h: off_t write_pack_header(struct hashfile *f, uint32_t);\n     +/*\n     + * Read the report that git-index-pack(1) writes to its standard output after\n     + * the packfile is written. The hexadecimal name of the packfile is appended to\n    -+ * `pack_name` and, if `is_keep` is non-NULL, set it to indicate whether a\n    ++ * `pack_name` and, if `is_keep` is non-NULL, it is set to indicate whether a\n     + * \".keep\" file was written. Return 0 on success and a negative error code on\n     + * failure.\n     + */\n2:  beebbc65d8 ! 3:  9236493740 object-file: explicitly write \".keep\" lockfile\n    @@ object-file.c: int read_loose_object(struct repository *repo,\n      \treturn ret;\n      }\n      \n    -+static int write_pack_lock(struct odb_source *source, const char *pack_name,\n    -+\t\t\t   const char *keep_msg)\n    ++static int write_pack_lockfile(struct odb_source *source, const char *pack_name,\n    ++\t\t\t       const char *keep_msg)\n     +{\n     +\tstruct strbuf lock_path = STRBUF_INIT;\n     +\tstruct strbuf contents = STRBUF_INIT;\n    @@ object-file.c: int read_loose_object(struct repository *repo,\n     +\n     +\tfd = safe_create_file_with_leading_directories(source->odb->repo,\n     +\t\t\t\t\t\t       lock_path.buf);\n    -+\tif (fd < 0)\n    ++\tif (fd < 0) {\n    ++\t\terror_errno(_(\"unable to create '%s'\"), lock_path.buf);\n     +\t\tgoto out;\n    ++\t}\n     +\n     +\tif (write_in_full(fd, contents.buf, contents.len) < 0) {\n    ++\t\terror_errno(_(\"unable to write '%s'\"), lock_path.buf);\n     +\t\tclose(fd);\n     +\t\tunlink(lock_path.buf);\n     +\t\tgoto out;\n     +\t}\n     +\n    -+\tif (close(fd)) {\n    ++\tif (close(fd) < 0) {\n    ++\t\terror_errno(_(\"unable to close '%s'\"), lock_path.buf);\n     +\t\tunlink(lock_path.buf);\n     +\t\tgoto out;\n     +\t}\n    @@ object-file.c: int read_loose_object(struct repository *repo,\n      {\n      \tstruct odb_transaction_files *transaction =\n     @@ object-file.c: static int odb_transaction_files_write_pack(struct odb_transaction *base,\n    - \t\t\treturn -1;\n    + \t\tcontainer_of(base, struct odb_transaction_files, base);\n    + \tstruct repository *repo = base->source->odb->repo;\n    + \tstruct child_process child = CHILD_PROCESS_INIT;\n    ++\tstruct strbuf final_path = STRBUF_INIT;\n    ++\tstruct strbuf pack_name = STRBUF_INIT;\n    ++\tstruct strbuf keep_msg = STRBUF_INIT;\n    + \tstruct pack_header hdr;\n    + \tconst char *hdr_err;\n    + \tint err_fd = opts->err_fd;\n    ++\tint ret = -1;\n    + \tint status;\n    + \n    + \thdr_err = parse_pack_header(&hdr, pack_fd);\n    +@@ object-file.c: static int odb_transaction_files_write_pack(struct odb_transaction *base,\n      \t\t}\n      \t} else {\n    -+\t\tstruct strbuf final_path = STRBUF_INIT;\n    -+\t\tstruct strbuf pack_name = STRBUF_INIT;\n    -+\t\tstruct strbuf keep_msg = STRBUF_INIT;\n      \t\tchar hostname[HOST_NAME_MAX + 1];\n     -\t\tchar *lockfile;\n    -+\t\tint ret;\n      \n      \t\tstrvec_pushl(&child.args, \"index-pack\", \"--stdin\", NULL);\n      \t\tpush_header_arg(&child.args, &hdr);\n    @@ object-file.c: static int odb_transaction_files_write_pack(struct odb_transactio\n      \n      \t\tstatus = finish_command(&child);\n      \t\tif (status) {\n    -+\t\t\tstrbuf_release(&pack_name);\n      \t\t\tstrbuf_addstr(err_msg, \"index-pack abnormal exit\");\n    - \t\t\treturn -1;\n    - \t\t}\n    - \n    -+\t\tif (ret) {\n    -+\t\t\tstrbuf_release(&pack_name);\n    -+\t\t\tstrbuf_addstr(err_msg, \"index-pack failed to report pack name\");\n    -+\t\t\treturn -1;\n    +-\t\t\treturn -1;\n    ++\t\t\tgoto out;\n     +\t\t}\n     +\n    ++\t\tif (ret) {\n    ++\t\t\tstrbuf_addstr(err_msg, \"index-pack failed to report pack name\");\n    ++\t\t\tgoto out;\n    + \t\t}\n    + \n     +\t\tstrbuf_addf(&keep_msg, \"receive-pack %\"PRIuMAX\" on %s\",\n     +\t\t\t    (uintmax_t)getpid(), hostname);\n     +\n    -+\t\tif (write_pack_lock(transaction->quarantine, pack_name.buf, keep_msg.buf)) {\n    -+\t\t\tstrbuf_release(&pack_name);\n    -+\t\t\tstrbuf_release(&keep_msg);\n    ++\t\tif (write_pack_lockfile(transaction->quarantine, pack_name.buf,\n    ++\t\t\t\t\tkeep_msg.buf)) {\n     +\t\t\tstrbuf_addstr(err_msg, \"lockfile write failed\");\n    -+\t\t\treturn -1;\n    ++\t\t\tgoto out;\n     +\t\t}\n     +\n     +\t\tALLOC_GROW(transaction->pack_lockfiles,\n    @@ object-file.c: static int odb_transaction_files_write_pack(struct odb_transactio\n      \t\todb_source_prepare(transaction->quarantine,\n      \t\t\t\t   ODB_PREPARE_FLUSH_CACHES);\n     +\n    -+\t\tstrbuf_release(&final_path);\n    -+\t\tstrbuf_release(&pack_name);\n    -+\t\tstrbuf_release(&keep_msg);\n      \t}\n      \n    - \treturn 0;\n    +-\treturn 0;\n    ++\tret = 0;\n    ++\n    ++out:\n    ++\tstrbuf_release(&final_path);\n    ++\tstrbuf_release(&pack_name);\n    ++\tstrbuf_release(&keep_msg);\n    ++\n    ++\treturn ret;\n    + }\n    + \n    + static int odb_transaction_files_finalize(struct odb_transaction *base)\n3:  735b922865 ! 4:  c1a175e2b9 object-file: write \".keep\" lockfiles during transaction commit\n    @@ Commit message\n         primary ODB alongside its packfile. Writing the temporary lockfile in\n         this manner has a couple of downsides:\n     \n    -      - Failing to migrate the lockfile during transaction commit due to a\n    -        concurrent process already acquiring the same lock still allows the\n    -        rest of the transaction to be migrated into the primary ODB. This\n    -        dirties the primary ODB with new objects even though no references\n    -        were updated and the operation failed.\n    +      - Migrating quarantined objects via `tmp_objdir_migrate()` doesn't\n    +        immediately stop if a \".keep\" lockfile already exists for the same\n    +        packfile in the primary ODB and instead allows all the remaining\n    +        objects to be migrated. So even though `odb_transaction_commit()`\n    +        ultimately returns an error here, the objects are needlessly written\n    +        to the primary ODB anyways.\n     \n           - Even more concerning, the \".keep\" tempfile atexit(3) handler tries\n             to remove any lockfile of the same name regardless of whether it was\n    @@ object-file.c: struct odb_transaction_files {\n     @@ object-file.c: int read_loose_object(struct repository *repo,\n      }\n      \n    - static int write_pack_lock(struct odb_source *source, const char *pack_name,\n    --\t\t\t   const char *keep_msg)\n    -+\t\t\t   const char *keep_msg, struct tempfile **out)\n    + static int write_pack_lockfile(struct odb_source *source, const char *pack_name,\n    +-\t\t\t       const char *keep_msg)\n    ++\t\t\t       const char *keep_msg, struct tempfile **out)\n      {\n      \tstruct strbuf lock_path = STRBUF_INIT;\n      \tstruct strbuf contents = STRBUF_INIT;\n    -@@ object-file.c: static int write_pack_lock(struct odb_source *source, const char *pack_name,\n    +@@ object-file.c: static int write_pack_lockfile(struct odb_source *source, const char *pack_name,\n      \t\tgoto out;\n      \t}\n      \n    @@ object-file.c: static int write_pack_lock(struct odb_source *source, const char\n      \tret = 0;\n      \n      out:\n    -@@ object-file.c: static int write_pack_lock(struct odb_source *source, const char *pack_name,\n    +@@ object-file.c: static int write_pack_lockfile(struct odb_source *source, const char *pack_name,\n      \treturn ret;\n      }\n      \n    -+static int write_pack_locks(struct odb_transaction_files *transaction)\n    ++static int write_pack_lockfiles(struct odb_transaction_files *transaction)\n     +{\n     +\tstruct strbuf keep_msg = STRBUF_INIT;\n     +\tchar hostname[HOST_NAME_MAX + 1];\n    @@ object-file.c: static int write_pack_lock(struct odb_source *source, const char\n     +\tfor (size_t i = 0; i < transaction->pack_names.nr; i++) {\n     +\t\tstruct tempfile *lock;\n     +\n    -+\t\tret = write_pack_lock(transaction->base.source,\n    -+\t\t\t\t      transaction->pack_names.v[i],\n    -+\t\t\t\t      keep_msg.buf, &lock);\n    ++\t\tret = write_pack_lockfile(transaction->base.source,\n    ++\t\t\t\t\t  transaction->pack_names.v[i],\n    ++\t\t\t\t\t  keep_msg.buf, &lock);\n     +\t\tif (ret < 0)\n     +\t\t\tbreak;\n     +\n    @@ object-file.c: static int odb_transaction_files_commit(struct odb_transaction *b\n      \t\tdelete_tempfile(&temp);\n      \t\tstrbuf_release(&temp_path);\n      \n    -+\t\tif (write_pack_locks(transaction))\n    ++\t\tif (write_pack_lockfiles(transaction))\n     +\t\t\treturn error(_(\"unable to lock packfiles\"));\n     +\n      \t\t/*\n      \t\t * Make the object files visible in the primary ODB after their data is\n      \t\t * fully durable.\n    +@@ object-file.c: static int odb_transaction_files_write_pack(struct odb_transaction *base,\n    + \t\tcontainer_of(base, struct odb_transaction_files, base);\n    + \tstruct repository *repo = base->source->odb->repo;\n    + \tstruct child_process child = CHILD_PROCESS_INIT;\n    +-\tstruct strbuf final_path = STRBUF_INIT;\n    + \tstruct strbuf pack_name = STRBUF_INIT;\n    +-\tstruct strbuf keep_msg = STRBUF_INIT;\n    + \tstruct pack_header hdr;\n    + \tconst char *hdr_err;\n    + \tint err_fd = opts->err_fd;\n     @@ object-file.c: static int odb_transaction_files_write_pack(struct odb_transaction *base,\n      \t\t\treturn -1;\n      \t\t}\n      \t} else {\n    --\t\tstruct strbuf final_path = STRBUF_INIT;\n    - \t\tstruct strbuf pack_name = STRBUF_INIT;\n    --\t\tstruct strbuf keep_msg = STRBUF_INIT;\n     -\t\tchar hostname[HOST_NAME_MAX + 1];\n    - \t\tint ret;\n    - \n    +-\n      \t\tstrvec_pushl(&child.args, \"index-pack\", \"--stdin\", NULL);\n      \t\tpush_header_arg(&child.args, &hdr);\n      \n    @@ object-file.c: static int odb_transaction_files_write_pack(struct odb_transactio\n      \t\t\tstrvec_push(&child.args, \"--show-resolving-progress\");\n      \t\tif (err_fd)\n     @@ object-file.c: static int odb_transaction_files_write_pack(struct odb_transaction *base,\n    - \t\t\treturn -1;\n    + \t\t\tgoto out;\n      \t\t}\n      \n     -\t\tstrbuf_addf(&keep_msg, \"receive-pack %\"PRIuMAX\" on %s\",\n     -\t\t\t    (uintmax_t)getpid(), hostname);\n     -\n    --\t\tif (write_pack_lock(transaction->quarantine, pack_name.buf, keep_msg.buf)) {\n    --\t\t\tstrbuf_release(&pack_name);\n    --\t\t\tstrbuf_release(&keep_msg);\n    +-\t\tif (write_pack_lockfile(transaction->quarantine, pack_name.buf,\n    +-\t\t\t\t\tkeep_msg.buf)) {\n     -\t\t\tstrbuf_addstr(err_msg, \"lockfile write failed\");\n    --\t\t\treturn -1;\n    +-\t\t\tgoto out;\n     -\t\t}\n     -\n     -\t\tALLOC_GROW(transaction->pack_lockfiles,\n    @@ object-file.c: static int odb_transaction_files_write_pack(struct odb_transactio\n      \n      \t\todb_source_prepare(transaction->quarantine,\n      \t\t\t\t   ODB_PREPARE_FLUSH_CACHES);\n    - \n    --\t\tstrbuf_release(&final_path);\n    - \t\tstrbuf_release(&pack_name);\n    --\t\tstrbuf_release(&keep_msg);\n    +-\n      \t}\n      \n    - \treturn 0;\n    + \tret = 0;\n    + \n    + out:\n    +-\tstrbuf_release(&final_path);\n    + \tstrbuf_release(&pack_name);\n    +-\tstrbuf_release(&keep_msg);\n    + \n    + \treturn ret;\n    + }\n     @@ object-file.c: static int odb_transaction_files_finalize(struct odb_transaction *base)\n      \t\tret |= delete_tempfile(&transaction->pack_lockfiles[i]);\n      \n    @@ t/t5547-push-quarantine.sh: test_expect_success '.keep file is removed after pus\n     +\tgit -C keep.git config set receive.unpackLimit 0 &&\n     +\n     +\t# Create a \".keep\" lockfile matching the packfile to simulate a\n    -+\t# concurrent git-receive-pack(1) that was already written its lockfile.\n    ++\t# concurrent git-receive-pack(1) that has already written its lockfile.\n     +\ttest_hook -C keep.git pre-receive <<-\\EOF &&\n     +\tpack=\"$(ls \"$GIT_QUARANTINE_PATH\"/pack/pack-*.pack)\" &&\n     +\tpack=\"${pack##*/}\" &&\n    @@ t/t5547-push-quarantine.sh: test_expect_success '.keep file is removed after pus\n     +\t# left in its place.\n     +\tkeep=\"$(ls keep.git/objects/pack/pack-*.keep)\" &&\n     +\ttest_path_is_file \"$keep\" &&\n    -+\ttest_grep \"baz\" \"$keep\"\n    ++\ttest_grep \"baz\" \"$keep\" &&\n    ++\ttest_path_is_missing \"${keep%.keep}.pack\"\n     +'\n     +\n      test_done\n\nbase-commit: f57c9a73884400380bddd172acc88b569834f909\n-- \n2.56.0.173.gf57c9a7388\n\n\n"},{"id":"555282","messageId":"20261016213716.593278-2-jltobler@gmail.com","threadId":"66550","inReplyTo":"20261016213716.593278-1-jltobler@gmail.com","subject":"[PATCH v2 1/4] pack-write: make index-pack output comment hash agnostic","fromName":"Justin Tobler","fromEmail":"jltobler@gmail.com","sentAt":"2026-10-16T21:37:13Z","receivedAt":"2026-10-16T21:37:13Z","isPatch":true,"sender":{"key":"jltobler@gmail.com","avatar":"https://avatars.githubusercontent.com/u/53454972?v=4"},"body":"The code comment in `index_pack_lockfile()` explains the expected format\nof output from git-index-pack(1). As written it explicitly mentions\nSHA1, but both the git-index-pack(1) output and the parsing are not\nspecific to SHA1 and also support SHA256.\n\nUpdate the comment explanation to be hash agnostic accordingly.\n\nSigned-off-by: Justin Tobler <jltobler@gmail.com>\n---\n pack-write.c | 14 +++++++++-----\n 1 file changed, 9 insertions(+), 5 deletions(-)\n\ndiff --git a/pack-write.c b/pack-write.c\nindex 85674e4b72..b1311c9923 100644\n--- a/pack-write.c\n+++ b/pack-write.c\n@@ -476,11 +476,15 @@ char *index_pack_lockfile(struct odb_source *source, int ip_out,\n \tconst int len = source->odb->repo->hash_algo->hexsz + 6;\n \n \t/*\n-\t * The first thing we expect from index-pack's output\n-\t * is \"pack\\t%40s\\n\" or \"keep\\t%40s\\n\" (46 bytes) where\n-\t * %40s is the newly created pack SHA1 name.  In the \"keep\"\n-\t * case, we need it to remove the corresponding .keep file\n-\t * later on.  If we don't get that then tough luck with it.\n+\t * The first thing we expect from index-pack's output is\n+\t * \"pack\\t<name>\\n\" or \"keep\\t<name>\\n\", where <name> is the hexadecimal\n+\t * name of the packfile it has just written. The line is therefore five\n+\t * bytes of prefix, `hexsz` bytes of name, and a trailing newline. A\n+\t * \"keep\" prefix tells us that index-pack has also created the\n+\t * corresponding \".keep\" file.\n+\t *\n+\t * In the \"keep\" case, we need it to remove the corresponding \".keep\"\n+\t * file later on. If we don't get that then tough luck with it.\n \t */\n \tif (read_in_full(ip_out, packname, len) == len && packname[len-1] == '\\n') {\n \t\tconst char *name;\n-- \n2.56.0.173.gf57c9a7388\n\n\n"},{"id":"555283","messageId":"20261016213716.593278-3-jltobler@gmail.com","threadId":"66550","inReplyTo":"20261016213716.593278-1-jltobler@gmail.com","subject":"[PATCH v2 2/4] pack-write: lift and expose index-pack report parsing","fromName":"Justin Tobler","fromEmail":"jltobler@gmail.com","sentAt":"2026-10-16T21:37:14Z","receivedAt":"2026-10-16T21:37:14Z","isPatch":true,"sender":{"key":"jltobler@gmail.com","avatar":"https://avatars.githubusercontent.com/u/53454972?v=4"},"body":"Once git-index-pack(1) finishes writing a packfile, it reports the\nhexadecimal name of that packfile on stdout prefixed with either \"pack\"\nor \"keep\" depending on whether a \".keep\" file was also created alongside\nit. Callers that are required to manage the lifecycle of \".keep\" files\nparse this report via `index_pack_lockfile()`, which provides the path\nto the temporary \".keep\" file.\n\nIn a subsequent commit, the \"files\" ODB transaction begins creating the\npack lockfiles during transaction commit instead of eagerly staging them\nin the transaction quarantine directory with the packfile. Extract the\ngeneric parsing logic for the git-index-pack(1) output into\n`read_index_pack_report()` in preparation for recording the names of\nnewly written packfiles.\n\nSigned-off-by: Justin Tobler <jltobler@gmail.com>\n---\n pack-write.c | 56 ++++++++++++++++++++++++++++++++++++++--------------\n pack.h       | 10 ++++++++++\n 2 files changed, 51 insertions(+), 15 deletions(-)\n\ndiff --git a/pack-write.c b/pack-write.c\nindex b1311c9923..2ed33448ba 100644\n--- a/pack-write.c\n+++ b/pack-write.c\n@@ -469,11 +469,16 @@ void fixup_pack_header_footer(const struct git_hash_algo *hash_algo,\n \tfsync_component_or_die(FSYNC_COMPONENT_PACK, pack_fd, pack_name);\n }\n \n-char *index_pack_lockfile(struct odb_source *source, int ip_out,\n-\t\t\t  int *is_well_formed)\n+int read_index_pack_report(struct repository *repo, int ip_out,\n+\t\t\t   struct strbuf *pack_name, int *is_keep)\n {\n+\tconst int len = repo->hash_algo->hexsz + 6;\n \tchar packname[GIT_MAX_HEXSZ + 6];\n-\tconst int len = source->odb->repo->hash_algo->hexsz + 6;\n+\tint is_keep_ignored;\n+\tconst char *name;\n+\n+\tif (!is_keep)\n+\t\tis_keep = &is_keep_ignored;\n \n \t/*\n \t * The first thing we expect from index-pack's output is\n@@ -482,24 +487,45 @@ char *index_pack_lockfile(struct odb_source *source, int ip_out,\n \t * bytes of prefix, `hexsz` bytes of name, and a trailing newline. A\n \t * \"keep\" prefix tells us that index-pack has also created the\n \t * corresponding \".keep\" file.\n-\t *\n-\t * In the \"keep\" case, we need it to remove the corresponding \".keep\"\n-\t * file later on. If we don't get that then tough luck with it.\n \t */\n-\tif (read_in_full(ip_out, packname, len) == len && packname[len-1] == '\\n') {\n-\t\tconst char *name;\n+\tif (read_in_full(ip_out, packname, len) != len || packname[len - 1] != '\\n')\n+\t\treturn -1;\n+\n+\tpackname[len - 1] = 0;\n+\tif (skip_prefix(packname, \"pack\\t\", &name))\n+\t\t*is_keep = 0;\n+\telse if (skip_prefix(packname, \"keep\\t\", &name))\n+\t\t*is_keep = 1;\n+\telse\n+\t\treturn -1;\n+\n+\tstrbuf_addstr(pack_name, name);\n+\n+\treturn 0;\n+}\n \n+char *index_pack_lockfile(struct odb_source *source, int ip_out,\n+\t\t\t  int *is_well_formed)\n+{\n+\tstruct strbuf pack_name = STRBUF_INIT;\n+\tchar *lockfile = NULL;\n+\tint is_keep = 0;\n+\n+\tif (read_index_pack_report(source->odb->repo, ip_out, &pack_name, &is_keep)) {\n \t\tif (is_well_formed)\n-\t\t\t*is_well_formed = 1;\n-\t\tpackname[len-1] = 0;\n-\t\tif (skip_prefix(packname, \"keep\\t\", &name))\n-\t\t\treturn xstrfmt(\"%s/pack/pack-%s.keep\",\n-\t\t\t\t       source->path, name);\n+\t\t\t*is_well_formed = 0;\n \t\treturn NULL;\n \t}\n+\n \tif (is_well_formed)\n-\t\t*is_well_formed = 0;\n-\treturn NULL;\n+\t\t*is_well_formed = 1;\n+\n+\t/* If there is no \".keep\" file, then there is no lockfile to return. */\n+\tif (is_keep)\n+\t\tlockfile = xstrfmt(\"%s/pack/pack-%s.keep\", source->path, pack_name.buf);\n+\n+\tstrbuf_release(&pack_name);\n+\treturn lockfile;\n }\n \n /*\ndiff --git a/pack.h b/pack.h\nindex ada506b5c5..2f2ce97c11 100644\n--- a/pack.h\n+++ b/pack.h\n@@ -106,6 +106,16 @@ off_t write_pack_header(struct hashfile *f, uint32_t);\n void fixup_pack_header_footer(const struct git_hash_algo *, int,\n \t\t\t      unsigned char *, const char *, uint32_t,\n \t\t\t      unsigned char *, off_t);\n+\n+/*\n+ * Read the report that git-index-pack(1) writes to its standard output after\n+ * the packfile is written. The hexadecimal name of the packfile is appended to\n+ * `pack_name` and, if `is_keep` is non-NULL, it is set to indicate whether a\n+ * \".keep\" file was written. Return 0 on success and a negative error code on\n+ * failure.\n+ */\n+int read_index_pack_report(struct repository *repo, int ip_out,\n+\t\t\t   struct strbuf *pack_name, int *is_keep);\n char *index_pack_lockfile(struct odb_source *source, int fd,\n \t\t\t  int *is_well_formed);\n \n-- \n2.56.0.173.gf57c9a7388\n\n\n"},{"id":"555284","messageId":"20261016213716.593278-4-jltobler@gmail.com","threadId":"66550","inReplyTo":"20261016213716.593278-1-jltobler@gmail.com","subject":"[PATCH v2 3/4] object-file: explicitly write \".keep\" lockfile","fromName":"Justin Tobler","fromEmail":"jltobler@gmail.com","sentAt":"2026-10-16T21:37:15Z","receivedAt":"2026-10-16T21:37:15Z","isPatch":true,"sender":{"key":"jltobler@gmail.com","avatar":"https://avatars.githubusercontent.com/u/53454972?v=4"},"body":"The `odb_transaction_write_pack()` interface for the \"files\" backend\nrelies on git-index-pack(1) to write the specified packfile into an ODB\ntransaction as a packfile. To prevent a concurrent housekeeping\noperation from deleting the newly written packfile prior to the\nreferences being updated, a \".keep\" lockfile is also generated by\ngit-index-pack(1) via the `--keep` option. Consequently, the \".keep\"\nlockfile is written into the ODB transaction alongside the packfile and\nlater migrated together to the primary ODB during transaction commit.\n\nIn a subsequent commit, writing the lockfile is deferred until\ntransaction commit and thus cannot rely on git-index-pack(1) to write\nit. In preparation for this, update `odb_transaction_files_write_pack()`\nto explicitly generate the same \".keep\" lockfile itself.\n\nSigned-off-by: Justin Tobler <jltobler@gmail.com>\n---\n object-file.c | 108 ++++++++++++++++++++++++++++++++++++++++----------\n 1 file changed, 86 insertions(+), 22 deletions(-)\n\ndiff --git a/object-file.c b/object-file.c\nindex 210984f825..07a61c610a 100644\n--- a/object-file.c\n+++ b/object-file.c\n@@ -1256,6 +1256,47 @@ int read_loose_object(struct repository *repo,\n \treturn ret;\n }\n \n+static int write_pack_lockfile(struct odb_source *source, const char *pack_name,\n+\t\t\t       const char *keep_msg)\n+{\n+\tstruct strbuf lock_path = STRBUF_INIT;\n+\tstruct strbuf contents = STRBUF_INIT;\n+\tint ret = -1;\n+\tint fd;\n+\n+\tstrbuf_addf(&lock_path, \"%s/pack/pack-%s.keep\",\n+\t\t    source->path, pack_name);\n+\tstrbuf_addf(&contents, \"%s\\n\", keep_msg);\n+\n+\tfd = safe_create_file_with_leading_directories(source->odb->repo,\n+\t\t\t\t\t\t       lock_path.buf);\n+\tif (fd < 0) {\n+\t\terror_errno(_(\"unable to create '%s'\"), lock_path.buf);\n+\t\tgoto out;\n+\t}\n+\n+\tif (write_in_full(fd, contents.buf, contents.len) < 0) {\n+\t\terror_errno(_(\"unable to write '%s'\"), lock_path.buf);\n+\t\tclose(fd);\n+\t\tunlink(lock_path.buf);\n+\t\tgoto out;\n+\t}\n+\n+\tif (close(fd) < 0) {\n+\t\terror_errno(_(\"unable to close '%s'\"), lock_path.buf);\n+\t\tunlink(lock_path.buf);\n+\t\tgoto out;\n+\t}\n+\n+\tret = 0;\n+\n+out:\n+\tstrbuf_release(&lock_path);\n+\tstrbuf_release(&contents);\n+\n+\treturn ret;\n+}\n+\n static int odb_transaction_files_commit(struct odb_transaction *base)\n {\n \tstruct odb_transaction_files *transaction =\n@@ -1348,9 +1389,13 @@ static int odb_transaction_files_write_pack(struct odb_transaction *base,\n \t\tcontainer_of(base, struct odb_transaction_files, base);\n \tstruct repository *repo = base->source->odb->repo;\n \tstruct child_process child = CHILD_PROCESS_INIT;\n+\tstruct strbuf final_path = STRBUF_INIT;\n+\tstruct strbuf pack_name = STRBUF_INIT;\n+\tstruct strbuf keep_msg = STRBUF_INIT;\n \tstruct pack_header hdr;\n \tconst char *hdr_err;\n \tint err_fd = opts->err_fd;\n+\tint ret = -1;\n \tint status;\n \n \thdr_err = parse_pack_header(&hdr, pack_fd);\n@@ -1390,17 +1435,12 @@ static int odb_transaction_files_write_pack(struct odb_transaction *base,\n \t\t}\n \t} else {\n \t\tchar hostname[HOST_NAME_MAX + 1];\n-\t\tchar *lockfile;\n \n \t\tstrvec_pushl(&child.args, \"index-pack\", \"--stdin\", NULL);\n \t\tpush_header_arg(&child.args, &hdr);\n \n \t\tif (xgethostname(hostname, sizeof(hostname)))\n \t\t\txsnprintf(hostname, sizeof(hostname), \"localhost\");\n-\t\tstrvec_pushf(&child.args,\n-\t\t\t     \"--keep=receive-pack %\"PRIuMAX\" on %s\",\n-\t\t\t     (uintmax_t)getpid(),\n-\t\t\t     hostname);\n \n \t\tif (!opts->quiet && err_fd)\n \t\t\tstrvec_push(&child.args, \"--show-resolving-progress\");\n@@ -1424,34 +1464,58 @@ static int odb_transaction_files_write_pack(struct odb_transaction *base,\n \t\t\treturn -1;\n \t\t}\n \n-\t\t/*\n-\t\t * The lockfile filepath is expected to be the final location of\n-\t\t * the \".keep\" file after being migrated to the main ODB source.\n-\t\t * This ensures the lockfile can be found and removed later\n-\t\t * after the ODB transaction has been committed.\n-\t\t */\n-\t\tlockfile = index_pack_lockfile(base->source, child.out, NULL);\n-\t\tif (lockfile) {\n-\t\t\tALLOC_GROW(transaction->pack_lockfiles,\n-\t\t\t\t   transaction->pack_lockfiles_nr + 1,\n-\t\t\t\t   transaction->pack_lockfiles_alloc);\n-\t\t\ttransaction->pack_lockfiles[transaction->pack_lockfiles_nr++] =\n-\t\t\t\tregister_tempfile(lockfile);\n-\t\t\tfree(lockfile);\n-\t\t}\n+\t\tret = read_index_pack_report(repo, child.out, &pack_name, NULL);\n+\n \t\tclose(child.out);\n \n \t\tstatus = finish_command(&child);\n \t\tif (status) {\n \t\t\tstrbuf_addstr(err_msg, \"index-pack abnormal exit\");\n-\t\t\treturn -1;\n+\t\t\tgoto out;\n+\t\t}\n+\n+\t\tif (ret) {\n+\t\t\tstrbuf_addstr(err_msg, \"index-pack failed to report pack name\");\n+\t\t\tgoto out;\n \t\t}\n \n+\t\tstrbuf_addf(&keep_msg, \"receive-pack %\"PRIuMAX\" on %s\",\n+\t\t\t    (uintmax_t)getpid(), hostname);\n+\n+\t\tif (write_pack_lockfile(transaction->quarantine, pack_name.buf,\n+\t\t\t\t\tkeep_msg.buf)) {\n+\t\t\tstrbuf_addstr(err_msg, \"lockfile write failed\");\n+\t\t\tgoto out;\n+\t\t}\n+\n+\t\tALLOC_GROW(transaction->pack_lockfiles,\n+\t\t\t   transaction->pack_lockfiles_nr + 1,\n+\t\t\t   transaction->pack_lockfiles_alloc);\n+\n+\t\t/*\n+\t\t * The lockfile filepath is expected to be the final location of\n+\t\t * the \".keep\" file after being migrated to the main ODB source.\n+\t\t * This ensures the lockfile can be found and removed later\n+\t\t * after the ODB transaction has been committed.\n+\t\t */\n+\t\tstrbuf_addf(&final_path, \"%s/pack/pack-%s.keep\",\n+\t\t\t    base->source->path, pack_name.buf);\n+\t\ttransaction->pack_lockfiles[transaction->pack_lockfiles_nr++] =\n+\t\t\tregister_tempfile(final_path.buf);\n+\n \t\todb_source_prepare(transaction->quarantine,\n \t\t\t\t   ODB_PREPARE_FLUSH_CACHES);\n+\n \t}\n \n-\treturn 0;\n+\tret = 0;\n+\n+out:\n+\tstrbuf_release(&final_path);\n+\tstrbuf_release(&pack_name);\n+\tstrbuf_release(&keep_msg);\n+\n+\treturn ret;\n }\n \n static int odb_transaction_files_finalize(struct odb_transaction *base)\n-- \n2.56.0.173.gf57c9a7388\n\n\n"},{"id":"555285","messageId":"20261016213716.593278-5-jltobler@gmail.com","threadId":"66550","inReplyTo":"20261016213716.593278-1-jltobler@gmail.com","subject":"[PATCH v2 4/4] object-file: write \".keep\" lockfiles during transaction commit","fromName":"Justin Tobler","fromEmail":"jltobler@gmail.com","sentAt":"2026-10-16T21:37:16Z","receivedAt":"2026-10-16T21:37:16Z","isPatch":true,"sender":{"key":"jltobler@gmail.com","avatar":"https://avatars.githubusercontent.com/u/53454972?v=4"},"body":"The \"files\" ODB backend writes a \".keep\" lockfile into the transaction\nquarantine directory via `odb_transaction_files_write_pack()`. When the\nODB transaction is committed, the \".keep\" lockfile is migrated to the\nprimary ODB alongside its packfile. Writing the temporary lockfile in\nthis manner has a couple of downsides:\n\n  - Migrating quarantined objects via `tmp_objdir_migrate()` doesn't\n    immediately stop if a \".keep\" lockfile already exists for the same\n    packfile in the primary ODB and instead allows all the remaining\n    objects to be migrated. So even though `odb_transaction_commit()`\n    ultimately returns an error here, the objects are needlessly written\n    to the primary ODB anyways.\n\n  - Even more concerning, the \".keep\" tempfile atexit(3) handler tries\n    to remove any lockfile of the same name regardless of whether it was\n    created by the same process. If the \".keep\" lockfile of the same\n    name for a concurrent process gets deleted prematurely, its packfile\n    becomes at risk of being deleted by concurrent housekeeping.\n\nStop creating/registering the temporary lockfiles when writing the\npackfile to the transaction and instead do so when committing the\ntransaction. This ensures the lockfile is properly acquired prior to\nmigrating objects and that only the owning process can delete its own\nlockfiles.\n\nReported-by: Qin ShiCheng <qeesung@live.com>\nSigned-off-by: Justin Tobler <jltobler@gmail.com>\n---\n object-file.c              | 77 +++++++++++++++++++++-----------------\n t/t5547-push-quarantine.sh | 30 +++++++++++++--\n 2 files changed, 70 insertions(+), 37 deletions(-)\n\ndiff --git a/object-file.c b/object-file.c\nindex 07a61c610a..b7465adf04 100644\n--- a/object-file.c\n+++ b/object-file.c\n@@ -492,6 +492,7 @@ struct odb_transaction_files {\n \tstruct transaction_packfile packfile;\n \tconst char *prefix;\n \n+\tstruct strvec pack_names;\n \tstruct tempfile **pack_lockfiles;\n \tsize_t pack_lockfiles_nr;\n \tsize_t pack_lockfiles_alloc;\n@@ -1257,7 +1258,7 @@ int read_loose_object(struct repository *repo,\n }\n \n static int write_pack_lockfile(struct odb_source *source, const char *pack_name,\n-\t\t\t       const char *keep_msg)\n+\t\t\t       const char *keep_msg, struct tempfile **out)\n {\n \tstruct strbuf lock_path = STRBUF_INIT;\n \tstruct strbuf contents = STRBUF_INIT;\n@@ -1288,6 +1289,7 @@ static int write_pack_lockfile(struct odb_source *source, const char *pack_name,\n \t\tgoto out;\n \t}\n \n+\t*out = register_tempfile(lock_path.buf);\n \tret = 0;\n \n out:\n@@ -1297,6 +1299,40 @@ static int write_pack_lockfile(struct odb_source *source, const char *pack_name,\n \treturn ret;\n }\n \n+static int write_pack_lockfiles(struct odb_transaction_files *transaction)\n+{\n+\tstruct strbuf keep_msg = STRBUF_INIT;\n+\tchar hostname[HOST_NAME_MAX + 1];\n+\tint ret = 0;\n+\n+\tif (!transaction->pack_names.nr)\n+\t\treturn 0;\n+\n+\tif (xgethostname(hostname, sizeof(hostname)))\n+\t\txsnprintf(hostname, sizeof(hostname), \"localhost\");\n+\tstrbuf_addf(&keep_msg, \"receive-pack %\"PRIuMAX\" on %s\",\n+\t\t    (uintmax_t)getpid(), hostname);\n+\n+\tfor (size_t i = 0; i < transaction->pack_names.nr; i++) {\n+\t\tstruct tempfile *lock;\n+\n+\t\tret = write_pack_lockfile(transaction->base.source,\n+\t\t\t\t\t  transaction->pack_names.v[i],\n+\t\t\t\t\t  keep_msg.buf, &lock);\n+\t\tif (ret < 0)\n+\t\t\tbreak;\n+\n+\t\tALLOC_GROW(transaction->pack_lockfiles,\n+\t\t\t   transaction->pack_lockfiles_nr + 1,\n+\t\t\t   transaction->pack_lockfiles_alloc);\n+\t\ttransaction->pack_lockfiles[transaction->pack_lockfiles_nr++] = lock;\n+\t}\n+\n+\tstrbuf_release(&keep_msg);\n+\n+\treturn ret;\n+}\n+\n static int odb_transaction_files_commit(struct odb_transaction *base)\n {\n \tstruct odb_transaction_files *transaction =\n@@ -1325,6 +1361,9 @@ static int odb_transaction_files_commit(struct odb_transaction *base)\n \t\tdelete_tempfile(&temp);\n \t\tstrbuf_release(&temp_path);\n \n+\t\tif (write_pack_lockfiles(transaction))\n+\t\t\treturn error(_(\"unable to lock packfiles\"));\n+\n \t\t/*\n \t\t * Make the object files visible in the primary ODB after their data is\n \t\t * fully durable.\n@@ -1389,9 +1428,7 @@ static int odb_transaction_files_write_pack(struct odb_transaction *base,\n \t\tcontainer_of(base, struct odb_transaction_files, base);\n \tstruct repository *repo = base->source->odb->repo;\n \tstruct child_process child = CHILD_PROCESS_INIT;\n-\tstruct strbuf final_path = STRBUF_INIT;\n \tstruct strbuf pack_name = STRBUF_INIT;\n-\tstruct strbuf keep_msg = STRBUF_INIT;\n \tstruct pack_header hdr;\n \tconst char *hdr_err;\n \tint err_fd = opts->err_fd;\n@@ -1434,14 +1471,9 @@ static int odb_transaction_files_write_pack(struct odb_transaction *base,\n \t\t\treturn -1;\n \t\t}\n \t} else {\n-\t\tchar hostname[HOST_NAME_MAX + 1];\n-\n \t\tstrvec_pushl(&child.args, \"index-pack\", \"--stdin\", NULL);\n \t\tpush_header_arg(&child.args, &hdr);\n \n-\t\tif (xgethostname(hostname, sizeof(hostname)))\n-\t\t\txsnprintf(hostname, sizeof(hostname), \"localhost\");\n-\n \t\tif (!opts->quiet && err_fd)\n \t\t\tstrvec_push(&child.args, \"--show-resolving-progress\");\n \t\tif (err_fd)\n@@ -1479,41 +1511,16 @@ static int odb_transaction_files_write_pack(struct odb_transaction *base,\n \t\t\tgoto out;\n \t\t}\n \n-\t\tstrbuf_addf(&keep_msg, \"receive-pack %\"PRIuMAX\" on %s\",\n-\t\t\t    (uintmax_t)getpid(), hostname);\n-\n-\t\tif (write_pack_lockfile(transaction->quarantine, pack_name.buf,\n-\t\t\t\t\tkeep_msg.buf)) {\n-\t\t\tstrbuf_addstr(err_msg, \"lockfile write failed\");\n-\t\t\tgoto out;\n-\t\t}\n-\n-\t\tALLOC_GROW(transaction->pack_lockfiles,\n-\t\t\t   transaction->pack_lockfiles_nr + 1,\n-\t\t\t   transaction->pack_lockfiles_alloc);\n-\n-\t\t/*\n-\t\t * The lockfile filepath is expected to be the final location of\n-\t\t * the \".keep\" file after being migrated to the main ODB source.\n-\t\t * This ensures the lockfile can be found and removed later\n-\t\t * after the ODB transaction has been committed.\n-\t\t */\n-\t\tstrbuf_addf(&final_path, \"%s/pack/pack-%s.keep\",\n-\t\t\t    base->source->path, pack_name.buf);\n-\t\ttransaction->pack_lockfiles[transaction->pack_lockfiles_nr++] =\n-\t\t\tregister_tempfile(final_path.buf);\n+\t\tstrvec_push(&transaction->pack_names, pack_name.buf);\n \n \t\todb_source_prepare(transaction->quarantine,\n \t\t\t\t   ODB_PREPARE_FLUSH_CACHES);\n-\n \t}\n \n \tret = 0;\n \n out:\n-\tstrbuf_release(&final_path);\n \tstrbuf_release(&pack_name);\n-\tstrbuf_release(&keep_msg);\n \n \treturn ret;\n }\n@@ -1528,6 +1535,7 @@ static int odb_transaction_files_finalize(struct odb_transaction *base)\n \t\tret |= delete_tempfile(&transaction->pack_lockfiles[i]);\n \n \tfree(transaction->pack_lockfiles);\n+\tstrvec_clear(&transaction->pack_names);\n \n \treturn ret;\n }\n@@ -1560,6 +1568,7 @@ int odb_transaction_files_begin(struct odb_source *source,\n \ttransaction->base.write_pack = odb_transaction_files_write_pack;\n \ttransaction->base.env = odb_transaction_files_env;\n \ttransaction->flags = flags;\n+\tstrvec_init(&transaction->pack_names);\n \n \ttransaction->prefix = \"bulk-fsync\";\n \tif (flags & ODB_TRANSACTION_RECEIVE) {\ndiff --git a/t/t5547-push-quarantine.sh b/t/t5547-push-quarantine.sh\nindex 1b7097179e..6c14dfc42b 100755\n--- a/t/t5547-push-quarantine.sh\n+++ b/t/t5547-push-quarantine.sh\n@@ -77,10 +77,9 @@ test_expect_success '.keep file is removed after push' '\n \tgit -C keep.git config set receive.unpackLimit 0 &&\n \n \t# While incoming objects are still quarantined, validate that the\n-\t# \".keep\" lockfile is present in the quarantine directory.\n+\t# \".keep\" lockfile is not present in the quarantine directory.\n \ttest_hook -C keep.git pre-receive <<-\\EOF &&\n-\tkeep=\"$(ls \"$GIT_QUARANTINE_PATH\"/pack/pack-*.keep)\" &&\n-\ttest -f \"$keep\"\n+\ttest ! -e \"$GIT_QUARANTINE_PATH\"/pack/pack-*.keep\n \tEOF\n \n \t# After quarantined objects are migrated, validate that the \".keep\"\n@@ -101,4 +100,29 @@ test_expect_success '.keep file is removed after push' '\n \ttest_path_is_missing \"$keep\"\n '\n \n+test_expect_success '.keep file created outside of process is not removed' '\n+\ttest_when_finished rm -rf keep.git &&\n+\tgit init --bare keep.git &&\n+\n+\tgit -C keep.git config set receive.unpackLimit 0 &&\n+\n+\t# Create a \".keep\" lockfile matching the packfile to simulate a\n+\t# concurrent git-receive-pack(1) that has already written its lockfile.\n+\ttest_hook -C keep.git pre-receive <<-\\EOF &&\n+\tpack=\"$(ls \"$GIT_QUARANTINE_PATH\"/pack/pack-*.pack)\" &&\n+\tpack=\"${pack##*/}\" &&\n+\techo \"baz\" >\"objects/pack/${pack%.pack}.keep\"\n+\tEOF\n+\n+\ttest_commit bar &&\n+\ttest_must_fail git push keep.git HEAD &&\n+\n+\t# The \".keep\" lockfile of the concurrent git-receive-pack(1) should be\n+\t# left in its place.\n+\tkeep=\"$(ls keep.git/objects/pack/pack-*.keep)\" &&\n+\ttest_path_is_file \"$keep\" &&\n+\ttest_grep \"baz\" \"$keep\" &&\n+\ttest_path_is_missing \"${keep%.keep}.pack\"\n+'\n+\n test_done\n-- \n2.56.0.173.gf57c9a7388\n\n\n"},{"id":"555380","messageId":"atW1Vh0b406cxCZJ@pks.im","threadId":"66550","inReplyTo":"20261016213716.593278-4-jltobler@gmail.com","subject":"Re: [PATCH v2 3/4] object-file: explicitly write \".keep\" lockfile","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-10-19T06:14:46Z","receivedAt":"2026-10-19T06:14:46Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"On Fri, Oct 16, 2026 at 04:37:15PM -0500, Justin Tobler wrote:\n> diff --git a/object-file.c b/object-file.c\n> index 210984f825..07a61c610a 100644\n> --- a/object-file.c\n> +++ b/object-file.c\n> @@ -1256,6 +1256,47 @@ int read_loose_object(struct repository *repo,\n>  \treturn ret;\n>  }\n>  \n> +static int write_pack_lockfile(struct odb_source *source, const char *pack_name,\n> +\t\t\t       const char *keep_msg)\n> +{\n> +\tstruct strbuf lock_path = STRBUF_INIT;\n> +\tstruct strbuf contents = STRBUF_INIT;\n> +\tint ret = -1;\n> +\tint fd;\n> +\n> +\tstrbuf_addf(&lock_path, \"%s/pack/pack-%s.keep\",\n> +\t\t    source->path, pack_name);\n> +\tstrbuf_addf(&contents, \"%s\\n\", keep_msg);\n> +\n> +\tfd = safe_create_file_with_leading_directories(source->odb->repo,\n> +\t\t\t\t\t\t       lock_path.buf);\n> +\tif (fd < 0) {\n> +\t\terror_errno(_(\"unable to create '%s'\"), lock_path.buf);\n\nRight, `safe_create_file_with_leading_directories()` guarantees that\nopen(3p) was the last function that was called when returning, so we\nknow that `errno` would be properly set.\n\n> +\t\tgoto out;\n> +\t}\n> +\n> +\tif (write_in_full(fd, contents.buf, contents.len) < 0) {\n> +\t\terror_errno(_(\"unable to write '%s'\"), lock_path.buf);\n> +\t\tclose(fd);\n> +\t\tunlink(lock_path.buf);\n> +\t\tgoto out;\n> +\t}\n> +\n> +\tif (close(fd) < 0) {\n> +\t\terror_errno(_(\"unable to close '%s'\"), lock_path.buf);\n> +\t\tunlink(lock_path.buf);\n> +\t\tgoto out;\n> +\t}\n\nNit: now that all error paths call `error_errno()` I would have\npreferred to keep `ret` uninitialized and then assign the return value\nof `error_errno()` to it. But that's a minor detail only that does not\nwarrant a reroll.\n\n> @@ -1424,34 +1464,58 @@ static int odb_transaction_files_write_pack(struct odb_transaction *base,\n>  \t\t\treturn -1;\n>  \t\t}\n>  \n> -\t\t/*\n> -\t\t * The lockfile filepath is expected to be the final location of\n> -\t\t * the \".keep\" file after being migrated to the main ODB source.\n> -\t\t * This ensures the lockfile can be found and removed later\n> -\t\t * after the ODB transaction has been committed.\n> -\t\t */\n> -\t\tlockfile = index_pack_lockfile(base->source, child.out, NULL);\n> -\t\tif (lockfile) {\n> -\t\t\tALLOC_GROW(transaction->pack_lockfiles,\n> -\t\t\t\t   transaction->pack_lockfiles_nr + 1,\n> -\t\t\t\t   transaction->pack_lockfiles_alloc);\n> -\t\t\ttransaction->pack_lockfiles[transaction->pack_lockfiles_nr++] =\n> -\t\t\t\tregister_tempfile(lockfile);\n> -\t\t\tfree(lockfile);\n> -\t\t}\n> +\t\tret = read_index_pack_report(repo, child.out, &pack_name, NULL);\n> +\n>  \t\tclose(child.out);\n>  \n>  \t\tstatus = finish_command(&child);\n>  \t\tif (status) {\n>  \t\t\tstrbuf_addstr(err_msg, \"index-pack abnormal exit\");\n> -\t\t\treturn -1;\n> +\t\t\tgoto out;\n> +\t\t}\n> +\n> +\t\tif (ret) {\n> +\t\t\tstrbuf_addstr(err_msg, \"index-pack failed to report pack name\");\n> +\t\t\tgoto out;\n>  \t\t}\n>  \n> +\t\tstrbuf_addf(&keep_msg, \"receive-pack %\"PRIuMAX\" on %s\",\n> +\t\t\t    (uintmax_t)getpid(), hostname);\n> +\n> +\t\tif (write_pack_lockfile(transaction->quarantine, pack_name.buf,\n> +\t\t\t\t\tkeep_msg.buf)) {\n> +\t\t\tstrbuf_addstr(err_msg, \"lockfile write failed\");\n> +\t\t\tgoto out;\n> +\t\t}\n> +\n> +\t\tALLOC_GROW(transaction->pack_lockfiles,\n> +\t\t\t   transaction->pack_lockfiles_nr + 1,\n> +\t\t\t   transaction->pack_lockfiles_alloc);\n> +\n> +\t\t/*\n> +\t\t * The lockfile filepath is expected to be the final location of\n> +\t\t * the \".keep\" file after being migrated to the main ODB source.\n> +\t\t * This ensures the lockfile can be found and removed later\n> +\t\t * after the ODB transaction has been committed.\n> +\t\t */\n> +\t\tstrbuf_addf(&final_path, \"%s/pack/pack-%s.keep\",\n> +\t\t\t    base->source->path, pack_name.buf);\n> +\t\ttransaction->pack_lockfiles[transaction->pack_lockfiles_nr++] =\n> +\t\t\tregister_tempfile(final_path.buf);\n> +\n>  \t\todb_source_prepare(transaction->quarantine,\n>  \t\t\t\t   ODB_PREPARE_FLUSH_CACHES);\n> +\n\nNit: empty newline before curly brace.\n\n>  \t}\n>  \n> -\treturn 0;\n> +\tret = 0;\n> +\n> +out:\n> +\tstrbuf_release(&final_path);\n> +\tstrbuf_release(&pack_name);\n> +\tstrbuf_release(&keep_msg);\n> +\n> +\treturn ret;\n>  }\n\nThis reads a lot better now with the unified exit path, thanks!\n\nPatrick\n\n"},{"id":"555381","messageId":"atW1Ww74i6De0l8q@pks.im","threadId":"66550","inReplyTo":"20261016213716.593278-5-jltobler@gmail.com","subject":"Re: [PATCH v2 4/4] object-file: write \".keep\" lockfiles during transaction commit","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-10-19T06:14:51Z","receivedAt":"2026-10-19T06:14:51Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"On Fri, Oct 16, 2026 at 04:37:16PM -0500, Justin Tobler wrote:\n> The \"files\" ODB backend writes a \".keep\" lockfile into the transaction\n> quarantine directory via `odb_transaction_files_write_pack()`. When the\n> ODB transaction is committed, the \".keep\" lockfile is migrated to the\n> primary ODB alongside its packfile. Writing the temporary lockfile in\n> this manner has a couple of downsides:\n> \n>   - Migrating quarantined objects via `tmp_objdir_migrate()` doesn't\n>     immediately stop if a \".keep\" lockfile already exists for the same\n>     packfile in the primary ODB and instead allows all the remaining\n>     objects to be migrated. So even though `odb_transaction_commit()`\n>     ultimately returns an error here, the objects are needlessly written\n>     to the primary ODB anyways.\n\nThanks, this is a lot clearer now.\n\nPatrick\n\n"},{"id":"555475","messageId":"ataOnaLL1AzafGld@denethor","threadId":"66550","inReplyTo":"atW1Vh0b406cxCZJ@pks.im","subject":"Re: [PATCH v2 3/4] object-file: explicitly write \".keep\" lockfile","fromName":"Justin Tobler","fromEmail":"jltobler@gmail.com","sentAt":"2026-10-19T21:44:27Z","receivedAt":"2026-10-19T21:44:27Z","isPatch":true,"sender":{"key":"jltobler@gmail.com","avatar":"https://avatars.githubusercontent.com/u/53454972?v=4"},"body":"On 26/10/19 08:14AM, Patrick Steinhardt wrote:\n> On Fri, Oct 16, 2026 at 04:37:15PM -0500, Justin Tobler wrote:\n\n> Nit: now that all error paths call `error_errno()` I would have\n> preferred to keep `ret` uninitialized and then assign the return value\n> of `error_errno()` to it. But that's a minor detail only that does not\n> warrant a reroll.\n\nI've queued up locally this change a long with the removal of the extra\nnewline. If I don't get any other comments, I may just go ahead and send\nit up.\n\nThanks,\n-Justin\n\n"},{"id":"555534","messageId":"20261020132900.1208343-1-jltobler@gmail.com","threadId":"66550","inReplyTo":"20261016213716.593278-1-jltobler@gmail.com","subject":"[PATCH v3 0/4] object-file: fix \".keep\" lockfile ownership in ODB transactions","fromName":"Justin Tobler","fromEmail":"jltobler@gmail.com","sentAt":"2026-10-20T13:28:56Z","receivedAt":"2026-10-20T13:28:56Z","isPatch":true,"sender":{"key":"jltobler@gmail.com","avatar":"https://avatars.githubusercontent.com/u/53454972?v=4"},"body":"Greetings,\n\nThis series addresses a bug report[1] regarding a git-receive-pack(1)\nprocess deleting \".keep\" lockfiles of with the same name but from\nanother concurrent git-receive-pack(1) process receiving an identical\npackfile. Deleting the \".keep\" lockfile for another process leaves its\nnewly written packfile vulnerable to deletion by another concurrent\nhousekeeping process.\n\nThe root of the problem here is that the temporary \".keep\" lockfiles are\ngenerated and registered in the object quarantine prior to being written\nin the primary ODB. This means that even if the ODB transaction never\ncommits and exits early, the tempfile atexit(3) handler still runs and\ntries to delete the lockfile at its expected final location. This\nblindly deletes any file matching regardless of whether is was created\nby the process.\n\nThe first three patches are some preparatory changes so that\n`odb_transaction_files_write_pack()` is able to start explicitly\ncreating the lockfiles without relying on git-index-pack(1). Patch four\nmigrates lockfile generation to happen during ODB transaction commit to\nensure the lockfile is actually able to be written before registering it\nas a tempfile.\n\nChanges since V2:\n  - In `write_pack_lockfile()` the error is now explicitly assigned to\n    `ret`.\n  - Removed an added newline that was not needed.\n\nChanges since V1:\n  - Split out comment change into separate commit as suggested.\n  - Improved error handling and logic flow in a couple of functions.\n  - Renamed `write_pack_lock()` to `write_pack_lockfile()`.\n  - Added better error messages and errno reporting to\n    `write_pack_lockfile()` helper.\n  - Adapted tests case to assert that packfile is not migrated.\n  - Clarified some commit messages and fixed some typos.\n\nThanks,\n-Justin\n\n[1]: <932e8e425aecfbd33c1e5caf66c80a0226abacba.1789385483.git.gitgitgadget@gmail.com>\n\nJustin Tobler (4):\n  pack-write: make index-pack output comment hash agnostic\n  pack-write: lift and expose index-pack report parsing\n  object-file: explicitly write \".keep\" lockfile\n  object-file: write \".keep\" lockfiles during transaction commit\n\n object-file.c              | 126 +++++++++++++++++++++++++++++--------\n pack-write.c               |  64 ++++++++++++++-----\n pack.h                     |  10 +++\n t/t5547-push-quarantine.sh |  30 ++++++++-\n 4 files changed, 183 insertions(+), 47 deletions(-)\n\nRange-diff against v2:\n1:  b9197cbf3b = 1:  b9197cbf3b pack-write: make index-pack output comment hash agnostic\n2:  e27679ac16 = 2:  e27679ac16 pack-write: lift and expose index-pack report parsing\n3:  9236493740 ! 3:  e7f72aa53d object-file: explicitly write \".keep\" lockfile\n    @@ object-file.c: int read_loose_object(struct repository *repo,\n     +{\n     +\tstruct strbuf lock_path = STRBUF_INIT;\n     +\tstruct strbuf contents = STRBUF_INIT;\n    -+\tint ret = -1;\n    ++\tint ret = 0;\n     +\tint fd;\n     +\n     +\tstrbuf_addf(&lock_path, \"%s/pack/pack-%s.keep\",\n    @@ object-file.c: int read_loose_object(struct repository *repo,\n     +\tfd = safe_create_file_with_leading_directories(source->odb->repo,\n     +\t\t\t\t\t\t       lock_path.buf);\n     +\tif (fd < 0) {\n    -+\t\terror_errno(_(\"unable to create '%s'\"), lock_path.buf);\n    ++\t\tret = error_errno(_(\"unable to create '%s'\"), lock_path.buf);\n     +\t\tgoto out;\n     +\t}\n     +\n     +\tif (write_in_full(fd, contents.buf, contents.len) < 0) {\n    -+\t\terror_errno(_(\"unable to write '%s'\"), lock_path.buf);\n    ++\t\tret = error_errno(_(\"unable to write '%s'\"), lock_path.buf);\n     +\t\tclose(fd);\n     +\t\tunlink(lock_path.buf);\n     +\t\tgoto out;\n     +\t}\n     +\n     +\tif (close(fd) < 0) {\n    -+\t\terror_errno(_(\"unable to close '%s'\"), lock_path.buf);\n    ++\t\tret = error_errno(_(\"unable to close '%s'\"), lock_path.buf);\n     +\t\tunlink(lock_path.buf);\n     +\t\tgoto out;\n     +\t}\n     +\n    -+\tret = 0;\n    -+\n     +out:\n     +\tstrbuf_release(&lock_path);\n     +\tstrbuf_release(&contents);\n    @@ object-file.c: static int odb_transaction_files_write_pack(struct odb_transactio\n      \t\t\tstrbuf_addstr(err_msg, \"index-pack abnormal exit\");\n     -\t\t\treturn -1;\n     +\t\t\tgoto out;\n    -+\t\t}\n    -+\n    + \t\t}\n    + \n     +\t\tif (ret) {\n     +\t\t\tstrbuf_addstr(err_msg, \"index-pack failed to report pack name\");\n     +\t\t\tgoto out;\n    - \t\t}\n    - \n    ++\t\t}\n    ++\n     +\t\tstrbuf_addf(&keep_msg, \"receive-pack %\"PRIuMAX\" on %s\",\n     +\t\t\t    (uintmax_t)getpid(), hostname);\n     +\n    @@ object-file.c: static int odb_transaction_files_write_pack(struct odb_transactio\n     +\n      \t\todb_source_prepare(transaction->quarantine,\n      \t\t\t\t   ODB_PREPARE_FLUSH_CACHES);\n    -+\n      \t}\n      \n     -\treturn 0;\n4:  c1a175e2b9 ! 4:  046ff4741e object-file: write \".keep\" lockfiles during transaction commit\n    @@ object-file.c: static int write_pack_lockfile(struct odb_source *source, const c\n      \t}\n      \n     +\t*out = register_tempfile(lock_path.buf);\n    - \tret = 0;\n    - \n    ++\n      out:\n    + \tstrbuf_release(&lock_path);\n    + \tstrbuf_release(&contents);\n     @@ object-file.c: static int write_pack_lockfile(struct odb_source *source, const char *pack_name,\n      \treturn ret;\n      }\n    @@ object-file.c: static int odb_transaction_files_write_pack(struct odb_transactio\n      \n      \t\todb_source_prepare(transaction->quarantine,\n      \t\t\t\t   ODB_PREPARE_FLUSH_CACHES);\n    --\n    - \t}\n    - \n    +@@ object-file.c: static int odb_transaction_files_write_pack(struct odb_transaction *base,\n      \tret = 0;\n      \n      out:\n\nbase-commit: f57c9a73884400380bddd172acc88b569834f909\n-- \n2.56.0.173.gf57c9a7388\n\n\n"},{"id":"555535","messageId":"20261020132900.1208343-2-jltobler@gmail.com","threadId":"66550","inReplyTo":"20261020132900.1208343-1-jltobler@gmail.com","subject":"[PATCH v3 1/4] pack-write: make index-pack output comment hash agnostic","fromName":"Justin Tobler","fromEmail":"jltobler@gmail.com","sentAt":"2026-10-20T13:28:57Z","receivedAt":"2026-10-20T13:28:57Z","isPatch":true,"sender":{"key":"jltobler@gmail.com","avatar":"https://avatars.githubusercontent.com/u/53454972?v=4"},"body":"The code comment in `index_pack_lockfile()` explains the expected format\nof output from git-index-pack(1). As written it explicitly mentions\nSHA1, but both the git-index-pack(1) output and the parsing are not\nspecific to SHA1 and also support SHA256.\n\nUpdate the comment explanation to be hash agnostic accordingly.\n\nSigned-off-by: Justin Tobler <jltobler@gmail.com>\n---\n pack-write.c | 14 +++++++++-----\n 1 file changed, 9 insertions(+), 5 deletions(-)\n\ndiff --git a/pack-write.c b/pack-write.c\nindex 85674e4b72..b1311c9923 100644\n--- a/pack-write.c\n+++ b/pack-write.c\n@@ -476,11 +476,15 @@ char *index_pack_lockfile(struct odb_source *source, int ip_out,\n \tconst int len = source->odb->repo->hash_algo->hexsz + 6;\n \n \t/*\n-\t * The first thing we expect from index-pack's output\n-\t * is \"pack\\t%40s\\n\" or \"keep\\t%40s\\n\" (46 bytes) where\n-\t * %40s is the newly created pack SHA1 name.  In the \"keep\"\n-\t * case, we need it to remove the corresponding .keep file\n-\t * later on.  If we don't get that then tough luck with it.\n+\t * The first thing we expect from index-pack's output is\n+\t * \"pack\\t<name>\\n\" or \"keep\\t<name>\\n\", where <name> is the hexadecimal\n+\t * name of the packfile it has just written. The line is therefore five\n+\t * bytes of prefix, `hexsz` bytes of name, and a trailing newline. A\n+\t * \"keep\" prefix tells us that index-pack has also created the\n+\t * corresponding \".keep\" file.\n+\t *\n+\t * In the \"keep\" case, we need it to remove the corresponding \".keep\"\n+\t * file later on. If we don't get that then tough luck with it.\n \t */\n \tif (read_in_full(ip_out, packname, len) == len && packname[len-1] == '\\n') {\n \t\tconst char *name;\n-- \n2.56.0.173.gf57c9a7388\n\n\n"},{"id":"555536","messageId":"20261020132900.1208343-3-jltobler@gmail.com","threadId":"66550","inReplyTo":"20261020132900.1208343-1-jltobler@gmail.com","subject":"[PATCH v3 2/4] pack-write: lift and expose index-pack report parsing","fromName":"Justin Tobler","fromEmail":"jltobler@gmail.com","sentAt":"2026-10-20T13:28:58Z","receivedAt":"2026-10-20T13:28:58Z","isPatch":true,"sender":{"key":"jltobler@gmail.com","avatar":"https://avatars.githubusercontent.com/u/53454972?v=4"},"body":"Once git-index-pack(1) finishes writing a packfile, it reports the\nhexadecimal name of that packfile on stdout prefixed with either \"pack\"\nor \"keep\" depending on whether a \".keep\" file was also created alongside\nit. Callers that are required to manage the lifecycle of \".keep\" files\nparse this report via `index_pack_lockfile()`, which provides the path\nto the temporary \".keep\" file.\n\nIn a subsequent commit, the \"files\" ODB transaction begins creating the\npack lockfiles during transaction commit instead of eagerly staging them\nin the transaction quarantine directory with the packfile. Extract the\ngeneric parsing logic for the git-index-pack(1) output into\n`read_index_pack_report()` in preparation for recording the names of\nnewly written packfiles.\n\nSigned-off-by: Justin Tobler <jltobler@gmail.com>\n---\n pack-write.c | 56 ++++++++++++++++++++++++++++++++++++++--------------\n pack.h       | 10 ++++++++++\n 2 files changed, 51 insertions(+), 15 deletions(-)\n\ndiff --git a/pack-write.c b/pack-write.c\nindex b1311c9923..2ed33448ba 100644\n--- a/pack-write.c\n+++ b/pack-write.c\n@@ -469,11 +469,16 @@ void fixup_pack_header_footer(const struct git_hash_algo *hash_algo,\n \tfsync_component_or_die(FSYNC_COMPONENT_PACK, pack_fd, pack_name);\n }\n \n-char *index_pack_lockfile(struct odb_source *source, int ip_out,\n-\t\t\t  int *is_well_formed)\n+int read_index_pack_report(struct repository *repo, int ip_out,\n+\t\t\t   struct strbuf *pack_name, int *is_keep)\n {\n+\tconst int len = repo->hash_algo->hexsz + 6;\n \tchar packname[GIT_MAX_HEXSZ + 6];\n-\tconst int len = source->odb->repo->hash_algo->hexsz + 6;\n+\tint is_keep_ignored;\n+\tconst char *name;\n+\n+\tif (!is_keep)\n+\t\tis_keep = &is_keep_ignored;\n \n \t/*\n \t * The first thing we expect from index-pack's output is\n@@ -482,24 +487,45 @@ char *index_pack_lockfile(struct odb_source *source, int ip_out,\n \t * bytes of prefix, `hexsz` bytes of name, and a trailing newline. A\n \t * \"keep\" prefix tells us that index-pack has also created the\n \t * corresponding \".keep\" file.\n-\t *\n-\t * In the \"keep\" case, we need it to remove the corresponding \".keep\"\n-\t * file later on. If we don't get that then tough luck with it.\n \t */\n-\tif (read_in_full(ip_out, packname, len) == len && packname[len-1] == '\\n') {\n-\t\tconst char *name;\n+\tif (read_in_full(ip_out, packname, len) != len || packname[len - 1] != '\\n')\n+\t\treturn -1;\n+\n+\tpackname[len - 1] = 0;\n+\tif (skip_prefix(packname, \"pack\\t\", &name))\n+\t\t*is_keep = 0;\n+\telse if (skip_prefix(packname, \"keep\\t\", &name))\n+\t\t*is_keep = 1;\n+\telse\n+\t\treturn -1;\n+\n+\tstrbuf_addstr(pack_name, name);\n+\n+\treturn 0;\n+}\n \n+char *index_pack_lockfile(struct odb_source *source, int ip_out,\n+\t\t\t  int *is_well_formed)\n+{\n+\tstruct strbuf pack_name = STRBUF_INIT;\n+\tchar *lockfile = NULL;\n+\tint is_keep = 0;\n+\n+\tif (read_index_pack_report(source->odb->repo, ip_out, &pack_name, &is_keep)) {\n \t\tif (is_well_formed)\n-\t\t\t*is_well_formed = 1;\n-\t\tpackname[len-1] = 0;\n-\t\tif (skip_prefix(packname, \"keep\\t\", &name))\n-\t\t\treturn xstrfmt(\"%s/pack/pack-%s.keep\",\n-\t\t\t\t       source->path, name);\n+\t\t\t*is_well_formed = 0;\n \t\treturn NULL;\n \t}\n+\n \tif (is_well_formed)\n-\t\t*is_well_formed = 0;\n-\treturn NULL;\n+\t\t*is_well_formed = 1;\n+\n+\t/* If there is no \".keep\" file, then there is no lockfile to return. */\n+\tif (is_keep)\n+\t\tlockfile = xstrfmt(\"%s/pack/pack-%s.keep\", source->path, pack_name.buf);\n+\n+\tstrbuf_release(&pack_name);\n+\treturn lockfile;\n }\n \n /*\ndiff --git a/pack.h b/pack.h\nindex ada506b5c5..2f2ce97c11 100644\n--- a/pack.h\n+++ b/pack.h\n@@ -106,6 +106,16 @@ off_t write_pack_header(struct hashfile *f, uint32_t);\n void fixup_pack_header_footer(const struct git_hash_algo *, int,\n \t\t\t      unsigned char *, const char *, uint32_t,\n \t\t\t      unsigned char *, off_t);\n+\n+/*\n+ * Read the report that git-index-pack(1) writes to its standard output after\n+ * the packfile is written. The hexadecimal name of the packfile is appended to\n+ * `pack_name` and, if `is_keep` is non-NULL, it is set to indicate whether a\n+ * \".keep\" file was written. Return 0 on success and a negative error code on\n+ * failure.\n+ */\n+int read_index_pack_report(struct repository *repo, int ip_out,\n+\t\t\t   struct strbuf *pack_name, int *is_keep);\n char *index_pack_lockfile(struct odb_source *source, int fd,\n \t\t\t  int *is_well_formed);\n \n-- \n2.56.0.173.gf57c9a7388\n\n\n"},{"id":"555537","messageId":"20261020132900.1208343-4-jltobler@gmail.com","threadId":"66550","inReplyTo":"20261020132900.1208343-1-jltobler@gmail.com","subject":"[PATCH v3 3/4] object-file: explicitly write \".keep\" lockfile","fromName":"Justin Tobler","fromEmail":"jltobler@gmail.com","sentAt":"2026-10-20T13:28:59Z","receivedAt":"2026-10-20T13:28:59Z","isPatch":true,"sender":{"key":"jltobler@gmail.com","avatar":"https://avatars.githubusercontent.com/u/53454972?v=4"},"body":"The `odb_transaction_write_pack()` interface for the \"files\" backend\nrelies on git-index-pack(1) to write the specified packfile into an ODB\ntransaction as a packfile. To prevent a concurrent housekeeping\noperation from deleting the newly written packfile prior to the\nreferences being updated, a \".keep\" lockfile is also generated by\ngit-index-pack(1) via the `--keep` option. Consequently, the \".keep\"\nlockfile is written into the ODB transaction alongside the packfile and\nlater migrated together to the primary ODB during transaction commit.\n\nIn a subsequent commit, writing the lockfile is deferred until\ntransaction commit and thus cannot rely on git-index-pack(1) to write\nit. In preparation for this, update `odb_transaction_files_write_pack()`\nto explicitly generate the same \".keep\" lockfile itself.\n\nSigned-off-by: Justin Tobler <jltobler@gmail.com>\n---\n object-file.c | 105 +++++++++++++++++++++++++++++++++++++++-----------\n 1 file changed, 83 insertions(+), 22 deletions(-)\n\ndiff --git a/object-file.c b/object-file.c\nindex 210984f825..ad55c01198 100644\n--- a/object-file.c\n+++ b/object-file.c\n@@ -1256,6 +1256,45 @@ int read_loose_object(struct repository *repo,\n \treturn ret;\n }\n \n+static int write_pack_lockfile(struct odb_source *source, const char *pack_name,\n+\t\t\t       const char *keep_msg)\n+{\n+\tstruct strbuf lock_path = STRBUF_INIT;\n+\tstruct strbuf contents = STRBUF_INIT;\n+\tint ret = 0;\n+\tint fd;\n+\n+\tstrbuf_addf(&lock_path, \"%s/pack/pack-%s.keep\",\n+\t\t    source->path, pack_name);\n+\tstrbuf_addf(&contents, \"%s\\n\", keep_msg);\n+\n+\tfd = safe_create_file_with_leading_directories(source->odb->repo,\n+\t\t\t\t\t\t       lock_path.buf);\n+\tif (fd < 0) {\n+\t\tret = error_errno(_(\"unable to create '%s'\"), lock_path.buf);\n+\t\tgoto out;\n+\t}\n+\n+\tif (write_in_full(fd, contents.buf, contents.len) < 0) {\n+\t\tret = error_errno(_(\"unable to write '%s'\"), lock_path.buf);\n+\t\tclose(fd);\n+\t\tunlink(lock_path.buf);\n+\t\tgoto out;\n+\t}\n+\n+\tif (close(fd) < 0) {\n+\t\tret = error_errno(_(\"unable to close '%s'\"), lock_path.buf);\n+\t\tunlink(lock_path.buf);\n+\t\tgoto out;\n+\t}\n+\n+out:\n+\tstrbuf_release(&lock_path);\n+\tstrbuf_release(&contents);\n+\n+\treturn ret;\n+}\n+\n static int odb_transaction_files_commit(struct odb_transaction *base)\n {\n \tstruct odb_transaction_files *transaction =\n@@ -1348,9 +1387,13 @@ static int odb_transaction_files_write_pack(struct odb_transaction *base,\n \t\tcontainer_of(base, struct odb_transaction_files, base);\n \tstruct repository *repo = base->source->odb->repo;\n \tstruct child_process child = CHILD_PROCESS_INIT;\n+\tstruct strbuf final_path = STRBUF_INIT;\n+\tstruct strbuf pack_name = STRBUF_INIT;\n+\tstruct strbuf keep_msg = STRBUF_INIT;\n \tstruct pack_header hdr;\n \tconst char *hdr_err;\n \tint err_fd = opts->err_fd;\n+\tint ret = -1;\n \tint status;\n \n \thdr_err = parse_pack_header(&hdr, pack_fd);\n@@ -1390,17 +1433,12 @@ static int odb_transaction_files_write_pack(struct odb_transaction *base,\n \t\t}\n \t} else {\n \t\tchar hostname[HOST_NAME_MAX + 1];\n-\t\tchar *lockfile;\n \n \t\tstrvec_pushl(&child.args, \"index-pack\", \"--stdin\", NULL);\n \t\tpush_header_arg(&child.args, &hdr);\n \n \t\tif (xgethostname(hostname, sizeof(hostname)))\n \t\t\txsnprintf(hostname, sizeof(hostname), \"localhost\");\n-\t\tstrvec_pushf(&child.args,\n-\t\t\t     \"--keep=receive-pack %\"PRIuMAX\" on %s\",\n-\t\t\t     (uintmax_t)getpid(),\n-\t\t\t     hostname);\n \n \t\tif (!opts->quiet && err_fd)\n \t\t\tstrvec_push(&child.args, \"--show-resolving-progress\");\n@@ -1424,34 +1462,57 @@ static int odb_transaction_files_write_pack(struct odb_transaction *base,\n \t\t\treturn -1;\n \t\t}\n \n-\t\t/*\n-\t\t * The lockfile filepath is expected to be the final location of\n-\t\t * the \".keep\" file after being migrated to the main ODB source.\n-\t\t * This ensures the lockfile can be found and removed later\n-\t\t * after the ODB transaction has been committed.\n-\t\t */\n-\t\tlockfile = index_pack_lockfile(base->source, child.out, NULL);\n-\t\tif (lockfile) {\n-\t\t\tALLOC_GROW(transaction->pack_lockfiles,\n-\t\t\t\t   transaction->pack_lockfiles_nr + 1,\n-\t\t\t\t   transaction->pack_lockfiles_alloc);\n-\t\t\ttransaction->pack_lockfiles[transaction->pack_lockfiles_nr++] =\n-\t\t\t\tregister_tempfile(lockfile);\n-\t\t\tfree(lockfile);\n-\t\t}\n+\t\tret = read_index_pack_report(repo, child.out, &pack_name, NULL);\n+\n \t\tclose(child.out);\n \n \t\tstatus = finish_command(&child);\n \t\tif (status) {\n \t\t\tstrbuf_addstr(err_msg, \"index-pack abnormal exit\");\n-\t\t\treturn -1;\n+\t\t\tgoto out;\n \t\t}\n \n+\t\tif (ret) {\n+\t\t\tstrbuf_addstr(err_msg, \"index-pack failed to report pack name\");\n+\t\t\tgoto out;\n+\t\t}\n+\n+\t\tstrbuf_addf(&keep_msg, \"receive-pack %\"PRIuMAX\" on %s\",\n+\t\t\t    (uintmax_t)getpid(), hostname);\n+\n+\t\tif (write_pack_lockfile(transaction->quarantine, pack_name.buf,\n+\t\t\t\t\tkeep_msg.buf)) {\n+\t\t\tstrbuf_addstr(err_msg, \"lockfile write failed\");\n+\t\t\tgoto out;\n+\t\t}\n+\n+\t\tALLOC_GROW(transaction->pack_lockfiles,\n+\t\t\t   transaction->pack_lockfiles_nr + 1,\n+\t\t\t   transaction->pack_lockfiles_alloc);\n+\n+\t\t/*\n+\t\t * The lockfile filepath is expected to be the final location of\n+\t\t * the \".keep\" file after being migrated to the main ODB source.\n+\t\t * This ensures the lockfile can be found and removed later\n+\t\t * after the ODB transaction has been committed.\n+\t\t */\n+\t\tstrbuf_addf(&final_path, \"%s/pack/pack-%s.keep\",\n+\t\t\t    base->source->path, pack_name.buf);\n+\t\ttransaction->pack_lockfiles[transaction->pack_lockfiles_nr++] =\n+\t\t\tregister_tempfile(final_path.buf);\n+\n \t\todb_source_prepare(transaction->quarantine,\n \t\t\t\t   ODB_PREPARE_FLUSH_CACHES);\n \t}\n \n-\treturn 0;\n+\tret = 0;\n+\n+out:\n+\tstrbuf_release(&final_path);\n+\tstrbuf_release(&pack_name);\n+\tstrbuf_release(&keep_msg);\n+\n+\treturn ret;\n }\n \n static int odb_transaction_files_finalize(struct odb_transaction *base)\n-- \n2.56.0.173.gf57c9a7388\n\n\n"},{"id":"555538","messageId":"20261020132900.1208343-5-jltobler@gmail.com","threadId":"66550","inReplyTo":"20261020132900.1208343-1-jltobler@gmail.com","subject":"[PATCH v3 4/4] object-file: write \".keep\" lockfiles during transaction commit","fromName":"Justin Tobler","fromEmail":"jltobler@gmail.com","sentAt":"2026-10-20T13:29:00Z","receivedAt":"2026-10-20T13:29:00Z","isPatch":true,"sender":{"key":"jltobler@gmail.com","avatar":"https://avatars.githubusercontent.com/u/53454972?v=4"},"body":"The \"files\" ODB backend writes a \".keep\" lockfile into the transaction\nquarantine directory via `odb_transaction_files_write_pack()`. When the\nODB transaction is committed, the \".keep\" lockfile is migrated to the\nprimary ODB alongside its packfile. Writing the temporary lockfile in\nthis manner has a couple of downsides:\n\n  - Migrating quarantined objects via `tmp_objdir_migrate()` doesn't\n    immediately stop if a \".keep\" lockfile already exists for the same\n    packfile in the primary ODB and instead allows all the remaining\n    objects to be migrated. So even though `odb_transaction_commit()`\n    ultimately returns an error here, the objects are needlessly written\n    to the primary ODB anyways.\n\n  - Even more concerning, the \".keep\" tempfile atexit(3) handler tries\n    to remove any lockfile of the same name regardless of whether it was\n    created by the same process. If the \".keep\" lockfile of the same\n    name for a concurrent process gets deleted prematurely, its packfile\n    becomes at risk of being deleted by concurrent housekeeping.\n\nStop creating/registering the temporary lockfiles when writing the\npackfile to the transaction and instead do so when committing the\ntransaction. This ensures the lockfile is properly acquired prior to\nmigrating objects and that only the owning process can delete its own\nlockfiles.\n\nReported-by: Qin ShiCheng <qeesung@live.com>\nSigned-off-by: Justin Tobler <jltobler@gmail.com>\n---\n object-file.c              | 77 ++++++++++++++++++++++----------------\n t/t5547-push-quarantine.sh | 30 +++++++++++++--\n 2 files changed, 71 insertions(+), 36 deletions(-)\n\ndiff --git a/object-file.c b/object-file.c\nindex ad55c01198..383aac8e3f 100644\n--- a/object-file.c\n+++ b/object-file.c\n@@ -492,6 +492,7 @@ struct odb_transaction_files {\n \tstruct transaction_packfile packfile;\n \tconst char *prefix;\n \n+\tstruct strvec pack_names;\n \tstruct tempfile **pack_lockfiles;\n \tsize_t pack_lockfiles_nr;\n \tsize_t pack_lockfiles_alloc;\n@@ -1257,7 +1258,7 @@ int read_loose_object(struct repository *repo,\n }\n \n static int write_pack_lockfile(struct odb_source *source, const char *pack_name,\n-\t\t\t       const char *keep_msg)\n+\t\t\t       const char *keep_msg, struct tempfile **out)\n {\n \tstruct strbuf lock_path = STRBUF_INIT;\n \tstruct strbuf contents = STRBUF_INIT;\n@@ -1288,6 +1289,8 @@ static int write_pack_lockfile(struct odb_source *source, const char *pack_name,\n \t\tgoto out;\n \t}\n \n+\t*out = register_tempfile(lock_path.buf);\n+\n out:\n \tstrbuf_release(&lock_path);\n \tstrbuf_release(&contents);\n@@ -1295,6 +1298,40 @@ static int write_pack_lockfile(struct odb_source *source, const char *pack_name,\n \treturn ret;\n }\n \n+static int write_pack_lockfiles(struct odb_transaction_files *transaction)\n+{\n+\tstruct strbuf keep_msg = STRBUF_INIT;\n+\tchar hostname[HOST_NAME_MAX + 1];\n+\tint ret = 0;\n+\n+\tif (!transaction->pack_names.nr)\n+\t\treturn 0;\n+\n+\tif (xgethostname(hostname, sizeof(hostname)))\n+\t\txsnprintf(hostname, sizeof(hostname), \"localhost\");\n+\tstrbuf_addf(&keep_msg, \"receive-pack %\"PRIuMAX\" on %s\",\n+\t\t    (uintmax_t)getpid(), hostname);\n+\n+\tfor (size_t i = 0; i < transaction->pack_names.nr; i++) {\n+\t\tstruct tempfile *lock;\n+\n+\t\tret = write_pack_lockfile(transaction->base.source,\n+\t\t\t\t\t  transaction->pack_names.v[i],\n+\t\t\t\t\t  keep_msg.buf, &lock);\n+\t\tif (ret < 0)\n+\t\t\tbreak;\n+\n+\t\tALLOC_GROW(transaction->pack_lockfiles,\n+\t\t\t   transaction->pack_lockfiles_nr + 1,\n+\t\t\t   transaction->pack_lockfiles_alloc);\n+\t\ttransaction->pack_lockfiles[transaction->pack_lockfiles_nr++] = lock;\n+\t}\n+\n+\tstrbuf_release(&keep_msg);\n+\n+\treturn ret;\n+}\n+\n static int odb_transaction_files_commit(struct odb_transaction *base)\n {\n \tstruct odb_transaction_files *transaction =\n@@ -1323,6 +1360,9 @@ static int odb_transaction_files_commit(struct odb_transaction *base)\n \t\tdelete_tempfile(&temp);\n \t\tstrbuf_release(&temp_path);\n \n+\t\tif (write_pack_lockfiles(transaction))\n+\t\t\treturn error(_(\"unable to lock packfiles\"));\n+\n \t\t/*\n \t\t * Make the object files visible in the primary ODB after their data is\n \t\t * fully durable.\n@@ -1387,9 +1427,7 @@ static int odb_transaction_files_write_pack(struct odb_transaction *base,\n \t\tcontainer_of(base, struct odb_transaction_files, base);\n \tstruct repository *repo = base->source->odb->repo;\n \tstruct child_process child = CHILD_PROCESS_INIT;\n-\tstruct strbuf final_path = STRBUF_INIT;\n \tstruct strbuf pack_name = STRBUF_INIT;\n-\tstruct strbuf keep_msg = STRBUF_INIT;\n \tstruct pack_header hdr;\n \tconst char *hdr_err;\n \tint err_fd = opts->err_fd;\n@@ -1432,14 +1470,9 @@ static int odb_transaction_files_write_pack(struct odb_transaction *base,\n \t\t\treturn -1;\n \t\t}\n \t} else {\n-\t\tchar hostname[HOST_NAME_MAX + 1];\n-\n \t\tstrvec_pushl(&child.args, \"index-pack\", \"--stdin\", NULL);\n \t\tpush_header_arg(&child.args, &hdr);\n \n-\t\tif (xgethostname(hostname, sizeof(hostname)))\n-\t\t\txsnprintf(hostname, sizeof(hostname), \"localhost\");\n-\n \t\tif (!opts->quiet && err_fd)\n \t\t\tstrvec_push(&child.args, \"--show-resolving-progress\");\n \t\tif (err_fd)\n@@ -1477,29 +1510,7 @@ static int odb_transaction_files_write_pack(struct odb_transaction *base,\n \t\t\tgoto out;\n \t\t}\n \n-\t\tstrbuf_addf(&keep_msg, \"receive-pack %\"PRIuMAX\" on %s\",\n-\t\t\t    (uintmax_t)getpid(), hostname);\n-\n-\t\tif (write_pack_lockfile(transaction->quarantine, pack_name.buf,\n-\t\t\t\t\tkeep_msg.buf)) {\n-\t\t\tstrbuf_addstr(err_msg, \"lockfile write failed\");\n-\t\t\tgoto out;\n-\t\t}\n-\n-\t\tALLOC_GROW(transaction->pack_lockfiles,\n-\t\t\t   transaction->pack_lockfiles_nr + 1,\n-\t\t\t   transaction->pack_lockfiles_alloc);\n-\n-\t\t/*\n-\t\t * The lockfile filepath is expected to be the final location of\n-\t\t * the \".keep\" file after being migrated to the main ODB source.\n-\t\t * This ensures the lockfile can be found and removed later\n-\t\t * after the ODB transaction has been committed.\n-\t\t */\n-\t\tstrbuf_addf(&final_path, \"%s/pack/pack-%s.keep\",\n-\t\t\t    base->source->path, pack_name.buf);\n-\t\ttransaction->pack_lockfiles[transaction->pack_lockfiles_nr++] =\n-\t\t\tregister_tempfile(final_path.buf);\n+\t\tstrvec_push(&transaction->pack_names, pack_name.buf);\n \n \t\todb_source_prepare(transaction->quarantine,\n \t\t\t\t   ODB_PREPARE_FLUSH_CACHES);\n@@ -1508,9 +1519,7 @@ static int odb_transaction_files_write_pack(struct odb_transaction *base,\n \tret = 0;\n \n out:\n-\tstrbuf_release(&final_path);\n \tstrbuf_release(&pack_name);\n-\tstrbuf_release(&keep_msg);\n \n \treturn ret;\n }\n@@ -1525,6 +1534,7 @@ static int odb_transaction_files_finalize(struct odb_transaction *base)\n \t\tret |= delete_tempfile(&transaction->pack_lockfiles[i]);\n \n \tfree(transaction->pack_lockfiles);\n+\tstrvec_clear(&transaction->pack_names);\n \n \treturn ret;\n }\n@@ -1557,6 +1567,7 @@ int odb_transaction_files_begin(struct odb_source *source,\n \ttransaction->base.write_pack = odb_transaction_files_write_pack;\n \ttransaction->base.env = odb_transaction_files_env;\n \ttransaction->flags = flags;\n+\tstrvec_init(&transaction->pack_names);\n \n \ttransaction->prefix = \"bulk-fsync\";\n \tif (flags & ODB_TRANSACTION_RECEIVE) {\ndiff --git a/t/t5547-push-quarantine.sh b/t/t5547-push-quarantine.sh\nindex 1b7097179e..6c14dfc42b 100755\n--- a/t/t5547-push-quarantine.sh\n+++ b/t/t5547-push-quarantine.sh\n@@ -77,10 +77,9 @@ test_expect_success '.keep file is removed after push' '\n \tgit -C keep.git config set receive.unpackLimit 0 &&\n \n \t# While incoming objects are still quarantined, validate that the\n-\t# \".keep\" lockfile is present in the quarantine directory.\n+\t# \".keep\" lockfile is not present in the quarantine directory.\n \ttest_hook -C keep.git pre-receive <<-\\EOF &&\n-\tkeep=\"$(ls \"$GIT_QUARANTINE_PATH\"/pack/pack-*.keep)\" &&\n-\ttest -f \"$keep\"\n+\ttest ! -e \"$GIT_QUARANTINE_PATH\"/pack/pack-*.keep\n \tEOF\n \n \t# After quarantined objects are migrated, validate that the \".keep\"\n@@ -101,4 +100,29 @@ test_expect_success '.keep file is removed after push' '\n \ttest_path_is_missing \"$keep\"\n '\n \n+test_expect_success '.keep file created outside of process is not removed' '\n+\ttest_when_finished rm -rf keep.git &&\n+\tgit init --bare keep.git &&\n+\n+\tgit -C keep.git config set receive.unpackLimit 0 &&\n+\n+\t# Create a \".keep\" lockfile matching the packfile to simulate a\n+\t# concurrent git-receive-pack(1) that has already written its lockfile.\n+\ttest_hook -C keep.git pre-receive <<-\\EOF &&\n+\tpack=\"$(ls \"$GIT_QUARANTINE_PATH\"/pack/pack-*.pack)\" &&\n+\tpack=\"${pack##*/}\" &&\n+\techo \"baz\" >\"objects/pack/${pack%.pack}.keep\"\n+\tEOF\n+\n+\ttest_commit bar &&\n+\ttest_must_fail git push keep.git HEAD &&\n+\n+\t# The \".keep\" lockfile of the concurrent git-receive-pack(1) should be\n+\t# left in its place.\n+\tkeep=\"$(ls keep.git/objects/pack/pack-*.keep)\" &&\n+\ttest_path_is_file \"$keep\" &&\n+\ttest_grep \"baz\" \"$keep\" &&\n+\ttest_path_is_missing \"${keep%.keep}.pack\"\n+'\n+\n test_done\n-- \n2.56.0.173.gf57c9a7388\n\n\n"},{"id":"555549","messageId":"atd3ioVos5paeT6b@pks.im","threadId":"66550","inReplyTo":"20261020132900.1208343-1-jltobler@gmail.com","subject":"Re: [PATCH v3 0/4] object-file: fix \".keep\" lockfile ownership in ODB transactions","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-10-20T14:15:38Z","receivedAt":"2026-10-20T14:15:38Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"On Tue, Oct 20, 2026 at 08:28:56AM -0500, Justin Tobler wrote:\n> Changes since V2:\n>   - In `write_pack_lockfile()` the error is now explicitly assigned to\n>     `ret`.\n>   - Removed an added newline that was not needed.\n\nThe changes in `write_pack_lockfile()` aren't quite in line with what I\nsuggested, but I'm not sure whteher it'd really need a reroll just to\nfix that. Everything else looks good to me, thanks!\n\nPatrick\n\n"},{"id":"555551","messageId":"atd6IjqH8F_O44gP@pks.im","threadId":"66550","inReplyTo":"20261020132900.1208343-4-jltobler@gmail.com","subject":"Re: [PATCH v3 3/4] object-file: explicitly write \".keep\" lockfile","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-10-20T14:26:42Z","receivedAt":"2026-10-20T14:26:42Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"On Tue, Oct 20, 2026 at 08:28:59AM -0500, Justin Tobler wrote:\n> diff --git a/object-file.c b/object-file.c\n> index 210984f825..ad55c01198 100644\n> --- a/object-file.c\n> +++ b/object-file.c\n> @@ -1256,6 +1256,45 @@ int read_loose_object(struct repository *repo,\n>  \treturn ret;\n>  }\n>  \n> +static int write_pack_lockfile(struct odb_source *source, const char *pack_name,\n> +\t\t\t       const char *keep_msg)\n> +{\n> +\tstruct strbuf lock_path = STRBUF_INIT;\n> +\tstruct strbuf contents = STRBUF_INIT;\n> +\tint ret = 0;\n> +\tint fd;\n\nHum, not quite what I meant :P I'd leave the variable explicitly\nuninitialized and then set it on all exit paths. Like that, the compiler\n(or at least most common compilers) would warn for all code paths where\nwe don't set the error code. So:\n\n        int ret;\n\n> +\tstrbuf_addf(&lock_path, \"%s/pack/pack-%s.keep\",\n> +\t\t    source->path, pack_name);\n> +\tstrbuf_addf(&contents, \"%s\\n\", keep_msg);\n> +\n> +\tfd = safe_create_file_with_leading_directories(source->odb->repo,\n> +\t\t\t\t\t\t       lock_path.buf);\n> +\tif (fd < 0) {\n> +\t\tret = error_errno(_(\"unable to create '%s'\"), lock_path.buf);\n> +\t\tgoto out;\n> +\t}\n> +\n> +\tif (write_in_full(fd, contents.buf, contents.len) < 0) {\n> +\t\tret = error_errno(_(\"unable to write '%s'\"), lock_path.buf);\n> +\t\tclose(fd);\n> +\t\tunlink(lock_path.buf);\n> +\t\tgoto out;\n> +\t}\n> +\n> +\tif (close(fd) < 0) {\n> +\t\tret = error_errno(_(\"unable to close '%s'\"), lock_path.buf);\n> +\t\tunlink(lock_path.buf);\n> +\t\tgoto out;\n> +\t}\n> +\n\nand then:\n\n        ret = 0;\n\n> +out:\n> +\tstrbuf_release(&lock_path);\n> +\tstrbuf_release(&contents);\n> +\n> +\treturn ret;\n> +}\n> +\n>  static int odb_transaction_files_commit(struct odb_transaction *base)\n>  {\n>  \tstruct odb_transaction_files *transaction =\n\nThanks!\n\nPatrick\n\n"},{"id":"555569","messageId":"ateNY8W17UAVPZ9b@denethor","threadId":"66550","inReplyTo":"atd6IjqH8F_O44gP@pks.im","subject":"Re: [PATCH v3 3/4] object-file: explicitly write \".keep\" lockfile","fromName":"Justin Tobler","fromEmail":"jltobler@gmail.com","sentAt":"2026-10-20T15:51:11Z","receivedAt":"2026-10-20T15:51:11Z","isPatch":true,"sender":{"key":"jltobler@gmail.com","avatar":"https://avatars.githubusercontent.com/u/53454972?v=4"},"body":"On 26/10/20 04:26PM, Patrick Steinhardt wrote:\n> On Tue, Oct 20, 2026 at 08:28:59AM -0500, Justin Tobler wrote:\n> > +static int write_pack_lockfile(struct odb_source *source, const char *pack_name,\n> > +\t\t\t       const char *keep_msg)\n> > +{\n> > +\tstruct strbuf lock_path = STRBUF_INIT;\n> > +\tstruct strbuf contents = STRBUF_INIT;\n> > +\tint ret = 0;\n> > +\tint fd;\n> \n> Hum, not quite what I meant :P I'd leave the variable explicitly\n> uninitialized and then set it on all exit paths. Like that, the compiler\n> (or at least most common compilers) would warn for all code paths where\n> we don't set the error code.\n\nApologies, I should have read your suggestion more closely. I don't mind\nsending another version correcting this though.\n\n-Justin\n\n"},{"id":"555754","messageId":"xmqqa4o7q9ui.fsf@gitster.g","threadId":"66550","inReplyTo":"ateNY8W17UAVPZ9b@denethor","subject":"Re: [PATCH v3 3/4] object-file: explicitly write \".keep\" lockfile","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-10-21T16:47:33Z","receivedAt":"2026-10-21T16:47:33Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Justin Tobler <jltobler@gmail.com> writes:\n\n> On 26/10/20 04:26PM, Patrick Steinhardt wrote:\n>> On Tue, Oct 20, 2026 at 08:28:59AM -0500, Justin Tobler wrote:\n>> > +static int write_pack_lockfile(struct odb_source *source, const char *pack_name,\n>> > +\t\t\t       const char *keep_msg)\n>> > +{\n>> > +\tstruct strbuf lock_path = STRBUF_INIT;\n>> > +\tstruct strbuf contents = STRBUF_INIT;\n>> > +\tint ret = 0;\n>> > +\tint fd;\n>> \n>> Hum, not quite what I meant :P I'd leave the variable explicitly\n>> uninitialized and then set it on all exit paths. Like that, the compiler\n>> (or at least most common compilers) would warn for all code paths where\n>> we don't set the error code.\n>\n> Apologies, I should have read your suggestion more closely. I don't mind\n> sending another version correcting this though.\n\nThen let me mark the topic as \"Expecting a reroll\".\n\nThanks.\n\n"},{"id":"555772","messageId":"20261021182528.3055494-1-jltobler@gmail.com","threadId":"66550","inReplyTo":"20261020132900.1208343-1-jltobler@gmail.com","subject":"[PATCH v4 0/4] object-file: fix \".keep\" lockfile ownership in ODB transactions","fromName":"Justin Tobler","fromEmail":"jltobler@gmail.com","sentAt":"2026-10-21T18:25:24Z","receivedAt":"2026-10-21T18:25:24Z","isPatch":true,"sender":{"key":"jltobler@gmail.com","avatar":"https://avatars.githubusercontent.com/u/53454972?v=4"},"body":"Greetings,\n\nThis series addresses a bug report[1] regarding a git-receive-pack(1)\nprocess deleting \".keep\" lockfiles of with the same name but from\nanother concurrent git-receive-pack(1) process receiving an identical\npackfile. Deleting the \".keep\" lockfile for another process leaves its\nnewly written packfile vulnerable to deletion by another concurrent\nhousekeeping process.\n\nThe root of the problem here is that the temporary \".keep\" lockfiles are\ngenerated and registered in the object quarantine prior to being written\nin the primary ODB. This means that even if the ODB transaction never\ncommits and exits early, the tempfile atexit(3) handler still runs and\ntries to delete the lockfile at its expected final location. This\nblindly deletes any file matching regardless of whether is was created\nby the process.\n\nThe first three patches are some preparatory changes so that\n`odb_transaction_files_write_pack()` is able to start explicitly\ncreating the lockfiles without relying on git-index-pack(1). Patch four\nmigrates lockfile generation to happen during ODB transaction commit to\nensure the lockfile is actually able to be written before registering it\nas a tempfile.\n\nChange since V3:\n  - Followed Patrick's suggestion correctly this time.\n\nChanges since V2:\n  - In `write_pack_lockfile()` the error is now explicitly assigned to\n    `ret`.\n  - Removed an added newline that was not needed.\n\nChanges since V1:\n  - Split out comment change into separate commit as suggested.\n  - Improved error handling and logic flow in a couple of functions.\n  - Renamed `write_pack_lock()` to `write_pack_lockfile()`.\n  - Added better error messages and errno reporting to\n    `write_pack_lockfile()` helper.\n  - Adapted tests case to assert that packfile is not migrated.\n  - Clarified some commit messages and fixed some typos.\n\nThanks,\n-Justin\n\n[1]: <932e8e425aecfbd33c1e5caf66c80a0226abacba.1789385483.git.gitgitgadget@gmail.com>\n\nJustin Tobler (4):\n  pack-write: make index-pack output comment hash agnostic\n  pack-write: lift and expose index-pack report parsing\n  object-file: explicitly write \".keep\" lockfile\n  object-file: write \".keep\" lockfiles during transaction commit\n\n object-file.c              | 127 +++++++++++++++++++++++++++++--------\n pack-write.c               |  64 ++++++++++++++-----\n pack.h                     |  10 +++\n t/t5547-push-quarantine.sh |  30 ++++++++-\n 4 files changed, 184 insertions(+), 47 deletions(-)\n\nRange-diff against v3:\n1:  b9197cbf3b = 1:  b9197cbf3b pack-write: make index-pack output comment hash agnostic\n2:  e27679ac16 = 2:  e27679ac16 pack-write: lift and expose index-pack report parsing\n3:  e7f72aa53d ! 3:  b8ccd78e0f object-file: explicitly write \".keep\" lockfile\n    @@ object-file.c: int read_loose_object(struct repository *repo,\n     +{\n     +\tstruct strbuf lock_path = STRBUF_INIT;\n     +\tstruct strbuf contents = STRBUF_INIT;\n    -+\tint ret = 0;\n    ++\tint ret;\n     +\tint fd;\n     +\n     +\tstrbuf_addf(&lock_path, \"%s/pack/pack-%s.keep\",\n    @@ object-file.c: int read_loose_object(struct repository *repo,\n     +\t\tgoto out;\n     +\t}\n     +\n    ++\tret = 0;\n    ++\n     +out:\n     +\tstrbuf_release(&lock_path);\n     +\tstrbuf_release(&contents);\n    @@ object-file.c: static int odb_transaction_files_write_pack(struct odb_transactio\n      \t\t\tstrbuf_addstr(err_msg, \"index-pack abnormal exit\");\n     -\t\t\treturn -1;\n     +\t\t\tgoto out;\n    - \t\t}\n    - \n    ++\t\t}\n    ++\n     +\t\tif (ret) {\n     +\t\t\tstrbuf_addstr(err_msg, \"index-pack failed to report pack name\");\n     +\t\t\tgoto out;\n    -+\t\t}\n    -+\n    + \t\t}\n    + \n     +\t\tstrbuf_addf(&keep_msg, \"receive-pack %\"PRIuMAX\" on %s\",\n     +\t\t\t    (uintmax_t)getpid(), hostname);\n     +\n4:  046ff4741e ! 4:  5fea4b5a36 object-file: write \".keep\" lockfiles during transaction commit\n    @@ object-file.c: static int write_pack_lockfile(struct odb_source *source, const c\n      \t}\n      \n     +\t*out = register_tempfile(lock_path.buf);\n    -+\n    + \tret = 0;\n    + \n      out:\n    - \tstrbuf_release(&lock_path);\n    - \tstrbuf_release(&contents);\n     @@ object-file.c: static int write_pack_lockfile(struct odb_source *source, const char *pack_name,\n      \treturn ret;\n      }\n\nbase-commit: f57c9a73884400380bddd172acc88b569834f909\n-- \n2.56.0.173.gf57c9a7388\n\n\n"},{"id":"555773","messageId":"20261021182528.3055494-2-jltobler@gmail.com","threadId":"66550","inReplyTo":"20261021182528.3055494-1-jltobler@gmail.com","subject":"[PATCH v4 1/4] pack-write: make index-pack output comment hash agnostic","fromName":"Justin Tobler","fromEmail":"jltobler@gmail.com","sentAt":"2026-10-21T18:25:25Z","receivedAt":"2026-10-21T18:25:25Z","isPatch":true,"sender":{"key":"jltobler@gmail.com","avatar":"https://avatars.githubusercontent.com/u/53454972?v=4"},"body":"The code comment in `index_pack_lockfile()` explains the expected format\nof output from git-index-pack(1). As written it explicitly mentions\nSHA1, but both the git-index-pack(1) output and the parsing are not\nspecific to SHA1 and also support SHA256.\n\nUpdate the comment explanation to be hash agnostic accordingly.\n\nSigned-off-by: Justin Tobler <jltobler@gmail.com>\n---\n pack-write.c | 14 +++++++++-----\n 1 file changed, 9 insertions(+), 5 deletions(-)\n\ndiff --git a/pack-write.c b/pack-write.c\nindex 85674e4b72..b1311c9923 100644\n--- a/pack-write.c\n+++ b/pack-write.c\n@@ -476,11 +476,15 @@ char *index_pack_lockfile(struct odb_source *source, int ip_out,\n \tconst int len = source->odb->repo->hash_algo->hexsz + 6;\n \n \t/*\n-\t * The first thing we expect from index-pack's output\n-\t * is \"pack\\t%40s\\n\" or \"keep\\t%40s\\n\" (46 bytes) where\n-\t * %40s is the newly created pack SHA1 name.  In the \"keep\"\n-\t * case, we need it to remove the corresponding .keep file\n-\t * later on.  If we don't get that then tough luck with it.\n+\t * The first thing we expect from index-pack's output is\n+\t * \"pack\\t<name>\\n\" or \"keep\\t<name>\\n\", where <name> is the hexadecimal\n+\t * name of the packfile it has just written. The line is therefore five\n+\t * bytes of prefix, `hexsz` bytes of name, and a trailing newline. A\n+\t * \"keep\" prefix tells us that index-pack has also created the\n+\t * corresponding \".keep\" file.\n+\t *\n+\t * In the \"keep\" case, we need it to remove the corresponding \".keep\"\n+\t * file later on. If we don't get that then tough luck with it.\n \t */\n \tif (read_in_full(ip_out, packname, len) == len && packname[len-1] == '\\n') {\n \t\tconst char *name;\n-- \n2.56.0.173.gf57c9a7388\n\n\n"},{"id":"555774","messageId":"20261021182528.3055494-3-jltobler@gmail.com","threadId":"66550","inReplyTo":"20261021182528.3055494-1-jltobler@gmail.com","subject":"[PATCH v4 2/4] pack-write: lift and expose index-pack report parsing","fromName":"Justin Tobler","fromEmail":"jltobler@gmail.com","sentAt":"2026-10-21T18:25:26Z","receivedAt":"2026-10-21T18:25:26Z","isPatch":true,"sender":{"key":"jltobler@gmail.com","avatar":"https://avatars.githubusercontent.com/u/53454972?v=4"},"body":"Once git-index-pack(1) finishes writing a packfile, it reports the\nhexadecimal name of that packfile on stdout prefixed with either \"pack\"\nor \"keep\" depending on whether a \".keep\" file was also created alongside\nit. Callers that are required to manage the lifecycle of \".keep\" files\nparse this report via `index_pack_lockfile()`, which provides the path\nto the temporary \".keep\" file.\n\nIn a subsequent commit, the \"files\" ODB transaction begins creating the\npack lockfiles during transaction commit instead of eagerly staging them\nin the transaction quarantine directory with the packfile. Extract the\ngeneric parsing logic for the git-index-pack(1) output into\n`read_index_pack_report()` in preparation for recording the names of\nnewly written packfiles.\n\nSigned-off-by: Justin Tobler <jltobler@gmail.com>\n---\n pack-write.c | 56 ++++++++++++++++++++++++++++++++++++++--------------\n pack.h       | 10 ++++++++++\n 2 files changed, 51 insertions(+), 15 deletions(-)\n\ndiff --git a/pack-write.c b/pack-write.c\nindex b1311c9923..2ed33448ba 100644\n--- a/pack-write.c\n+++ b/pack-write.c\n@@ -469,11 +469,16 @@ void fixup_pack_header_footer(const struct git_hash_algo *hash_algo,\n \tfsync_component_or_die(FSYNC_COMPONENT_PACK, pack_fd, pack_name);\n }\n \n-char *index_pack_lockfile(struct odb_source *source, int ip_out,\n-\t\t\t  int *is_well_formed)\n+int read_index_pack_report(struct repository *repo, int ip_out,\n+\t\t\t   struct strbuf *pack_name, int *is_keep)\n {\n+\tconst int len = repo->hash_algo->hexsz + 6;\n \tchar packname[GIT_MAX_HEXSZ + 6];\n-\tconst int len = source->odb->repo->hash_algo->hexsz + 6;\n+\tint is_keep_ignored;\n+\tconst char *name;\n+\n+\tif (!is_keep)\n+\t\tis_keep = &is_keep_ignored;\n \n \t/*\n \t * The first thing we expect from index-pack's output is\n@@ -482,24 +487,45 @@ char *index_pack_lockfile(struct odb_source *source, int ip_out,\n \t * bytes of prefix, `hexsz` bytes of name, and a trailing newline. A\n \t * \"keep\" prefix tells us that index-pack has also created the\n \t * corresponding \".keep\" file.\n-\t *\n-\t * In the \"keep\" case, we need it to remove the corresponding \".keep\"\n-\t * file later on. If we don't get that then tough luck with it.\n \t */\n-\tif (read_in_full(ip_out, packname, len) == len && packname[len-1] == '\\n') {\n-\t\tconst char *name;\n+\tif (read_in_full(ip_out, packname, len) != len || packname[len - 1] != '\\n')\n+\t\treturn -1;\n+\n+\tpackname[len - 1] = 0;\n+\tif (skip_prefix(packname, \"pack\\t\", &name))\n+\t\t*is_keep = 0;\n+\telse if (skip_prefix(packname, \"keep\\t\", &name))\n+\t\t*is_keep = 1;\n+\telse\n+\t\treturn -1;\n+\n+\tstrbuf_addstr(pack_name, name);\n+\n+\treturn 0;\n+}\n \n+char *index_pack_lockfile(struct odb_source *source, int ip_out,\n+\t\t\t  int *is_well_formed)\n+{\n+\tstruct strbuf pack_name = STRBUF_INIT;\n+\tchar *lockfile = NULL;\n+\tint is_keep = 0;\n+\n+\tif (read_index_pack_report(source->odb->repo, ip_out, &pack_name, &is_keep)) {\n \t\tif (is_well_formed)\n-\t\t\t*is_well_formed = 1;\n-\t\tpackname[len-1] = 0;\n-\t\tif (skip_prefix(packname, \"keep\\t\", &name))\n-\t\t\treturn xstrfmt(\"%s/pack/pack-%s.keep\",\n-\t\t\t\t       source->path, name);\n+\t\t\t*is_well_formed = 0;\n \t\treturn NULL;\n \t}\n+\n \tif (is_well_formed)\n-\t\t*is_well_formed = 0;\n-\treturn NULL;\n+\t\t*is_well_formed = 1;\n+\n+\t/* If there is no \".keep\" file, then there is no lockfile to return. */\n+\tif (is_keep)\n+\t\tlockfile = xstrfmt(\"%s/pack/pack-%s.keep\", source->path, pack_name.buf);\n+\n+\tstrbuf_release(&pack_name);\n+\treturn lockfile;\n }\n \n /*\ndiff --git a/pack.h b/pack.h\nindex ada506b5c5..2f2ce97c11 100644\n--- a/pack.h\n+++ b/pack.h\n@@ -106,6 +106,16 @@ off_t write_pack_header(struct hashfile *f, uint32_t);\n void fixup_pack_header_footer(const struct git_hash_algo *, int,\n \t\t\t      unsigned char *, const char *, uint32_t,\n \t\t\t      unsigned char *, off_t);\n+\n+/*\n+ * Read the report that git-index-pack(1) writes to its standard output after\n+ * the packfile is written. The hexadecimal name of the packfile is appended to\n+ * `pack_name` and, if `is_keep` is non-NULL, it is set to indicate whether a\n+ * \".keep\" file was written. Return 0 on success and a negative error code on\n+ * failure.\n+ */\n+int read_index_pack_report(struct repository *repo, int ip_out,\n+\t\t\t   struct strbuf *pack_name, int *is_keep);\n char *index_pack_lockfile(struct odb_source *source, int fd,\n \t\t\t  int *is_well_formed);\n \n-- \n2.56.0.173.gf57c9a7388\n\n\n"},{"id":"555775","messageId":"20261021182528.3055494-4-jltobler@gmail.com","threadId":"66550","inReplyTo":"20261021182528.3055494-1-jltobler@gmail.com","subject":"[PATCH v4 3/4] object-file: explicitly write \".keep\" lockfile","fromName":"Justin Tobler","fromEmail":"jltobler@gmail.com","sentAt":"2026-10-21T18:25:27Z","receivedAt":"2026-10-21T18:25:27Z","isPatch":true,"sender":{"key":"jltobler@gmail.com","avatar":"https://avatars.githubusercontent.com/u/53454972?v=4"},"body":"The `odb_transaction_write_pack()` interface for the \"files\" backend\nrelies on git-index-pack(1) to write the specified packfile into an ODB\ntransaction as a packfile. To prevent a concurrent housekeeping\noperation from deleting the newly written packfile prior to the\nreferences being updated, a \".keep\" lockfile is also generated by\ngit-index-pack(1) via the `--keep` option. Consequently, the \".keep\"\nlockfile is written into the ODB transaction alongside the packfile and\nlater migrated together to the primary ODB during transaction commit.\n\nIn a subsequent commit, writing the lockfile is deferred until\ntransaction commit and thus cannot rely on git-index-pack(1) to write\nit. In preparation for this, update `odb_transaction_files_write_pack()`\nto explicitly generate the same \".keep\" lockfile itself.\n\nSigned-off-by: Justin Tobler <jltobler@gmail.com>\n---\n object-file.c | 107 +++++++++++++++++++++++++++++++++++++++-----------\n 1 file changed, 85 insertions(+), 22 deletions(-)\n\ndiff --git a/object-file.c b/object-file.c\nindex 210984f825..74a31760bc 100644\n--- a/object-file.c\n+++ b/object-file.c\n@@ -1256,6 +1256,47 @@ int read_loose_object(struct repository *repo,\n \treturn ret;\n }\n \n+static int write_pack_lockfile(struct odb_source *source, const char *pack_name,\n+\t\t\t       const char *keep_msg)\n+{\n+\tstruct strbuf lock_path = STRBUF_INIT;\n+\tstruct strbuf contents = STRBUF_INIT;\n+\tint ret;\n+\tint fd;\n+\n+\tstrbuf_addf(&lock_path, \"%s/pack/pack-%s.keep\",\n+\t\t    source->path, pack_name);\n+\tstrbuf_addf(&contents, \"%s\\n\", keep_msg);\n+\n+\tfd = safe_create_file_with_leading_directories(source->odb->repo,\n+\t\t\t\t\t\t       lock_path.buf);\n+\tif (fd < 0) {\n+\t\tret = error_errno(_(\"unable to create '%s'\"), lock_path.buf);\n+\t\tgoto out;\n+\t}\n+\n+\tif (write_in_full(fd, contents.buf, contents.len) < 0) {\n+\t\tret = error_errno(_(\"unable to write '%s'\"), lock_path.buf);\n+\t\tclose(fd);\n+\t\tunlink(lock_path.buf);\n+\t\tgoto out;\n+\t}\n+\n+\tif (close(fd) < 0) {\n+\t\tret = error_errno(_(\"unable to close '%s'\"), lock_path.buf);\n+\t\tunlink(lock_path.buf);\n+\t\tgoto out;\n+\t}\n+\n+\tret = 0;\n+\n+out:\n+\tstrbuf_release(&lock_path);\n+\tstrbuf_release(&contents);\n+\n+\treturn ret;\n+}\n+\n static int odb_transaction_files_commit(struct odb_transaction *base)\n {\n \tstruct odb_transaction_files *transaction =\n@@ -1348,9 +1389,13 @@ static int odb_transaction_files_write_pack(struct odb_transaction *base,\n \t\tcontainer_of(base, struct odb_transaction_files, base);\n \tstruct repository *repo = base->source->odb->repo;\n \tstruct child_process child = CHILD_PROCESS_INIT;\n+\tstruct strbuf final_path = STRBUF_INIT;\n+\tstruct strbuf pack_name = STRBUF_INIT;\n+\tstruct strbuf keep_msg = STRBUF_INIT;\n \tstruct pack_header hdr;\n \tconst char *hdr_err;\n \tint err_fd = opts->err_fd;\n+\tint ret = -1;\n \tint status;\n \n \thdr_err = parse_pack_header(&hdr, pack_fd);\n@@ -1390,17 +1435,12 @@ static int odb_transaction_files_write_pack(struct odb_transaction *base,\n \t\t}\n \t} else {\n \t\tchar hostname[HOST_NAME_MAX + 1];\n-\t\tchar *lockfile;\n \n \t\tstrvec_pushl(&child.args, \"index-pack\", \"--stdin\", NULL);\n \t\tpush_header_arg(&child.args, &hdr);\n \n \t\tif (xgethostname(hostname, sizeof(hostname)))\n \t\t\txsnprintf(hostname, sizeof(hostname), \"localhost\");\n-\t\tstrvec_pushf(&child.args,\n-\t\t\t     \"--keep=receive-pack %\"PRIuMAX\" on %s\",\n-\t\t\t     (uintmax_t)getpid(),\n-\t\t\t     hostname);\n \n \t\tif (!opts->quiet && err_fd)\n \t\t\tstrvec_push(&child.args, \"--show-resolving-progress\");\n@@ -1424,34 +1464,57 @@ static int odb_transaction_files_write_pack(struct odb_transaction *base,\n \t\t\treturn -1;\n \t\t}\n \n-\t\t/*\n-\t\t * The lockfile filepath is expected to be the final location of\n-\t\t * the \".keep\" file after being migrated to the main ODB source.\n-\t\t * This ensures the lockfile can be found and removed later\n-\t\t * after the ODB transaction has been committed.\n-\t\t */\n-\t\tlockfile = index_pack_lockfile(base->source, child.out, NULL);\n-\t\tif (lockfile) {\n-\t\t\tALLOC_GROW(transaction->pack_lockfiles,\n-\t\t\t\t   transaction->pack_lockfiles_nr + 1,\n-\t\t\t\t   transaction->pack_lockfiles_alloc);\n-\t\t\ttransaction->pack_lockfiles[transaction->pack_lockfiles_nr++] =\n-\t\t\t\tregister_tempfile(lockfile);\n-\t\t\tfree(lockfile);\n-\t\t}\n+\t\tret = read_index_pack_report(repo, child.out, &pack_name, NULL);\n+\n \t\tclose(child.out);\n \n \t\tstatus = finish_command(&child);\n \t\tif (status) {\n \t\t\tstrbuf_addstr(err_msg, \"index-pack abnormal exit\");\n-\t\t\treturn -1;\n+\t\t\tgoto out;\n+\t\t}\n+\n+\t\tif (ret) {\n+\t\t\tstrbuf_addstr(err_msg, \"index-pack failed to report pack name\");\n+\t\t\tgoto out;\n \t\t}\n \n+\t\tstrbuf_addf(&keep_msg, \"receive-pack %\"PRIuMAX\" on %s\",\n+\t\t\t    (uintmax_t)getpid(), hostname);\n+\n+\t\tif (write_pack_lockfile(transaction->quarantine, pack_name.buf,\n+\t\t\t\t\tkeep_msg.buf)) {\n+\t\t\tstrbuf_addstr(err_msg, \"lockfile write failed\");\n+\t\t\tgoto out;\n+\t\t}\n+\n+\t\tALLOC_GROW(transaction->pack_lockfiles,\n+\t\t\t   transaction->pack_lockfiles_nr + 1,\n+\t\t\t   transaction->pack_lockfiles_alloc);\n+\n+\t\t/*\n+\t\t * The lockfile filepath is expected to be the final location of\n+\t\t * the \".keep\" file after being migrated to the main ODB source.\n+\t\t * This ensures the lockfile can be found and removed later\n+\t\t * after the ODB transaction has been committed.\n+\t\t */\n+\t\tstrbuf_addf(&final_path, \"%s/pack/pack-%s.keep\",\n+\t\t\t    base->source->path, pack_name.buf);\n+\t\ttransaction->pack_lockfiles[transaction->pack_lockfiles_nr++] =\n+\t\t\tregister_tempfile(final_path.buf);\n+\n \t\todb_source_prepare(transaction->quarantine,\n \t\t\t\t   ODB_PREPARE_FLUSH_CACHES);\n \t}\n \n-\treturn 0;\n+\tret = 0;\n+\n+out:\n+\tstrbuf_release(&final_path);\n+\tstrbuf_release(&pack_name);\n+\tstrbuf_release(&keep_msg);\n+\n+\treturn ret;\n }\n \n static int odb_transaction_files_finalize(struct odb_transaction *base)\n-- \n2.56.0.173.gf57c9a7388\n\n\n"},{"id":"555776","messageId":"20261021182528.3055494-5-jltobler@gmail.com","threadId":"66550","inReplyTo":"20261021182528.3055494-1-jltobler@gmail.com","subject":"[PATCH v4 4/4] object-file: write \".keep\" lockfiles during transaction commit","fromName":"Justin Tobler","fromEmail":"jltobler@gmail.com","sentAt":"2026-10-21T18:25:28Z","receivedAt":"2026-10-21T18:25:28Z","isPatch":true,"sender":{"key":"jltobler@gmail.com","avatar":"https://avatars.githubusercontent.com/u/53454972?v=4"},"body":"The \"files\" ODB backend writes a \".keep\" lockfile into the transaction\nquarantine directory via `odb_transaction_files_write_pack()`. When the\nODB transaction is committed, the \".keep\" lockfile is migrated to the\nprimary ODB alongside its packfile. Writing the temporary lockfile in\nthis manner has a couple of downsides:\n\n  - Migrating quarantined objects via `tmp_objdir_migrate()` doesn't\n    immediately stop if a \".keep\" lockfile already exists for the same\n    packfile in the primary ODB and instead allows all the remaining\n    objects to be migrated. So even though `odb_transaction_commit()`\n    ultimately returns an error here, the objects are needlessly written\n    to the primary ODB anyways.\n\n  - Even more concerning, the \".keep\" tempfile atexit(3) handler tries\n    to remove any lockfile of the same name regardless of whether it was\n    created by the same process. If the \".keep\" lockfile of the same\n    name for a concurrent process gets deleted prematurely, its packfile\n    becomes at risk of being deleted by concurrent housekeeping.\n\nStop creating/registering the temporary lockfiles when writing the\npackfile to the transaction and instead do so when committing the\ntransaction. This ensures the lockfile is properly acquired prior to\nmigrating objects and that only the owning process can delete its own\nlockfiles.\n\nReported-by: Qin ShiCheng <qeesung@live.com>\nSigned-off-by: Justin Tobler <jltobler@gmail.com>\n---\n object-file.c              | 76 +++++++++++++++++++++-----------------\n t/t5547-push-quarantine.sh | 30 +++++++++++++--\n 2 files changed, 70 insertions(+), 36 deletions(-)\n\ndiff --git a/object-file.c b/object-file.c\nindex 74a31760bc..e67c0865a2 100644\n--- a/object-file.c\n+++ b/object-file.c\n@@ -492,6 +492,7 @@ struct odb_transaction_files {\n \tstruct transaction_packfile packfile;\n \tconst char *prefix;\n \n+\tstruct strvec pack_names;\n \tstruct tempfile **pack_lockfiles;\n \tsize_t pack_lockfiles_nr;\n \tsize_t pack_lockfiles_alloc;\n@@ -1257,7 +1258,7 @@ int read_loose_object(struct repository *repo,\n }\n \n static int write_pack_lockfile(struct odb_source *source, const char *pack_name,\n-\t\t\t       const char *keep_msg)\n+\t\t\t       const char *keep_msg, struct tempfile **out)\n {\n \tstruct strbuf lock_path = STRBUF_INIT;\n \tstruct strbuf contents = STRBUF_INIT;\n@@ -1288,6 +1289,7 @@ static int write_pack_lockfile(struct odb_source *source, const char *pack_name,\n \t\tgoto out;\n \t}\n \n+\t*out = register_tempfile(lock_path.buf);\n \tret = 0;\n \n out:\n@@ -1297,6 +1299,40 @@ static int write_pack_lockfile(struct odb_source *source, const char *pack_name,\n \treturn ret;\n }\n \n+static int write_pack_lockfiles(struct odb_transaction_files *transaction)\n+{\n+\tstruct strbuf keep_msg = STRBUF_INIT;\n+\tchar hostname[HOST_NAME_MAX + 1];\n+\tint ret = 0;\n+\n+\tif (!transaction->pack_names.nr)\n+\t\treturn 0;\n+\n+\tif (xgethostname(hostname, sizeof(hostname)))\n+\t\txsnprintf(hostname, sizeof(hostname), \"localhost\");\n+\tstrbuf_addf(&keep_msg, \"receive-pack %\"PRIuMAX\" on %s\",\n+\t\t    (uintmax_t)getpid(), hostname);\n+\n+\tfor (size_t i = 0; i < transaction->pack_names.nr; i++) {\n+\t\tstruct tempfile *lock;\n+\n+\t\tret = write_pack_lockfile(transaction->base.source,\n+\t\t\t\t\t  transaction->pack_names.v[i],\n+\t\t\t\t\t  keep_msg.buf, &lock);\n+\t\tif (ret < 0)\n+\t\t\tbreak;\n+\n+\t\tALLOC_GROW(transaction->pack_lockfiles,\n+\t\t\t   transaction->pack_lockfiles_nr + 1,\n+\t\t\t   transaction->pack_lockfiles_alloc);\n+\t\ttransaction->pack_lockfiles[transaction->pack_lockfiles_nr++] = lock;\n+\t}\n+\n+\tstrbuf_release(&keep_msg);\n+\n+\treturn ret;\n+}\n+\n static int odb_transaction_files_commit(struct odb_transaction *base)\n {\n \tstruct odb_transaction_files *transaction =\n@@ -1325,6 +1361,9 @@ static int odb_transaction_files_commit(struct odb_transaction *base)\n \t\tdelete_tempfile(&temp);\n \t\tstrbuf_release(&temp_path);\n \n+\t\tif (write_pack_lockfiles(transaction))\n+\t\t\treturn error(_(\"unable to lock packfiles\"));\n+\n \t\t/*\n \t\t * Make the object files visible in the primary ODB after their data is\n \t\t * fully durable.\n@@ -1389,9 +1428,7 @@ static int odb_transaction_files_write_pack(struct odb_transaction *base,\n \t\tcontainer_of(base, struct odb_transaction_files, base);\n \tstruct repository *repo = base->source->odb->repo;\n \tstruct child_process child = CHILD_PROCESS_INIT;\n-\tstruct strbuf final_path = STRBUF_INIT;\n \tstruct strbuf pack_name = STRBUF_INIT;\n-\tstruct strbuf keep_msg = STRBUF_INIT;\n \tstruct pack_header hdr;\n \tconst char *hdr_err;\n \tint err_fd = opts->err_fd;\n@@ -1434,14 +1471,9 @@ static int odb_transaction_files_write_pack(struct odb_transaction *base,\n \t\t\treturn -1;\n \t\t}\n \t} else {\n-\t\tchar hostname[HOST_NAME_MAX + 1];\n-\n \t\tstrvec_pushl(&child.args, \"index-pack\", \"--stdin\", NULL);\n \t\tpush_header_arg(&child.args, &hdr);\n \n-\t\tif (xgethostname(hostname, sizeof(hostname)))\n-\t\t\txsnprintf(hostname, sizeof(hostname), \"localhost\");\n-\n \t\tif (!opts->quiet && err_fd)\n \t\t\tstrvec_push(&child.args, \"--show-resolving-progress\");\n \t\tif (err_fd)\n@@ -1479,29 +1511,7 @@ static int odb_transaction_files_write_pack(struct odb_transaction *base,\n \t\t\tgoto out;\n \t\t}\n \n-\t\tstrbuf_addf(&keep_msg, \"receive-pack %\"PRIuMAX\" on %s\",\n-\t\t\t    (uintmax_t)getpid(), hostname);\n-\n-\t\tif (write_pack_lockfile(transaction->quarantine, pack_name.buf,\n-\t\t\t\t\tkeep_msg.buf)) {\n-\t\t\tstrbuf_addstr(err_msg, \"lockfile write failed\");\n-\t\t\tgoto out;\n-\t\t}\n-\n-\t\tALLOC_GROW(transaction->pack_lockfiles,\n-\t\t\t   transaction->pack_lockfiles_nr + 1,\n-\t\t\t   transaction->pack_lockfiles_alloc);\n-\n-\t\t/*\n-\t\t * The lockfile filepath is expected to be the final location of\n-\t\t * the \".keep\" file after being migrated to the main ODB source.\n-\t\t * This ensures the lockfile can be found and removed later\n-\t\t * after the ODB transaction has been committed.\n-\t\t */\n-\t\tstrbuf_addf(&final_path, \"%s/pack/pack-%s.keep\",\n-\t\t\t    base->source->path, pack_name.buf);\n-\t\ttransaction->pack_lockfiles[transaction->pack_lockfiles_nr++] =\n-\t\t\tregister_tempfile(final_path.buf);\n+\t\tstrvec_push(&transaction->pack_names, pack_name.buf);\n \n \t\todb_source_prepare(transaction->quarantine,\n \t\t\t\t   ODB_PREPARE_FLUSH_CACHES);\n@@ -1510,9 +1520,7 @@ static int odb_transaction_files_write_pack(struct odb_transaction *base,\n \tret = 0;\n \n out:\n-\tstrbuf_release(&final_path);\n \tstrbuf_release(&pack_name);\n-\tstrbuf_release(&keep_msg);\n \n \treturn ret;\n }\n@@ -1527,6 +1535,7 @@ static int odb_transaction_files_finalize(struct odb_transaction *base)\n \t\tret |= delete_tempfile(&transaction->pack_lockfiles[i]);\n \n \tfree(transaction->pack_lockfiles);\n+\tstrvec_clear(&transaction->pack_names);\n \n \treturn ret;\n }\n@@ -1559,6 +1568,7 @@ int odb_transaction_files_begin(struct odb_source *source,\n \ttransaction->base.write_pack = odb_transaction_files_write_pack;\n \ttransaction->base.env = odb_transaction_files_env;\n \ttransaction->flags = flags;\n+\tstrvec_init(&transaction->pack_names);\n \n \ttransaction->prefix = \"bulk-fsync\";\n \tif (flags & ODB_TRANSACTION_RECEIVE) {\ndiff --git a/t/t5547-push-quarantine.sh b/t/t5547-push-quarantine.sh\nindex 1b7097179e..6c14dfc42b 100755\n--- a/t/t5547-push-quarantine.sh\n+++ b/t/t5547-push-quarantine.sh\n@@ -77,10 +77,9 @@ test_expect_success '.keep file is removed after push' '\n \tgit -C keep.git config set receive.unpackLimit 0 &&\n \n \t# While incoming objects are still quarantined, validate that the\n-\t# \".keep\" lockfile is present in the quarantine directory.\n+\t# \".keep\" lockfile is not present in the quarantine directory.\n \ttest_hook -C keep.git pre-receive <<-\\EOF &&\n-\tkeep=\"$(ls \"$GIT_QUARANTINE_PATH\"/pack/pack-*.keep)\" &&\n-\ttest -f \"$keep\"\n+\ttest ! -e \"$GIT_QUARANTINE_PATH\"/pack/pack-*.keep\n \tEOF\n \n \t# After quarantined objects are migrated, validate that the \".keep\"\n@@ -101,4 +100,29 @@ test_expect_success '.keep file is removed after push' '\n \ttest_path_is_missing \"$keep\"\n '\n \n+test_expect_success '.keep file created outside of process is not removed' '\n+\ttest_when_finished rm -rf keep.git &&\n+\tgit init --bare keep.git &&\n+\n+\tgit -C keep.git config set receive.unpackLimit 0 &&\n+\n+\t# Create a \".keep\" lockfile matching the packfile to simulate a\n+\t# concurrent git-receive-pack(1) that has already written its lockfile.\n+\ttest_hook -C keep.git pre-receive <<-\\EOF &&\n+\tpack=\"$(ls \"$GIT_QUARANTINE_PATH\"/pack/pack-*.pack)\" &&\n+\tpack=\"${pack##*/}\" &&\n+\techo \"baz\" >\"objects/pack/${pack%.pack}.keep\"\n+\tEOF\n+\n+\ttest_commit bar &&\n+\ttest_must_fail git push keep.git HEAD &&\n+\n+\t# The \".keep\" lockfile of the concurrent git-receive-pack(1) should be\n+\t# left in its place.\n+\tkeep=\"$(ls keep.git/objects/pack/pack-*.keep)\" &&\n+\ttest_path_is_file \"$keep\" &&\n+\ttest_grep \"baz\" \"$keep\" &&\n+\ttest_path_is_missing \"${keep%.keep}.pack\"\n+'\n+\n test_done\n-- \n2.56.0.173.gf57c9a7388\n\n\n"},{"id":"555821","messageId":"atmle8tlGVcHpzs4@pks.im","threadId":"66550","inReplyTo":"20261021182528.3055494-1-jltobler@gmail.com","subject":"Re: [PATCH v4 0/4] object-file: fix \".keep\" lockfile ownership in ODB transactions","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-10-22T05:56:11Z","receivedAt":"2026-10-22T05:56:11Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"On Wed, Oct 21, 2026 at 01:25:24PM -0500, Justin Tobler wrote:\n> Change since V3:\n>   - Followed Patrick's suggestion correctly this time.\n\nThanks for bearing with me. I'm happy with this version.\n\nPatrick\n\n"},{"id":"556356","messageId":"CAOLa=ZTnEJDPB6_fdFQFcM=zV21HEZ4gzzhWqB=A_euSzUut_w@mail.gmail.com","threadId":"66550","inReplyTo":"20261021182528.3055494-2-jltobler@gmail.com","subject":"Re: [PATCH v4 1/4] pack-write: make index-pack output comment hash agnostic","fromName":"Karthik Nayak","fromEmail":"karthik.188@gmail.com","sentAt":"2026-10-26T15:01:53Z","receivedAt":"2026-10-26T15:01:53Z","isPatch":true,"sender":{"key":"karthik.188@gmail.com","avatar":"https://avatars.githubusercontent.com/u/1786334?v=4"},"body":"Justin Tobler <jltobler@gmail.com> writes:\n\n> The code comment in `index_pack_lockfile()` explains the expected format\n> of output from git-index-pack(1). As written it explicitly mentions\n> SHA1, but both the git-index-pack(1) output and the parsing are not\n> specific to SHA1 and also support SHA256.\n>\n> Update the comment explanation to be hash agnostic accordingly.\n>\n> Signed-off-by: Justin Tobler <jltobler@gmail.com>\n> ---\n>  pack-write.c | 14 +++++++++-----\n>  1 file changed, 9 insertions(+), 5 deletions(-)\n>\n> diff --git a/pack-write.c b/pack-write.c\n> index 85674e4b72..b1311c9923 100644\n> --- a/pack-write.c\n> +++ b/pack-write.c\n> @@ -476,11 +476,15 @@ char *index_pack_lockfile(struct odb_source *source, int ip_out,\n>  \tconst int len = source->odb->repo->hash_algo->hexsz + 6;\n>\n>  \t/*\n> -\t * The first thing we expect from index-pack's output\n> -\t * is \"pack\\t%40s\\n\" or \"keep\\t%40s\\n\" (46 bytes) where\n> -\t * %40s is the newly created pack SHA1 name.  In the \"keep\"\n> -\t * case, we need it to remove the corresponding .keep file\n> -\t * later on.  If we don't get that then tough luck with it.\n> +\t * The first thing we expect from index-pack's output is\n> +\t * \"pack\\t<name>\\n\" or \"keep\\t<name>\\n\", where <name> is the hexadecimal\n> +\t * name of the packfile it has just written. The line is therefore five\n> +\t * bytes of prefix, `hexsz` bytes of name, and a trailing newline. A\n> +\t * \"keep\" prefix tells us that index-pack has also created the\n> +\t * corresponding \".keep\" file.\n> +\t *\n> +\t * In the \"keep\" case, we need it to remove the corresponding \".keep\"\n> +\t * file later on. If we don't get that then tough luck with it.\n\nWe start off saying we expect either '\"pack\\t<name>\\n\" or\n\"keep\\t<name>\\n\"'. But the last line makes it seem like the latter is\nalways expected? Is that the case, or should this comment be moved to\nthe caller in 'fetch-pack.c'?\n\n>  \t */\n>  \tif (read_in_full(ip_out, packname, len) == len && packname[len-1] == '\\n') {\n>  \t\tconst char *name;\n> --\n> 2.56.0.173.gf57c9a7388\n"},{"id":"556359","messageId":"at9v9p64l72xFEi8@denethor","threadId":"66550","inReplyTo":"CAOLa=ZTnEJDPB6_fdFQFcM=zV21HEZ4gzzhWqB=A_euSzUut_w@mail.gmail.com","subject":"Re: [PATCH v4 1/4] pack-write: make index-pack output comment hash agnostic","fromName":"Justin Tobler","fromEmail":"jltobler@gmail.com","sentAt":"2026-10-26T15:44:35Z","receivedAt":"2026-10-26T15:44:35Z","isPatch":true,"sender":{"key":"jltobler@gmail.com","avatar":"https://avatars.githubusercontent.com/u/53454972?v=4"},"body":"On 26/10/26 08:01AM, Karthik Nayak wrote:\n> Justin Tobler <jltobler@gmail.com> writes:\n> \n> >  \t/*\n> > -\t * The first thing we expect from index-pack's output\n> > -\t * is \"pack\\t%40s\\n\" or \"keep\\t%40s\\n\" (46 bytes) where\n> > -\t * %40s is the newly created pack SHA1 name.  In the \"keep\"\n> > -\t * case, we need it to remove the corresponding .keep file\n> > -\t * later on.  If we don't get that then tough luck with it.\n> > +\t * The first thing we expect from index-pack's output is\n> > +\t * \"pack\\t<name>\\n\" or \"keep\\t<name>\\n\", where <name> is the hexadecimal\n> > +\t * name of the packfile it has just written. The line is therefore five\n> > +\t * bytes of prefix, `hexsz` bytes of name, and a trailing newline. A\n> > +\t * \"keep\" prefix tells us that index-pack has also created the\n> > +\t * corresponding \".keep\" file.\n> > +\t *\n> > +\t * In the \"keep\" case, we need it to remove the corresponding \".keep\"\n> > +\t * file later on. If we don't get that then tough luck with it.\n> \n> We start off saying we expect either '\"pack\\t<name>\\n\" or\n> \"keep\\t<name>\\n\"'. But the last line makes it seem like the latter is\n> always expected? Is that the case, or should this comment be moved to\n> the caller in 'fetch-pack.c'?\n\nEven though all callers of this function specify the `--keep` option to\nthe git-index-pack(1) generating the output, it is expected that the\noutput may be either \"pack\" or \"keep\". When git-index-pack(1) tries to\ncreate a lockfile, but one already exists, it simply ignores the error\nand returns a \"pack\" output line. The last line is from the original\ncomment and indicates that when we do identify that a \".keep\" file was\ncreated, it is something the caller must handle later on.\n\nThe intent of this patch was just to update the comment to be hash\nagnostic. The next patch splits out the `read_index_pack_report()`\nfunction from `index_pack_lockfile()` and the last comment line is\nremoved accordingly. So I'm inclined to leave this patch as-is.\n\n-Justin\n\n"},{"id":"556368","messageId":"CAOLa=ZSdT2W19-Vg682iv6zhq6sEFM0+rHS-NqL=XQu--JOknw@mail.gmail.com","threadId":"66550","inReplyTo":"20261021182528.3055494-4-jltobler@gmail.com","subject":"Re: [PATCH v4 3/4] object-file: explicitly write \".keep\" lockfile","fromName":"Karthik Nayak","fromEmail":"karthik.188@gmail.com","sentAt":"2026-10-26T16:08:30Z","receivedAt":"2026-10-26T16:08:30Z","isPatch":true,"sender":{"key":"karthik.188@gmail.com","avatar":"https://avatars.githubusercontent.com/u/1786334?v=4"},"body":"Justin Tobler <jltobler@gmail.com> writes:\n\n> The `odb_transaction_write_pack()` interface for the \"files\" backend\n> relies on git-index-pack(1) to write the specified packfile into an ODB\n> transaction as a packfile. To prevent a concurrent housekeeping\n> operation from deleting the newly written packfile prior to the\n> references being updated, a \".keep\" lockfile is also generated by\n> git-index-pack(1) via the `--keep` option. Consequently, the \".keep\"\n> lockfile is written into the ODB transaction alongside the packfile and\n> later migrated together to the primary ODB during transaction commit.\n>\n> In a subsequent commit, writing the lockfile is deferred until\n> transaction commit and thus cannot rely on git-index-pack(1) to write\n> it. In preparation for this, update `odb_transaction_files_write_pack()`\n> to explicitly generate the same \".keep\" lockfile itself.\n>\n> Signed-off-by: Justin Tobler <jltobler@gmail.com>\n> ---\n>  object-file.c | 107 +++++++++++++++++++++++++++++++++++++++-----------\n>  1 file changed, 85 insertions(+), 22 deletions(-)\n>\n> diff --git a/object-file.c b/object-file.c\n> index 210984f825..74a31760bc 100644\n> --- a/object-file.c\n> +++ b/object-file.c\n> @@ -1256,6 +1256,47 @@ int read_loose_object(struct repository *repo,\n>  \treturn ret;\n>  }\n>\n> +static int write_pack_lockfile(struct odb_source *source, const char *pack_name,\n> +\t\t\t       const char *keep_msg)\n> +{\n> +\tstruct strbuf lock_path = STRBUF_INIT;\n> +\tstruct strbuf contents = STRBUF_INIT;\n> +\tint ret;\n> +\tint fd;\n> +\n> +\tstrbuf_addf(&lock_path, \"%s/pack/pack-%s.keep\",\n> +\t\t    source->path, pack_name);\n> +\tstrbuf_addf(&contents, \"%s\\n\", keep_msg);\n> +\n> +\tfd = safe_create_file_with_leading_directories(source->odb->repo,\n> +\t\t\t\t\t\t       lock_path.buf);\n> +\tif (fd < 0) {\n> +\t\tret = error_errno(_(\"unable to create '%s'\"), lock_path.buf);\n> +\t\tgoto out;\n> +\t}\n> +\n> +\tif (write_in_full(fd, contents.buf, contents.len) < 0) {\n> +\t\tret = error_errno(_(\"unable to write '%s'\"), lock_path.buf);\n> +\t\tclose(fd);\n> +\t\tunlink(lock_path.buf);\n\nShould we use `unlink_or_warn()` here?\n\n[snip]\n\nThe rest of the patch looks good.\n"},{"id":"556411","messageId":"CAOLa=ZTDm25XDambq4ohmeURaBuE29EwS6ORhHc6PDVaKyGWRA@mail.gmail.com","threadId":"66550","inReplyTo":"20261021182528.3055494-5-jltobler@gmail.com","subject":"Re: [PATCH v4 4/4] object-file: write \".keep\" lockfiles during transaction commit","fromName":"Karthik Nayak","fromEmail":"karthik.188@gmail.com","sentAt":"2026-10-26T21:49:36Z","receivedAt":"2026-10-26T21:49:36Z","isPatch":true,"sender":{"key":"karthik.188@gmail.com","avatar":"https://avatars.githubusercontent.com/u/1786334?v=4"},"body":"Justin Tobler <jltobler@gmail.com> writes:\n\n> The \"files\" ODB backend writes a \".keep\" lockfile into the transaction\n> quarantine directory via `odb_transaction_files_write_pack()`. When the\n> ODB transaction is committed, the \".keep\" lockfile is migrated to the\n> primary ODB alongside its packfile. Writing the temporary lockfile in\n> this manner has a couple of downsides:\n>\n>   - Migrating quarantined objects via `tmp_objdir_migrate()` doesn't\n>     immediately stop if a \".keep\" lockfile already exists for the same\n>     packfile in the primary ODB and instead allows all the remaining\n>     objects to be migrated. So even though `odb_transaction_commit()`\n>     ultimately returns an error here, the objects are needlessly written\n>     to the primary ODB anyways.\n>\n>   - Even more concerning, the \".keep\" tempfile atexit(3) handler tries\n>     to remove any lockfile of the same name regardless of whether it was\n>     created by the same process. If the \".keep\" lockfile of the same\n>     name for a concurrent process gets deleted prematurely, its packfile\n>     becomes at risk of being deleted by concurrent housekeeping.\n>\n> Stop creating/registering the temporary lockfiles when writing the\n> packfile to the transaction and instead do so when committing the\n> transaction. This ensures the lockfile is properly acquired prior to\n> migrating objects and that only the owning process can delete its own\n> lockfiles.\n>\n\nSo to reiterate, the old method was to ask index-pack to also write the\n'.keep' file in the quarantine directory. Post that, all the files in\nthe directory were moved to the canonical location.\n\nNow, we handle '.keep' file creation directly. Nit: There is a logical\njump from the last-1 sentence to the last sentence. How does moving the\nlockfile creation to the commit phase solve these issues?\n\n> Reported-by: Qin ShiCheng <qeesung@live.com>\n> Signed-off-by: Justin Tobler <jltobler@gmail.com>\n> ---\n>  object-file.c              | 76 +++++++++++++++++++++-----------------\n>  t/t5547-push-quarantine.sh | 30 +++++++++++++--\n>  2 files changed, 70 insertions(+), 36 deletions(-)\n>\n> diff --git a/object-file.c b/object-file.c\n> index 74a31760bc..e67c0865a2 100644\n> --- a/object-file.c\n> +++ b/object-file.c\n> @@ -492,6 +492,7 @@ struct odb_transaction_files {\n>  \tstruct transaction_packfile packfile;\n>  \tconst char *prefix;\n>\n> +\tstruct strvec pack_names;\n>  \tstruct tempfile **pack_lockfiles;\n>  \tsize_t pack_lockfiles_nr;\n>  \tsize_t pack_lockfiles_alloc;\n> @@ -1257,7 +1258,7 @@ int read_loose_object(struct repository *repo,\n>  }\n>\n>  static int write_pack_lockfile(struct odb_source *source, const char *pack_name,\n> -\t\t\t       const char *keep_msg)\n> +\t\t\t       const char *keep_msg, struct tempfile **out)\n>  {\n>  \tstruct strbuf lock_path = STRBUF_INIT;\n>  \tstruct strbuf contents = STRBUF_INIT;\n> @@ -1288,6 +1289,7 @@ static int write_pack_lockfile(struct odb_source *source, const char *pack_name,\n>  \t\tgoto out;\n>  \t}\n>\n> +\t*out = register_tempfile(lock_path.buf);\n>  \tret = 0;\n>\n>  out:\n> @@ -1297,6 +1299,40 @@ static int write_pack_lockfile(struct odb_source *source, const char *pack_name,\n>  \treturn ret;\n>  }\n>\n> +static int write_pack_lockfiles(struct odb_transaction_files *transaction)\n> +{\n> +\tstruct strbuf keep_msg = STRBUF_INIT;\n> +\tchar hostname[HOST_NAME_MAX + 1];\n> +\tint ret = 0;\n> +\n> +\tif (!transaction->pack_names.nr)\n> +\t\treturn 0;\n> +\n> +\tif (xgethostname(hostname, sizeof(hostname)))\n> +\t\txsnprintf(hostname, sizeof(hostname), \"localhost\");\n> +\tstrbuf_addf(&keep_msg, \"receive-pack %\"PRIuMAX\" on %s\",\n> +\t\t    (uintmax_t)getpid(), hostname);\n> +\n> +\tfor (size_t i = 0; i < transaction->pack_names.nr; i++) {\n> +\t\tstruct tempfile *lock;\n> +\n> +\t\tret = write_pack_lockfile(transaction->base.source,\n> +\t\t\t\t\t  transaction->pack_names.v[i],\n> +\t\t\t\t\t  keep_msg.buf, &lock);\n> +\t\tif (ret < 0)\n> +\t\t\tbreak;\n> +\n> +\t\tALLOC_GROW(transaction->pack_lockfiles,\n> +\t\t\t   transaction->pack_lockfiles_nr + 1,\n> +\t\t\t   transaction->pack_lockfiles_alloc);\n> +\t\ttransaction->pack_lockfiles[transaction->pack_lockfiles_nr++] = lock;\n> +\t}\n> +\n> +\tstrbuf_release(&keep_msg);\n> +\n> +\treturn ret;\n> +}\n>\n\nOkay so this function handles the lockfile creation for each of the\npacks. Looks good.\n\n[snip]\n\n> @@ -1434,14 +1471,9 @@ static int odb_transaction_files_write_pack(struct odb_transaction *base,\n>  \t\t\treturn -1;\n>  \t\t}\n>  \t} else {\n> -\t\tchar hostname[HOST_NAME_MAX + 1];\n> -\n>  \t\tstrvec_pushl(&child.args, \"index-pack\", \"--stdin\", NULL);\n>  \t\tpush_header_arg(&child.args, &hdr);\n>\n> -\t\tif (xgethostname(hostname, sizeof(hostname)))\n> -\t\t\txsnprintf(hostname, sizeof(hostname), \"localhost\");\n> -\n>  \t\tif (!opts->quiet && err_fd)\n>  \t\t\tstrvec_push(&child.args, \"--show-resolving-progress\");\n>  \t\tif (err_fd)\n> @@ -1479,29 +1511,7 @@ static int odb_transaction_files_write_pack(struct odb_transaction *base,\n>  \t\t\tgoto out;\n>  \t\t}\n>\n> -\t\tstrbuf_addf(&keep_msg, \"receive-pack %\"PRIuMAX\" on %s\",\n> -\t\t\t    (uintmax_t)getpid(), hostname);\n> -\n> -\t\tif (write_pack_lockfile(transaction->quarantine, pack_name.buf,\n> -\t\t\t\t\tkeep_msg.buf)) {\n> -\t\t\tstrbuf_addstr(err_msg, \"lockfile write failed\");\n> -\t\t\tgoto out;\n> -\t\t}\n> -\n> -\t\tALLOC_GROW(transaction->pack_lockfiles,\n> -\t\t\t   transaction->pack_lockfiles_nr + 1,\n> -\t\t\t   transaction->pack_lockfiles_alloc);\n> -\n> -\t\t/*\n> -\t\t * The lockfile filepath is expected to be the final location of\n> -\t\t * the \".keep\" file after being migrated to the main ODB source.\n> -\t\t * This ensures the lockfile can be found and removed later\n> -\t\t * after the ODB transaction has been committed.\n> -\t\t */\n> -\t\tstrbuf_addf(&final_path, \"%s/pack/pack-%s.keep\",\n> -\t\t\t    base->source->path, pack_name.buf);\n> -\t\ttransaction->pack_lockfiles[transaction->pack_lockfiles_nr++] =\n> -\t\t\tregister_tempfile(final_path.buf);\n> +\t\tstrvec_push(&transaction->pack_names, pack_name.buf);\n>\n>  \t\todb_source_prepare(transaction->quarantine,\n>  \t\t\t\t   ODB_PREPARE_FLUSH_CACHES);\n> @@ -1510,9 +1520,7 @@ static int odb_transaction_files_write_pack(struct odb_transaction *base,\n>  \tret = 0;\n>\n>  out:\n> -\tstrbuf_release(&final_path);\n>  \tstrbuf_release(&pack_name);\n> -\tstrbuf_release(&keep_msg);\n>\n>  \treturn ret;\n>  }\n>\n\nSo writing the individual pack no longer is concerned about also writing\nthe keep file. Makes sense.\n\n[snip]\n"},{"id":"556412","messageId":"CAOLa=ZSSwWTncnn99nRTfrKLCYF_DDbhHjNjcrsTggKUCoXJpw@mail.gmail.com","threadId":"66550","inReplyTo":"20261021182528.3055494-4-jltobler@gmail.com","subject":"Re: [PATCH v4 3/4] object-file: explicitly write \".keep\" lockfile","fromName":"Karthik Nayak","fromEmail":"karthik.188@gmail.com","sentAt":"2026-10-26T21:51:47Z","receivedAt":"2026-10-26T21:51:47Z","isPatch":true,"sender":{"key":"karthik.188@gmail.com","avatar":"https://avatars.githubusercontent.com/u/1786334?v=4"},"body":"Justin Tobler <jltobler@gmail.com> writes:\n\n[snip]\n\n> diff --git a/object-file.c b/object-file.c\n> index 210984f825..74a31760bc 100644\n> --- a/object-file.c\n> +++ b/object-file.c\n> @@ -1424,34 +1464,57 @@ static int odb_transaction_files_write_pack(struct odb_transaction *base,\n>  \t\t\treturn -1;\n>  \t\t}\n>\n> -\t\t/*\n> -\t\t * The lockfile filepath is expected to be the final location of\n> -\t\t * the \".keep\" file after being migrated to the main ODB source.\n> -\t\t * This ensures the lockfile can be found and removed later\n> -\t\t * after the ODB transaction has been committed.\n> -\t\t */\n> -\t\tlockfile = index_pack_lockfile(base->source, child.out, NULL);\n> -\t\tif (lockfile) {\n> -\t\t\tALLOC_GROW(transaction->pack_lockfiles,\n> -\t\t\t\t   transaction->pack_lockfiles_nr + 1,\n> -\t\t\t\t   transaction->pack_lockfiles_alloc);\n> -\t\t\ttransaction->pack_lockfiles[transaction->pack_lockfiles_nr++] =\n> -\t\t\t\tregister_tempfile(lockfile);\n> -\t\t\tfree(lockfile);\n> -\t\t}\n> +\t\tret = read_index_pack_report(repo, child.out, &pack_name, NULL);\n> +\n>  \t\tclose(child.out);\n>\n>  \t\tstatus = finish_command(&child);\n>  \t\tif (status) {\n>  \t\t\tstrbuf_addstr(err_msg, \"index-pack abnormal exit\");\n> -\t\t\treturn -1;\n> +\t\t\tgoto out;\n> +\t\t}\n> +\n> +\t\tif (ret) {\n> +\t\t\tstrbuf_addstr(err_msg, \"index-pack failed to report pack name\");\n> +\t\t\tgoto out;\n>  \t\t}\n>\n> +\t\tstrbuf_addf(&keep_msg, \"receive-pack %\"PRIuMAX\" on %s\",\n> +\t\t\t    (uintmax_t)getpid(), hostname);\n> +\n> +\t\tif (write_pack_lockfile(transaction->quarantine, pack_name.buf,\n> +\t\t\t\t\tkeep_msg.buf)) {\n> +\t\t\tstrbuf_addstr(err_msg, \"lockfile write failed\");\n> +\t\t\tgoto out;\n\nShouldn't we also set `ret = -1` here?\n\n> +\t\t}\n> +\n\n[snip]\n"},{"id":"556552","messageId":"auDsoWt5XVXw2hPN@denethor","threadId":"66550","inReplyTo":"CAOLa=ZSdT2W19-Vg682iv6zhq6sEFM0+rHS-NqL=XQu--JOknw@mail.gmail.com","subject":"Re: [PATCH v4 3/4] object-file: explicitly write \".keep\" lockfile","fromName":"Justin Tobler","fromEmail":"jltobler@gmail.com","sentAt":"2026-10-27T18:26:27Z","receivedAt":"2026-10-27T18:26:27Z","isPatch":true,"sender":{"key":"jltobler@gmail.com","avatar":"https://avatars.githubusercontent.com/u/53454972?v=4"},"body":"On 26/10/26 09:08AM, Karthik Nayak wrote:\n> Justin Tobler <jltobler@gmail.com> writes:\n> > +\tif (write_in_full(fd, contents.buf, contents.len) < 0) {\n> > +\t\tret = error_errno(_(\"unable to write '%s'\"), lock_path.buf);\n> > +\t\tclose(fd);\n> > +\t\tunlink(lock_path.buf);\n> \n> Should we use `unlink_or_warn()` here?\n\nYa, adding a warning here would probably be useful. I'll go ahead and do\nso in the next version.\n\nThanks,\n-Justin\n\n"},{"id":"556554","messageId":"auDt1rq4a_ENlQiK@denethor","threadId":"66550","inReplyTo":"auDsoWt5XVXw2hPN@denethor","subject":"Re: [PATCH v4 3/4] object-file: explicitly write \".keep\" lockfile","fromName":"Justin Tobler","fromEmail":"jltobler@gmail.com","sentAt":"2026-10-27T18:32:28Z","receivedAt":"2026-10-27T18:32:28Z","isPatch":true,"sender":{"key":"jltobler@gmail.com","avatar":"https://avatars.githubusercontent.com/u/53454972?v=4"},"body":"On 26/10/27 01:26PM, Justin Tobler wrote:\n> On 26/10/26 09:08AM, Karthik Nayak wrote:\n> > Justin Tobler <jltobler@gmail.com> writes:\n> > > +\tif (write_in_full(fd, contents.buf, contents.len) < 0) {\n> > > +\t\tret = error_errno(_(\"unable to write '%s'\"), lock_path.buf);\n> > > +\t\tclose(fd);\n> > > +\t\tunlink(lock_path.buf);\n> > \n> > Should we use `unlink_or_warn()` here?\n> \n> Ya, adding a warning here would probably be useful. I'll go ahead and do\n> so in the next version.\n\nI just noticed that this series was already merged to next, so I can\njust send this change a separate patch.\n\n-Justin\n\n"}]}