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

[PATCH v3 03/12] pkt-line: (optionally) libify the packet readers

From
Johannes Schindelin via GitGitGadget <gitgitgadget@gmail.com>
Date
Feb 13, 2021, 00:09 UTC
Message-ID
<e05467def4e158a5f1cfa3aafffdb5c77097859a.1613174954.git.gitgitgadget@gmail.com>
In-Reply-To
<pull.766.v3.git.1613174954.gitgitgadget@gmail.com>
From: Johannes Schindelin <johannes.schindelin@gmx.de>

So far, the (possibly indirect) callers of `get_packet_data()` can ask that function to return an error instead of `die()`ing upon end-of-file. However, random read errors will still cause the process to die.

So let's introduce an explicit option to tell the packet reader machinery to please be nice and only return an error.

This change prepares pkt-line for use by long-running daemon processes. Such processes should be able to serve multiple concurrent clients and and survive random IO errors. If there is an error on one connection, a daemon should be able to drop that connection and continue serving existing and future connections.

This ability will be used by a Git-aware "Internal FSMonitor" feature in a later patch series.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
---
 pkt-line.c | 19 +++++++++++++++++--
 pkt-line.h |  4 ++++
 2 files changed, 21 insertions(+), 2 deletions(-)
diff --git a/pkt-line.c b/pkt-line.c
index 3602b0d37092..83c46e6b46ee 100644
--- a/pkt-line.c
+++ b/pkt-line.c
@@ -304,8 +304,11 @@ static int get_packet_data(int fd, char **src_buf, size_t *src_size,
 		*src_size -= ret;
 	} else {
 		ret = read_in_full(fd, dst, size);
-		if (ret < 0)
+		if (ret < 0) {
+			if (options & PACKET_READ_NEVER_DIE)
+				return error_errno(_("read error"));
 			die_errno(_("read error"));
+		}
 	}
 
 	/* And complain if we didn't get enough bytes to satisfy the read. */
@@ -313,6 +316,8 @@ static int get_packet_data(int fd, char **src_buf, size_t *src_size,
 		if (options & PACKET_READ_GENTLE_ON_EOF)
 			return -1;
 
+		if (options & PACKET_READ_NEVER_DIE)
+			return error(_("the remote end hung up unexpectedly"));
 		die(_("the remote end hung up unexpectedly"));
 	}
 
@@ -341,6 +346,9 @@ enum packet_read_status packet_read_with_status(int fd, char **src_buffer,
 	len = packet_length(linelen);
 
 	if (len < 0) {
+		if (options & PACKET_READ_NEVER_DIE)
+			return error(_("protocol error: bad line length "
+				       "character: %.4s"), linelen);
 		die(_("protocol error: bad line length character: %.4s"), linelen);
 	} else if (!len) {
 		packet_trace("0000", 4, 0);
@@ -355,12 +363,19 @@ enum packet_read_status packet_read_with_status(int fd, char **src_buffer,
 		*pktlen = 0;
 		return PACKET_READ_RESPONSE_END;
 	} else if (len < 4) {
+		if (options & PACKET_READ_NEVER_DIE)
+			return error(_("protocol error: bad line length %d"),
+				     len);
 		die(_("protocol error: bad line length %d"), len);
 	}
 
 	len -= 4;
-	if ((unsigned)len >= size)
+	if ((unsigned)len >= size) {
+		if (options & PACKET_READ_NEVER_DIE)
+			return error(_("protocol error: bad line length %d"),
+				     len);
 		die(_("protocol error: bad line length %d"), len);
+	}
 
 	if (get_packet_data(fd, src_buffer, src_len, buffer, len, options) < 0) {
 		*pktlen = -1;
diff --git a/pkt-line.h b/pkt-line.h
index a7149429ac35..2e472efaf2c5 100644
--- a/pkt-line.h
+++ b/pkt-line.h
@@ -75,10 +75,14 @@ int write_packetized_from_buf_no_flush(const char *src_in, size_t len, int fd_ou
  *
  * If options contains PACKET_READ_DIE_ON_ERR_PACKET, it dies when it sees an
  * ERR packet.
+ *
+ * With `PACKET_READ_NEVER_DIE`, no errors are allowed to trigger die() (except
+ * an ERR packet, when `PACKET_READ_DIE_ON_ERR_PACKET` is in effect).
  */
 #define PACKET_READ_GENTLE_ON_EOF     (1u<<0)
 #define PACKET_READ_CHOMP_NEWLINE     (1u<<1)
 #define PACKET_READ_DIE_ON_ERR_PACKET (1u<<2)
+#define PACKET_READ_NEVER_DIE         (1u<<3)
 int packet_read(int fd, char **src_buffer, size_t *src_len, char
 		*buffer, unsigned size, int options);
 
-- 
gitgitgadget
Previous: Johannes Schindelin via GitGitGadgetNext: Johannes Schindelin via GitGitGadget
Message 71 of 178 in “[RFC] Simple IPC Mechanism”
  1. 00/10 [RFC] Simple IPC MechanismJeff Hostetler via GitGitGadget, Jan 12, 2021
  2. 01/10 pkt-line: use stack rather than static buffer in packet_write_gently()Jeff Hostetler via GitGitGadget, Jan 12, 2021
  3. Jeff KingJan 13, 2021
  4. Jeff HostetlerJan 25, 2021
  5. 04/10 pkt-line: accept additional options in read_packetized_to_strbuf()Johannes Schindelin via GitGitGadget, Jan 12, 2021
  6. 02/10 pkt-line: (optionally) libify the packet readersJohannes Schindelin via GitGitGadget, Jan 12, 2021
  7. 05/10 simple-ipc: design documentation for new IPC mechanismJeff Hostetler via GitGitGadget, Jan 12, 2021
  8. Ævar Arnfjörð BjarmasonJan 12, 2021
  9. 03/10 pkt-line: optionally skip the flush packet in write_packetized_from_buf()Johannes Schindelin via GitGitGadget, Jan 12, 2021
  10. 10/10 simple-ipc: add Unix domain socket implementationJeff Hostetler via GitGitGadget, Jan 12, 2021
  11. 08/10 unix-socket: add no-chdir option to unix_stream_listen_gently()Jeff Hostetler via GitGitGadget, Jan 12, 2021
  12. 09/10 simple-ipc: add t/helper/test-simple-ipc and t0052Jeff Hostetler via GitGitGadget, Jan 12, 2021
  13. 07/10 unix-socket: create gentle version of unix_stream_listen()Jeff Hostetler via GitGitGadget, Jan 12, 2021
  14. Jeff KingJan 13, 2021
  15. Chris TorekJan 14, 2021
  16. 06/10 simple-ipc: add win32 implementationJeff Hostetler via GitGitGadget, Jan 12, 2021
  17. Ævar Arnfjörð BjarmasonJan 12, 2021
  18. Jeff HostetlerJan 12, 2021
  19. Junio C HamanoJan 12, 2021
  20. Jeff HostetlerJan 12, 2021
  21. Junio C HamanoJan 13, 2021
  22. Jeff HostetlerJan 13, 2021
  23. Jeff KingJan 13, 2021
  24. Ævar Arnfjörð BjarmasonJan 13, 2021
  25. 00/14 Simple IPC MechanismJeff Hostetler via GitGitGadget, Feb 1, 2021
  26. 01/14 ci/install-depends: attempt to fix "brew cask" stuffJunio C Hamano via GitGitGadget, Feb 1, 2021
  27. 04/14 pkt-line: optionally skip the flush packet in write_packetized_from_buf()Johannes Schindelin via GitGitGadget, Feb 1, 2021
  28. Jeff KingFeb 2, 2021
  29. Johannes SchindelinFeb 2, 2021
  30. Jeff HostetlerFeb 5, 2021
  31. 05/14 pkt-line: (optionally) libify the packet readersJohannes Schindelin via GitGitGadget, Feb 1, 2021
  32. 06/14 pkt-line: accept additional options in read_packetized_to_strbuf()Johannes Schindelin via GitGitGadget, Feb 1, 2021
  33. Taylor BlauFeb 11, 2021
  34. 13/14 unix-socket: do not call die in unix_stream_connect()Jeff Hostetler via GitGitGadget, Feb 1, 2021
  35. 11/14 unix-socket: add options to unix_stream_listen()Jeff Hostetler via GitGitGadget, Feb 1, 2021
  36. Jeff KingFeb 2, 2021
  37. Jeff HostetlerFeb 5, 2021
  38. Jeff KingFeb 9, 2021
  39. Jeff HostetlerFeb 9, 2021
  40. Jeff KingFeb 10, 2021
  41. Jeff HostetlerFeb 10, 2021
  42. 07/14 simple-ipc: design documentation for new IPC mechanismJeff Hostetler via GitGitGadget, Feb 1, 2021
  43. 14/14 simple-ipc: add Unix domain socket implementationJeff Hostetler via GitGitGadget, Feb 1, 2021
  44. 08/14 simple-ipc: add win32 implementationJeff Hostetler via GitGitGadget, Feb 1, 2021
  45. 10/14 unix-socket: elimiate static unix_stream_socket() helper functionJeff Hostetler via GitGitGadget, Feb 1, 2021
  46. Jeff KingFeb 2, 2021
  47. Jeff KingFeb 2, 2021
  48. 09/14 simple-ipc: add t/helper/test-simple-ipc and t0052Jeff Hostetler via GitGitGadget, Feb 1, 2021
  49. SZEDER GáborFeb 2, 2021
  50. Jeff KingFeb 3, 2021
  51. Jeff HostetlerFeb 9, 2021
  52. SZEDER GáborFeb 5, 2021
  53. 12/14 unix-socket: add no-chdir option to unix_stream_listen()Jeff Hostetler via GitGitGadget, Feb 1, 2021
  54. Jeff KingFeb 2, 2021
  55. 03/14 pkt-line: add write_packetized_from_buf2() that takes scratch bufferJeff Hostetler via GitGitGadget, Feb 1, 2021
  56. Jeff KingFeb 2, 2021
  57. 02/14 pkt-line: promote static buffer in packet_write_gently() to callersJeff Hostetler via GitGitGadget, Feb 1, 2021
  58. Jeff KingFeb 2, 2021
  59. Jeff HostetlerFeb 2, 2021
  60. Johannes SchindelinFeb 2, 2021
  61. Jeff KingFeb 3, 2021
  62. Junio C HamanoFeb 1, 2021
  63. Jeff HostetlerFeb 1, 2021
  64. Johannes SchindelinFeb 2, 2021
  65. Junio C HamanoFeb 4, 2021
  66. candidate branches for `maint`, was Re: [PATCH v2 00/14] Simple IPC MechanismJohannes Schindelin, Feb 5, 2021
  67. Junio C HamanoFeb 5, 2021
  68. 00/12 Simple IPC MechanismJeff Hostetler via GitGitGadget, Feb 13, 2021
  69. 01/12 pkt-line: eliminate the need for static buffer in packet_write_gently()Jeff Hostetler via GitGitGadget, Feb 13, 2021
  70. 02/12 pkt-line: do not issue flush packets in write_packetized_*()Johannes Schindelin via GitGitGadget, Feb 13, 2021
  71. 03/12 pkt-line: (optionally) libify the packet readersJohannes Schindelin via GitGitGadget, Feb 13, 2021
  72. 04/12 pkt-line: add options argument to read_packetized_to_strbuf()Johannes Schindelin via GitGitGadget, Feb 13, 2021
  73. 05/12 simple-ipc: design documentation for new IPC mechanismJeff Hostetler via GitGitGadget, Feb 13, 2021
  74. 06/12 simple-ipc: add win32 implementationJeff Hostetler via GitGitGadget, Feb 13, 2021
  75. 07/12 unix-socket: elimiate static unix_stream_socket() helper functionJeff Hostetler via GitGitGadget, Feb 13, 2021
  76. 08/12 unix-socket: add backlog size option to unix_stream_listen()Jeff Hostetler via GitGitGadget, Feb 13, 2021
  77. 10/12 unix-socket: create `unix_stream_server__listen_with_lock()`Jeff Hostetler via GitGitGadget, Feb 13, 2021
  78. 09/12 unix-socket: disallow chdir() when creating unix domain socketsJeff Hostetler via GitGitGadget, Feb 13, 2021
  79. 12/12 t0052: add simple-ipc tests and t/helper/test-simple-ipc toolJeff Hostetler via GitGitGadget, Feb 13, 2021
  80. SZEDER GáborFeb 13, 2021
  81. Jeff HostetlerFeb 16, 2021
  82. 11/12 simple-ipc: add Unix domain socket implementationJeff Hostetler via GitGitGadget, Feb 13, 2021
  83. 00/12 Simple IPC MechanismJeff Hostetler via GitGitGadget, Feb 17, 2021
  84. 01/12 pkt-line: eliminate the need for static buffer in packet_write_gently()Jeff Hostetler via GitGitGadget, Feb 17, 2021
  85. Jeff KingFeb 26, 2021
  86. Jeff HostetlerFeb 26, 2021
  87. Jeff KingFeb 26, 2021
  88. Junio C HamanoMar 3, 2021
  89. Jeff HostetlerMar 4, 2021
  90. Junio C HamanoMar 4, 2021
  91. 02/12 pkt-line: do not issue flush packets in write_packetized_*()Johannes Schindelin via GitGitGadget, Feb 17, 2021
  92. 03/12 pkt-line: (optionally) libify the packet readersJohannes Schindelin via GitGitGadget, Feb 17, 2021
  93. Junio C HamanoMar 3, 2021
  94. Jeff HostetlerMar 4, 2021
  95. Jeff KingMar 4, 2021
  96. Junio C HamanoMar 4, 2021
  97. 04/12 pkt-line: add options argument to read_packetized_to_strbuf()Johannes Schindelin via GitGitGadget, Feb 17, 2021
  98. 05/12 simple-ipc: design documentation for new IPC mechanismJeff Hostetler via GitGitGadget, Feb 17, 2021
  99. Junio C HamanoMar 3, 2021
  100. 06/12 simple-ipc: add win32 implementationJeff Hostetler via GitGitGadget, Feb 17, 2021
  101. 07/12 unix-socket: elimiate static unix_stream_socket() helper functionJeff Hostetler via GitGitGadget, Feb 17, 2021
  102. Jeff KingFeb 26, 2021
  103. Junio C HamanoMar 3, 2021
  104. 08/12 unix-socket: add backlog size option to unix_stream_listen()Jeff Hostetler via GitGitGadget, Feb 17, 2021
  105. Jeff KingFeb 26, 2021
  106. Junio C HamanoMar 3, 2021
  107. 09/12 unix-socket: disallow chdir() when creating unix domain socketsJeff Hostetler via GitGitGadget, Feb 17, 2021
  108. Junio C HamanoMar 3, 2021
  109. Jeff KingMar 4, 2021
  110. Junio C HamanoMar 4, 2021
  111. Junio C HamanoMar 4, 2021
  112. Jeff KingMar 5, 2021
  113. Jeff KingMar 5, 2021
  114. Chris TorekMar 5, 2021
  115. Jeff HostetlerMar 5, 2021
  116. Junio C HamanoMar 5, 2021
  117. Jeff HostetlerMar 5, 2021
  118. Junio C HamanoMar 5, 2021
  119. 10/12 unix-socket: create `unix_stream_server__listen_with_lock()`Jeff Hostetler via GitGitGadget, Feb 17, 2021
  120. Jeff KingFeb 26, 2021
  121. Jeff HostetlerMar 2, 2021
  122. Jeff KingMar 4, 2021
  123. 12/12 t0052: add simple-ipc tests and t/helper/test-simple-ipc toolJeff Hostetler via GitGitGadget, Feb 17, 2021
  124. Jeff KingMar 2, 2021
  125. Jeff HostetlerMar 3, 2021
  126. 11/12 simple-ipc: add Unix domain socket implementationJeff Hostetler via GitGitGadget, Feb 17, 2021
  127. Junio C HamanoFeb 25, 2021
  128. Jeff KingFeb 26, 2021
  129. Jeff HostetlerFeb 26, 2021
  130. Jeff KingFeb 26, 2021
  131. Junio C HamanoMar 3, 2021
  132. 00/12 Simple IPC MechanismJeff Hostetler via GitGitGadget, Mar 9, 2021
  133. 01/12 pkt-line: eliminate the need for static buffer in packet_write_gently()Jeff Hostetler via GitGitGadget, Mar 9, 2021
  134. Junio C HamanoMar 9, 2021
  135. Jeff KingMar 11, 2021
  136. Junio C HamanoMar 11, 2021
  137. Jeff KingMar 11, 2021
  138. 05/12 simple-ipc: design documentation for new IPC mechanismJeff Hostetler via GitGitGadget, Mar 9, 2021
  139. 07/12 unix-socket: eliminate static unix_stream_socket() helper functionJeff Hostetler via GitGitGadget, Mar 9, 2021
  140. 06/12 simple-ipc: add win32 implementationJeff Hostetler via GitGitGadget, Mar 9, 2021
  141. 11/12 simple-ipc: add Unix domain socket implementationJeff Hostetler via GitGitGadget, Mar 9, 2021
  142. Junio C HamanoMar 10, 2021
  143. Jeff HostetlerMar 15, 2021
  144. 03/12 pkt-line: add PACKET_READ_GENTLE_ON_READ_ERROR optionJohannes Schindelin via GitGitGadget, Mar 9, 2021
  145. 02/12 pkt-line: do not issue flush packets in write_packetized_*()Johannes Schindelin via GitGitGadget, Mar 9, 2021
  146. 04/12 pkt-line: add options argument to read_packetized_to_strbuf()Johannes Schindelin via GitGitGadget, Mar 9, 2021
  147. 08/12 unix-socket: add backlog size option to unix_stream_listen()Jeff Hostetler via GitGitGadget, Mar 9, 2021
  148. 12/12 t0052: add simple-ipc tests and t/helper/test-simple-ipc toolJeff Hostetler via GitGitGadget, Mar 9, 2021
  149. 10/12 unix-stream-server: create unix domain socket under lockJeff Hostetler via GitGitGadget, Mar 9, 2021
  150. Junio C HamanoMar 10, 2021
  151. 09/12 unix-socket: disallow chdir() when creating unix domain socketsJeff Hostetler via GitGitGadget, Mar 9, 2021
  152. Junio C HamanoMar 9, 2021
  153. 00/12 Simple IPC MechanismJeff Hostetler via GitGitGadget, Mar 15, 2021
  154. 01/12 pkt-line: eliminate the need for static buffer in packet_write_gently()Jeff Hostetler via GitGitGadget, Mar 15, 2021
  155. 03/12 pkt-line: add PACKET_READ_GENTLE_ON_READ_ERROR optionJohannes Schindelin via GitGitGadget, Mar 15, 2021
  156. 02/12 pkt-line: do not issue flush packets in write_packetized_*()Johannes Schindelin via GitGitGadget, Mar 15, 2021
  157. 05/12 simple-ipc: design documentation for new IPC mechanismJeff Hostetler via GitGitGadget, Mar 15, 2021
  158. 08/12 unix-socket: add backlog size option to unix_stream_listen()Jeff Hostetler via GitGitGadget, Mar 15, 2021
  159. 04/12 pkt-line: add options argument to read_packetized_to_strbuf()Johannes Schindelin via GitGitGadget, Mar 15, 2021
  160. 10/12 unix-stream-server: create unix domain socket under lockJeff Hostetler via GitGitGadget, Mar 15, 2021
  161. 09/12 unix-socket: disallow chdir() when creating unix domain socketsJeff Hostetler via GitGitGadget, Mar 15, 2021
  162. 06/12 simple-ipc: add win32 implementationJeff Hostetler via GitGitGadget, Mar 15, 2021
  163. 07/12 unix-socket: eliminate static unix_stream_socket() helper functionJeff Hostetler via GitGitGadget, Mar 15, 2021
  164. 11/12 simple-ipc: add Unix domain socket implementationJeff Hostetler via GitGitGadget, Mar 15, 2021
  165. 12/12 t0052: add simple-ipc tests and t/helper/test-simple-ipc toolJeff Hostetler via GitGitGadget, Mar 15, 2021
  166. 00/12 Simple IPC MechanismJeff Hostetler via GitGitGadget, Mar 22, 2021
  167. 01/12 pkt-line: eliminate the need for static buffer in packet_write_gently()Jeff Hostetler via GitGitGadget, Mar 22, 2021
  168. 03/12 pkt-line: add PACKET_READ_GENTLE_ON_READ_ERROR optionJohannes Schindelin via GitGitGadget, Mar 22, 2021
  169. 02/12 pkt-line: do not issue flush packets in write_packetized_*()Johannes Schindelin via GitGitGadget, Mar 22, 2021
  170. 05/12 simple-ipc: design documentation for new IPC mechanismJeff Hostetler via GitGitGadget, Mar 22, 2021
  171. 04/12 pkt-line: add options argument to read_packetized_to_strbuf()Johannes Schindelin via GitGitGadget, Mar 22, 2021
  172. 08/12 unix-socket: add backlog size option to unix_stream_listen()Jeff Hostetler via GitGitGadget, Mar 22, 2021
  173. 09/12 unix-socket: disallow chdir() when creating unix domain socketsJeff Hostetler via GitGitGadget, Mar 22, 2021
  174. 07/12 unix-socket: eliminate static unix_stream_socket() helper functionJeff Hostetler via GitGitGadget, Mar 22, 2021
  175. 10/12 unix-stream-server: create unix domain socket under lockJeff Hostetler via GitGitGadget, Mar 22, 2021
  176. 12/12 t0052: add simple-ipc tests and t/helper/test-simple-ipc toolJeff Hostetler via GitGitGadget, Mar 22, 2021
  177. 11/12 simple-ipc: add Unix domain socket implementationJeff Hostetler via GitGitGadget, Mar 22, 2021
  178. 06/12 simple-ipc: add win32 implementationJeff Hostetler via GitGitGadget, Mar 22, 2021

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.