t4053: avoid writing to unopened pipe

This fixes an occasional hang I see when running t4053 with
--verbose-log using dash.

Commit 1e3f26542a (diff --no-index: support reading from named pipes,
2023-07-05) added a test that "diff --no-index" will complain when
comparing a named pipe and a directory. The minimum we need to test this
is to mkfifo the pipe, and then run "git diff --no-index pipe some_dir".
But the test does one thing more: it spawns a background shell process
that opens the pipe for writing, like this:

        {
                (>pipe) &
        } &&

This extra writer _could_ be useful if Git misbehaves and tries to open
the pipe for reading. Without the writer, Git would block indefinitely
and the test would never end. But since we do not have such a bug, Git
does not open the pipe and it is the writing process which will block
indefinitely, since there are no readers. The test addresses this by
running "kill $!" in a test_when_finished block. Since the writer should
be blocking forever, this kill command will reliably find it waiting.

However, this seems to be somewhat racy, in that the writing process
sometimes hangs around even after the "kill". In a normal run of the
test script without options, this doesn't have any effect; the
main test script completes anyway. But with --verbose-log, we spawn a
"tee" process that reads the script output, and it won't end until all
descriptors pointing to its input pipe are closed. And the background
process that is hanging around still has its stderr, etc, pointed into
that pipe.

You can reproduce the situation like this:

  cd t
  ./t4053-diff-no-index.sh --verbose-log --stress

Let that run for a few minutes, and then you'll find that some of the
runs have hung. For example, at 11:53, I ran:

  $ ps xk start o pid,start,command | grep tee | head
   713459 11:48:06 tee -a /home/peff/compile/git/t/test-results/t4053-diff-no-index.stress-9.out
   713527 11:48:06 tee -a /home/peff/compile/git/t/test-results/t4053-diff-no-index.stress-15.out
   719434 11:48:07 tee -a /home/peff/compile/git/t/test-results/t4053-diff-no-index.stress-1.out
   728117 11:48:08 tee -a /home/peff/compile/git/t/test-results/t4053-diff-no-index.stress-5.out
   738738 11:48:09 tee -a /home/peff/compile/git/t/test-results/t4053-diff-no-index.stress-31.out
   739457 11:48:09 tee -a /home/peff/compile/git/t/test-results/t4053-diff-no-index.stress-27.out
   744432 11:48:10 tee -a /home/peff/compile/git/t/test-results/t4053-diff-no-index.stress-21.out
   744471 11:48:10 tee -a /home/peff/compile/git/t/test-results/t4053-diff-no-index.stress-29.out
   761961 11:48:12 tee -a /home/peff/compile/git/t/test-results/t4053-diff-no-index.stress-0.out
   812299 11:48:19 tee -a /home/peff/compile/git/t/test-results/t4053-diff-no-index.stress-8.out

All of these have been hung for several minutes. We can investigate one
and see that it's waiting to get EOF on its input:

  $ strace -p 713459
  strace: Process 713459 attached
  read(0,
  ^C

Who else has that descriptor open?

  $ lsof -a -p 713459 -d 0 +E
  COMMAND    PID USER   FD   TYPE DEVICE SIZE/OFF    NODE NAME
  tee     713459 peff    0r  FIFO   0,13      0t0 3943636 pipe 719203,sh,5w 719203,sh,7w 719203,sh,12w 719203,sh,13w
  sh      719203 peff    5w  FIFO   0,13      0t0 3943636 pipe 713459,tee,0r 719203,sh,7w 719203,sh,12w 719203,sh,13w
  sh      719203 peff    7w  FIFO   0,13      0t0 3943636 pipe 713459,tee,0r 719203,sh,5w 719203,sh,12w 719203,sh,13w
  sh      719203 peff   12w  FIFO   0,13      0t0 3943636 pipe 713459,tee,0r 719203,sh,5w 719203,sh,7w 719203,sh,13w
  sh      719203 peff   13w  FIFO   0,13      0t0 3943636 pipe 713459,tee,0r 719203,sh,5w 719203,sh,7w 719203,sh,12w

It's a shell, presumably a subshell spawned by the main script. Though
it may seem odd, having the same descriptor open several times is not
unreasonable (they're all basically the original stdout/stderr of the
script that has been copied). And they should all close when the process
exits. So what's it doing? Curiously, it will exit as soon as we strace
it:

  $ strace -s 64 -p 719203
  strace: Process 719203 attached
  openat(AT_FDCWD, "pipe", O_WRONLY|O_CREAT|O_TRUNC, 0666) = -1 ENOENT (No such file or directory)
  write(2, "./t4053-diff-no-index.sh: 7: eval: ", 35) = 35
  write(2, "cannot create pipe: Directory nonexistent", 41) = 41
  write(2, "\n", 1)                       = 1
  exit_group(2)                           = ?
  +++ exited with 2 +++

I think what happens is this:

  - it is blocking in the openat() call for the pipe, as we expect (so
    this is definitely the backgrounded subshell mentioned above)

  - strace sends signals (probably STOP/CONT); those cause the kernel to
    stop blocking, but libc will restart the system call automatically

  - by this time, the "pipe" fifo is gone, so we'll actually try to
    create a regular file. But of course the surrounding directory is
    gone, too! So we get ENOENT, and then exit as normal.

So the blocking is something we expect to happen. But what we didn't
expect is for the process to still exist at all! It should have been
killed earlier when the parent process called "kill", but it wasn't. And
we can't catch the race at this point, because it happened much earlier.

One can guess, though, that there is some race with the shell setting up
the signal handling in the backgrounded subshell, and possibly blocking
or ignoring signals at the time that the "kill" is received.  Curiously,
the race does not seem to happen if I use "bash" instead of "dash", so
presumably bash's setup here is more atomic.

One fix might be to try killing the subshell more aggressively, either
using SIGKILL, or looping on kill/wait. But that seems complex and
likely to introduce new problems/races. Instead, we can observe that the
writer is not needed at all. Git will notice the pipe via stat() before
it is ever opened. So we can simply drop the writer subshell entirely.

If we ever changed Git to open the path and fstat() it, this would
result in the test hanging. But we're not likely to do that. After all,
we have to stat() paths to see if they are openable at all (e.g., it
could be a directory), so this seems like a low risk. And anybody who
does make such a change will immediately see the issue, as Git would
hang consistently.

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 file changed
tree: b76c3861affedd94a50c0dcb5b3b94be9bcd2f4e
  1. .github/
  2. block-sha1/
  3. builtin/
  4. ci/
  5. compat/
  6. contrib/
  7. Documentation/
  8. ewah/
  9. git-gui/
  10. gitk-git/
  11. gitweb/
  12. mergetools/
  13. negotiator/
  14. oss-fuzz/
  15. perl/
  16. po/
  17. refs/
  18. reftable/
  19. sha1dc/
  20. sha256/
  21. t/
  22. templates/
  23. trace2/
  24. xdiff/
  25. .cirrus.yml
  26. .clang-format
  27. .editorconfig
  28. .gitattributes
  29. .gitignore
  30. .gitmodules
  31. .mailmap
  32. .tsan-suppressions
  33. abspath.c
  34. abspath.h
  35. aclocal.m4
  36. add-interactive.c
  37. add-interactive.h
  38. add-patch.c
  39. advice.c
  40. advice.h
  41. alias.c
  42. alias.h
  43. alloc.c
  44. alloc.h
  45. apply.c
  46. apply.h
  47. archive-tar.c
  48. archive-zip.c
  49. archive.c
  50. archive.h
  51. attr.c
  52. attr.h
  53. banned.h
  54. base85.c
  55. base85.h
  56. bisect.c
  57. bisect.h
  58. blame.c
  59. blame.h
  60. blob.c
  61. blob.h
  62. bloom.c
  63. bloom.h
  64. branch.c
  65. branch.h
  66. builtin.h
  67. bulk-checkin.c
  68. bulk-checkin.h
  69. bundle-uri.c
  70. bundle-uri.h
  71. bundle.c
  72. bundle.h
  73. cache-tree.c
  74. cache-tree.h
  75. cache.h
  76. cbtree.c
  77. cbtree.h
  78. chdir-notify.c
  79. chdir-notify.h
  80. check-builtins.sh
  81. checkout.c
  82. checkout.h
  83. chunk-format.c
  84. chunk-format.h
  85. CODE_OF_CONDUCT.md
  86. color.c
  87. color.h
  88. column.c
  89. column.h
  90. combine-diff.c
  91. command-list.txt
  92. commit-graph.c
  93. commit-graph.h
  94. commit-reach.c
  95. commit-reach.h
  96. commit-slab-decl.h
  97. commit-slab-impl.h
  98. commit-slab.h
  99. commit.c
  100. commit.h
  101. common-main.c
  102. config.c
  103. config.h
  104. config.mak.dev
  105. config.mak.in
  106. config.mak.uname
  107. configure.ac
  108. connect.c
  109. connect.h
  110. connected.c
  111. connected.h
  112. convert.c
  113. convert.h
  114. copy.c
  115. copy.h
  116. COPYING
  117. credential.c
  118. credential.h
  119. csum-file.c
  120. csum-file.h
  121. ctype.c
  122. daemon.c
  123. date.c
  124. date.h
  125. decorate.c
  126. decorate.h
  127. delta-islands.c
  128. delta-islands.h
  129. delta.h
  130. detect-compiler
  131. diagnose.c
  132. diagnose.h
  133. diff-delta.c
  134. diff-lib.c
  135. diff-merges.c
  136. diff-merges.h
  137. diff-no-index.c
  138. diff.c
  139. diff.h
  140. diffcore-break.c
  141. diffcore-delta.c
  142. diffcore-order.c
  143. diffcore-pickaxe.c
  144. diffcore-rename.c
  145. diffcore-rotate.c
  146. diffcore.h
  147. dir-iterator.c
  148. dir-iterator.h
  149. dir.c
  150. dir.h
  151. editor.c
  152. editor.h
  153. entry.c
  154. entry.h
  155. environment.c
  156. environment.h
  157. exec-cmd.c
  158. exec-cmd.h
  159. fetch-negotiator.c
  160. fetch-negotiator.h
  161. fetch-pack.c
  162. fetch-pack.h
  163. fmt-merge-msg.c
  164. fmt-merge-msg.h
  165. fsck.c
  166. fsck.h
  167. fsmonitor--daemon.h
  168. fsmonitor-ipc.c
  169. fsmonitor-ipc.h
  170. fsmonitor-path-utils.h
  171. fsmonitor-settings.c
  172. fsmonitor-settings.h
  173. fsmonitor.c
  174. fsmonitor.h
  175. generate-cmdlist.sh
  176. generate-configlist.sh
  177. generate-hooklist.sh
  178. gettext.c
  179. gettext.h
  180. git-archimport.perl
  181. git-compat-util.h
  182. git-curl-compat.h
  183. git-cvsexportcommit.perl
  184. git-cvsimport.perl
  185. git-cvsserver.perl
  186. git-difftool--helper.sh
  187. git-filter-branch.sh
  188. git-instaweb.sh
  189. git-merge-octopus.sh
  190. git-merge-one-file.sh
  191. git-merge-resolve.sh
  192. git-mergetool--lib.sh
  193. git-mergetool.sh
  194. git-p4.py
  195. git-quiltimport.sh
  196. git-request-pull.sh
  197. git-send-email.perl
  198. git-sh-i18n.sh
  199. git-sh-setup.sh
  200. git-submodule.sh
  201. git-svn.perl
  202. GIT-VERSION-GEN
  203. git-web--browse.sh
  204. git-zlib.c
  205. git-zlib.h
  206. git.c
  207. git.rc
  208. gpg-interface.c
  209. gpg-interface.h
  210. graph.c
  211. graph.h
  212. grep.c
  213. grep.h
  214. hash-ll.h
  215. hash-lookup.c
  216. hash-lookup.h
  217. hash.h
  218. hashmap.c
  219. hashmap.h
  220. help.c
  221. help.h
  222. hex.c
  223. hex.h
  224. hook.c
  225. hook.h
  226. http-backend.c
  227. http-fetch.c
  228. http-push.c
  229. http-walker.c
  230. http.c
  231. http.h
  232. ident.c
  233. ident.h
  234. imap-send.c
  235. INSTALL
  236. iterator.h
  237. json-writer.c
  238. json-writer.h
  239. khash.h
  240. kwset.c
  241. kwset.h
  242. levenshtein.c
  243. levenshtein.h
  244. LGPL-2.1
  245. line-log.c
  246. line-log.h
  247. line-range.c
  248. line-range.h
  249. linear-assignment.c
  250. linear-assignment.h
  251. list-objects-filter-options.c
  252. list-objects-filter-options.h
  253. list-objects-filter.c
  254. list-objects-filter.h
  255. list-objects.c
  256. list-objects.h
  257. list.h
  258. ll-merge.c
  259. ll-merge.h
  260. lockfile.c
  261. lockfile.h
  262. log-tree.c
  263. log-tree.h
  264. ls-refs.c
  265. ls-refs.h
  266. mailinfo.c
  267. mailinfo.h
  268. mailmap.c
  269. mailmap.h
  270. Makefile
  271. match-trees.c
  272. match-trees.h
  273. mem-pool.c
  274. mem-pool.h
  275. merge-blobs.c
  276. merge-blobs.h
  277. merge-ort-wrappers.c
  278. merge-ort-wrappers.h
  279. merge-ort.c
  280. merge-ort.h
  281. merge-recursive.c
  282. merge-recursive.h
  283. merge.c
  284. mergesort.h
  285. midx.c
  286. midx.h
  287. name-hash.c
  288. notes-cache.c
  289. notes-cache.h
  290. notes-merge.c
  291. notes-merge.h
  292. notes-utils.c
  293. notes-utils.h
  294. notes.c
  295. notes.h
  296. object-file.c
  297. object-file.h
  298. object-name.c
  299. object-name.h
  300. object-store.h
  301. object.c
  302. object.h
  303. oid-array.c
  304. oid-array.h
  305. oidmap.c
  306. oidmap.h
  307. oidset.c
  308. oidset.h
  309. oidtree.c
  310. oidtree.h
  311. pack-bitmap-write.c
  312. pack-bitmap.c
  313. pack-bitmap.h
  314. pack-check.c
  315. pack-mtimes.c
  316. pack-mtimes.h
  317. pack-objects.c
  318. pack-objects.h
  319. pack-revindex.c
  320. pack-revindex.h
  321. pack-write.c
  322. pack.h
  323. packfile.c
  324. packfile.h
  325. pager.c
  326. pager.h
  327. parallel-checkout.c
  328. parallel-checkout.h
  329. parse-options-cb.c
  330. parse-options.c
  331. parse-options.h
  332. patch-delta.c
  333. patch-ids.c
  334. patch-ids.h
  335. path.c
  336. path.h
  337. pathspec.c
  338. pathspec.h
  339. pkt-line.c
  340. pkt-line.h
  341. preload-index.c
  342. pretty.c
  343. pretty.h
  344. prio-queue.c
  345. prio-queue.h
  346. progress.c
  347. progress.h
  348. promisor-remote.c
  349. promisor-remote.h
  350. prompt.c
  351. prompt.h
  352. protocol-caps.c
  353. protocol-caps.h
  354. protocol.c
  355. protocol.h
  356. prune-packed.c
  357. prune-packed.h
  358. quote.c
  359. quote.h
  360. range-diff.c
  361. range-diff.h
  362. reachable.c
  363. reachable.h
  364. read-cache.c
  365. README.md
  366. rebase-interactive.c
  367. rebase-interactive.h
  368. rebase.c
  369. rebase.h
  370. ref-filter.c
  371. ref-filter.h
  372. reflog-walk.c
  373. reflog-walk.h
  374. reflog.c
  375. reflog.h
  376. refs.c
  377. refs.h
  378. refspec.c
  379. refspec.h
  380. remote-curl.c
  381. remote.c
  382. remote.h
  383. replace-object.c
  384. replace-object.h
  385. repo-settings.c
  386. repository.c
  387. repository.h
  388. rerere.c
  389. rerere.h
  390. reset.c
  391. reset.h
  392. resolve-undo.c
  393. resolve-undo.h
  394. revision.c
  395. revision.h
  396. run-command.c
  397. run-command.h
  398. scalar.c
  399. SECURITY.md
  400. send-pack.c
  401. send-pack.h
  402. sequencer.c
  403. sequencer.h
  404. serve.c
  405. serve.h
  406. server-info.c
  407. server-info.h
  408. setup.c
  409. setup.h
  410. sh-i18n--envsubst.c
  411. sha1dc_git.c
  412. sha1dc_git.h
  413. shallow.c
  414. shallow.h
  415. shared.mak
  416. shell.c
  417. shortlog.h
  418. sideband.c
  419. sideband.h
  420. sigchain.c
  421. sigchain.h
  422. simple-ipc.h
  423. sparse-index.c
  424. sparse-index.h
  425. split-index.c
  426. split-index.h
  427. stable-qsort.c
  428. statinfo.h
  429. strbuf.c
  430. strbuf.h
  431. streaming.c
  432. streaming.h
  433. string-list.c
  434. string-list.h
  435. strmap.c
  436. strmap.h
  437. strvec.c
  438. strvec.h
  439. sub-process.c
  440. sub-process.h
  441. submodule-config.c
  442. submodule-config.h
  443. submodule.c
  444. submodule.h
  445. symlinks.c
  446. symlinks.h
  447. tag.c
  448. tag.h
  449. tar.h
  450. tempfile.c
  451. tempfile.h
  452. thread-utils.c
  453. thread-utils.h
  454. tmp-objdir.c
  455. tmp-objdir.h
  456. trace.c
  457. trace.h
  458. trace2.c
  459. trace2.h
  460. trailer.c
  461. trailer.h
  462. transport-helper.c
  463. transport-internal.h
  464. transport.c
  465. transport.h
  466. tree-diff.c
  467. tree-walk.c
  468. tree-walk.h
  469. tree.c
  470. tree.h
  471. unicode-width.h
  472. unimplemented.sh
  473. unix-socket.c
  474. unix-socket.h
  475. unix-stream-server.c
  476. unix-stream-server.h
  477. unpack-trees.c
  478. unpack-trees.h
  479. upload-pack.c
  480. upload-pack.h
  481. url.c
  482. url.h
  483. urlmatch.c
  484. urlmatch.h
  485. usage.c
  486. userdiff.c
  487. userdiff.h
  488. utf8.c
  489. utf8.h
  490. varint.c
  491. varint.h
  492. version.c
  493. version.h
  494. versioncmp.c
  495. versioncmp.h
  496. walker.c
  497. walker.h
  498. wildmatch.c
  499. wildmatch.h
  500. worktree.c
  501. worktree.h
  502. wrap-for-bin.sh
  503. wrapper.c
  504. wrapper.h
  505. write-or-die.c
  506. write-or-die.h
  507. ws.c
  508. ws.h
  509. wt-status.c
  510. wt-status.h
  511. xdiff-interface.c
  512. xdiff-interface.h
README.md

Build status

Git - fast, scalable, distributed revision control system

Git is a fast, scalable, distributed revision control system with an unusually rich command set that provides both high-level operations and full access to internals.

Git is an Open Source project covered by the GNU General Public License version 2 (some parts of it are under different licenses, compatible with the GPLv2). It was originally written by Linus Torvalds with help of a group of hackers around the net.

Please read the file INSTALL for installation instructions.

Many Git online resources are accessible from https://git-scm.com/ including full documentation and Git related tools.

See Documentation/gittutorial.txt to get started, then see Documentation/giteveryday.txt for a useful minimum set of commands, and Documentation/git-<commandname>.txt for documentation of each command. If git has been correctly installed, then the tutorial can also be read with man gittutorial or git help tutorial, and the documentation of each command with man git-<commandname> or git help <commandname>.

CVS users may also want to read Documentation/gitcvs-migration.txt (man gitcvs-migration or git help cvs-migration if git is installed).

The user discussion and development of Git take place on the Git mailing list -- everyone is welcome to post bug reports, feature requests, comments and patches to git@vger.kernel.org (read Documentation/SubmittingPatches for instructions on patch submission and Documentation/CodingGuidelines).

Those wishing to help with error message, usage and informational message string translations (localization l10) should see po/README.md (a po file is a Portable Object file that holds the translations).

To subscribe to the list, send an email with just “subscribe git” in the body to majordomo@vger.kernel.org (not the Git list). The mailing list archives are available at https://lore.kernel.org/git/, http://marc.info/?l=git and other archival sites.

Issues which are security relevant should be disclosed privately to the Git Security mailing list git-security@googlegroups.com.

The maintainer frequently sends the “What's cooking” reports that list the current status of various development topics to the mailing list. The discussion following them give a good reference for project status, development direction and remaining tasks.

The name “git” was given by Linus Torvalds when he wrote the very first version. He described the tool as “the stupid content tracker” and the name as (depending on your mood):

  • random three-letter combination that is pronounceable, and not actually used by any common UNIX command. The fact that it is a mispronunciation of “get” may or may not be relevant.
  • stupid. contemptible and despicable. simple. Take your pick from the dictionary of slang.
  • “global information tracker”: you're in a good mood, and it actually works for you. Angels sing, and a light suddenly fills the room.
  • “goddamn idiotic truckload of sh*t”: when it breaks