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

[PATCH v4 5/6] test-terminal: introduce --no-stdin-pty

From
Rubén Justo <rjusto@gmail.com>
Date
Jun 3, 2024, 20:38 UTC
Message-ID
<d95180fc-8f8a-4e1d-987d-3aa0811be7de@gmail.com>
In-Reply-To
<1ef0ac3a-3be5-4fc2-93f8-46610f3d1880@gmail.com>

In 18d8c26930 (test_terminal: redirect child process' stdin to a pty, 2015-08-04), t/test-terminal.perl learned to connect the child process' stdin to a pty. It works well for what was intended: satisfying an `isatty(STDIN_FILENO)` check.

However, the fork introduced, that copies the stdin to the child process, does not always manage to send all the information.

To illustrate this behaviour, we can use a function like this:
    f ()
    {
    	dd if=/dev/zero bs=1 count=10000 status=none |
    	t/test-terminal.perl cat - 2>/dev/null |
    	wc -c;
    }

We do not obtain the expected results when executing this function 100 times:

    $ for i in $(seq 100); do f; done | sort | uniq -c
         36 0
          4 1
         53 4095
          7 4159

If we do the same with a version that does not redirect stdin, a version prior to 18d8c26930, the expected result is obtained:

    $ git checkout 18d8c26930~1
    $ for i in $(seq 100); do f; done | sort | uniq -c
        100 10000

In a subsequent commit, a new test is going to rely on test-terminate, and it does not require stdin to be connected to a terminal, but all piped data needs to be successfully transmitted to the child process.

To make this possible, add a new parameter "--no-stdin-pty" to allow disabling the stdin redirection though a pty.

Signed-off-by: Rubén Justo <rjusto@gmail.com>
---
 t/test-terminal.perl | 32 ++++++++++++++++++--------------
 1 file changed, 18 insertions(+), 14 deletions(-)
diff --git a/t/test-terminal.perl b/t/test-terminal.perl
index 3810e9bb43..85edc9e8b9 100755
--- a/t/test-terminal.perl
+++ b/t/test-terminal.perl
@@ -12,10 +12,10 @@ sub start_child {
 	if (not defined $pid) {
 		die "fork failed: $!"
 	} elsif ($pid == 0) {
-		open STDIN, "<&", $in;
+		open STDIN, "<&", $in if $in;
 		open STDOUT, ">&", $out;
 		open STDERR, ">&", $err;
-		close $in;
+		close $in if $in;
 		close $out;
 		exec(@$argv) or die "cannot exec '$argv->[0]': $!"
 	}
@@ -78,28 +78,32 @@ sub copy_stdio {
 }
 
 if ($#ARGV < 1) {
-	die "usage: test-terminal program args";
+	die "usage: test-terminal [--no-stdin-pty] program args";
 }
+my $no_stdin_pty = $ARGV[0] eq '--no-stdin-pty';
+shift @ARGV if $no_stdin_pty;
 $ENV{TERM} = 'vt100';
-my $parent_in = new IO::Pty;
+my $parent_in = $no_stdin_pty ? undef : IO::Pty->new;
 my $parent_out = new IO::Pty;
 my $parent_err = new IO::Pty;
-$parent_in->set_raw();
+$parent_in->set_raw() if $parent_in;
 $parent_out->set_raw();
 $parent_err->set_raw();
-$parent_in->slave->set_raw();
+$parent_in->slave->set_raw() if $parent_in;
 $parent_out->slave->set_raw();
 $parent_err->slave->set_raw();
-my $pid = start_child(\@ARGV, $parent_in->slave, $parent_out->slave, $parent_err->slave);
-close $parent_in->slave;
+my $pid = start_child(\@ARGV,$parent_in ? $parent_in->slave : undef, $parent_out->slave, $parent_err->slave);
+close $parent_in->slave if $parent_in;
 close $parent_out->slave;
 close $parent_err->slave;
-my $in_pid = copy_stdin($parent_in);
+my $in_pid = $no_stdin_pty ? 0 : copy_stdin($parent_in);
 copy_stdio($parent_out, $parent_err);
 my $ret = finish_child($pid);
-# If the child process terminates before our copy_stdin() process is able to
-# write all of its data to $parent_in, the copy_stdin() process could stall.
-# Send SIGTERM to it to ensure it terminates.
-kill 'TERM', $in_pid;
-finish_child($in_pid);
+if ($in_pid) {
+	# If the child process terminates before our copy_stdin() process is able to
+	# write all of its data to $parent_in, the copy_stdin() process could stall.
+	# Send SIGTERM to it to ensure it terminates.
+	kill 'TERM', $in_pid;
+	finish_child($in_pid);
+}
 exit($ret);
-- 
2.45.0.97.gb691764a17
Previous: Junio C HamanoNext: Phillip Wood
Message 80 of 113 in “use the pager in 'add -p'”
  1. 0/5 use the pager in 'add -p'Rubén Justo, May 19, 2024
  2. 1/5 add-patch: test for 'p' commandRubén Justo, May 19, 2024
  3. 2/5 pager: do not close fd 2 unnecessarilyRubén Justo, May 19, 2024
  4. Junio C HamanoMay 20, 2024
  5. Rubén JustoMay 20, 2024
  6. Junio C HamanoMay 21, 2024
  7. Rubén JustoMay 21, 2024
  8. Junio C HamanoMay 21, 2024
  9. Rubén JustoMay 22, 2024
  10. Junio C HamanoMay 22, 2024
  11. Rubén JustoMay 26, 2024
  12. Junio C HamanoMay 26, 2024
  13. 3/5 pager: introduce wait_for_pagerRubén Justo, May 19, 2024
  14. 4/5 test-terminal: introduce --no-stdin-ptyRubén Justo, May 19, 2024
  15. 5/5 add-patch: render hunks through the pagerRubén Justo, May 19, 2024
  16. Junio C HamanoMay 20, 2024
  17. Dragan SimicMay 20, 2024
  18. Rubén JustoMay 20, 2024
  19. Dragan SimicMay 20, 2024
  20. Rubén JustoMay 21, 2024
  21. Jeff KingMay 21, 2024
  22. Rubén JustoMay 21, 2024
  23. Jeff KingMay 23, 2024
  24. Junio C HamanoMay 23, 2024
  25. Dragan SimicMay 23, 2024
  26. Junio C HamanoMay 23, 2024
  27. Dragan SimicMay 23, 2024
  28. Dragan SimicMay 23, 2024
  29. Junio C HamanoMay 23, 2024
  30. Dragan SimicMay 23, 2024
  31. Jeff KingMay 25, 2024
  32. Rubén JustoMay 23, 2024
  33. Dragan SimicMay 23, 2024
  34. Rubén JustoMay 20, 2024
  35. Junio C HamanoMay 20, 2024
  36. Rubén JustoMay 20, 2024
  37. 0/5 use the pager in 'add -p'Rubén Justo, May 21, 2024
  38. 1/5 add-patch: test for 'p' commandRubén Justo, May 21, 2024
  39. 2/5 pager: do not close fd 2 unnecessarilyRubén Justo, May 21, 2024
  40. 3/5 pager: introduce wait_for_pagerRubén Justo, May 21, 2024
  41. 4/5 test-terminal: introduce --no-stdin-ptyRubén Justo, May 21, 2024
  42. 5/5 add-patch: render hunks through the pagerRubén Justo, May 21, 2024
  43. Dragan SimicMay 22, 2024
  44. Junio C HamanoMay 22, 2024
  45. Rubén JustoMay 22, 2024
  46. Dragan SimicMay 22, 2024
  47. 0/6 use the pager in 'add -p'Rubén Justo, Jun 2, 2024
  48. 1/6 add-patch: test for 'p' commandRubén Justo, Jun 2, 2024
  49. 2/6 pager: do not close fd 2 unnecessarilyRubén Justo, Jun 2, 2024
  50. 3/6 pager: introduce wait_for_pagerRubén Justo, Jun 2, 2024
  51. 4/6 pager: introduce setup_custom_pagerRubén Justo, Jun 2, 2024
  52. 5/6 test-terminal: introduce --no-stdin-ptyRubén Justo, Jun 2, 2024
  53. 6/6 add-patch: introduce the command '|'Rubén Justo, Jun 2, 2024
  54. Junio C HamanoJun 2, 2024
  55. Junio C HamanoJun 2, 2024
  56. Rubén JustoJun 2, 2024
  57. Rubén JustoJun 2, 2024
  58. Dragan SimicJun 2, 2024
  59. Junio C HamanoJun 3, 2024
  60. Dragan SimicJun 3, 2024
  61. Junio C HamanoJun 3, 2024
  62. Dragan SimicJun 4, 2024
  63. Dragan SimicJun 2, 2024
  64. Junio C HamanoJun 3, 2024
  65. Dragan SimicJun 4, 2024
  66. Dragan SimicJun 4, 2024
  67. Rubén JustoJun 3, 2024
  68. Dragan SimicJun 4, 2024
  69. 0/6 use the pager in 'add -p'Rubén Justo, Jun 3, 2024
  70. 1/6 add-patch: test for 'p' commandRubén Justo, Jun 3, 2024
  71. 2/6 pager: do not close fd 2 unnecessarilyRubén Justo, Jun 3, 2024
  72. Junio C HamanoJun 4, 2024
  73. 3/6 pager: introduce wait_for_pagerRubén Justo, Jun 3, 2024
  74. Phillip WoodJun 4, 2024
  75. Junio C HamanoJun 4, 2024
  76. Rubén JustoJun 5, 2024
  77. Junio C HamanoJun 4, 2024
  78. 4/6 pager: introduce setup_custom_pagerRubén Justo, Jun 3, 2024
  79. Junio C HamanoJun 4, 2024
  80. 5/6 test-terminal: introduce --no-stdin-ptyRubén Justo, Jun 3, 2024
  81. Phillip WoodJun 4, 2024
  82. Jeff KingJun 4, 2024
  83. Junio C HamanoJun 5, 2024
  84. Jeff KingJun 6, 2024
  85. Rubén JustoJun 5, 2024
  86. Jeff KingJun 6, 2024
  87. Rubén JustoJun 9, 2024
  88. 6/6 add-patch: introduce the command '|'Rubén Justo, Jun 3, 2024
  89. Junio C HamanoJun 4, 2024
  90. Dragan SimicJun 4, 2024
  91. Junio C HamanoJun 5, 2024
  92. Jeff KingJun 4, 2024
  93. Junio C HamanoJun 4, 2024
  94. Jeff KingJun 5, 2024
  95. Phillip WoodJun 5, 2024
  96. Dragan SimicJun 8, 2024
  97. Rubén JustoJun 9, 2024
  98. Dragan SimicJun 9, 2024
  99. Rubén JustoJun 10, 2024
  100. Dragan SimicJun 10, 2024
  101. Phillip WoodJun 10, 2024
  102. Junio C HamanoJun 10, 2024
  103. Rubén JustoJun 10, 2024
  104. Junio C HamanoJun 10, 2024
  105. Dragan SimicJun 10, 2024
  106. Dragan SimicJun 10, 2024
  107. Junio C HamanoJun 10, 2024
  108. Dragan SimicJun 10, 2024
  109. phillip.wood123@gmail.comJun 9, 2024
  110. Dragan SimicJun 9, 2024
  111. Phillip WoodJun 10, 2024
  112. Dragan SimicJun 10, 2024
  113. Junio C HamanoJun 5, 2024

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

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