Skip to content

Commit 03f8c3e

Browse files
committed
rfc42: add subprocess attach RPC
Add an attach RPC to the subprocess server protocol, which allows a client to attach to an existing background subprocess. Key design decisions: - The attach request includes a flags parameter for future extensibility, but no flags are currently supported. Output forwarding behavior is inherited from the original subprocess. - An "exec attached" response is sent first to indicate success and communicate the subprocess flags to the client, followed by the same response types used for streaming exec requests. - If a waitable subprocess has already terminated, the full response sequence is still sent (attached->output EOFs->finished->ENODATA). - A successful attach that receives the finished response reaps the subprocess, preventing it from being waited on or attached to again. - If the client disconnects, the subprocess reverts to background mode rather than being terminated. Error conditions: - ENOENT: subprocess doesn't exist or has exited without waitable flag - EBUSY: subprocess is not in background mode - EPERM: client lacks authorization
1 parent 1dd7183 commit 03f8c3e

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed

spec_42.rst

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,97 @@ When initiated with a non-streaming request, an :program:`exec error`
261261
response SHALL NOT be sent if a failure occurs after a successful call to
262262
:func:`execvp`.
263263

264+
attach
265+
======
266+
267+
The :program:`attach` RPC attaches to an existing background subprocess.
268+
269+
.. object:: attach request
270+
271+
The request SHALL consist of a JSON object with the following keys:
272+
273+
.. object:: pid
274+
275+
(*integer*, REQUIRED) The process ID of the background subprocess.
276+
277+
.. object:: label
278+
279+
(*string*, OPTIONAL) The label of the background subprocess. If this key
280+
is present, the value of ``pid`` SHALL be ignored and the subprocess
281+
SHALL be identified by its label.
282+
283+
.. object:: flags
284+
285+
(*integer*, REQUIRED) A bitfield comprised of zero or more flags.
286+
287+
No flags are currently supported. The flags value SHALL be ignored
288+
and output forwarding behavior SHALL be inherited from the original
289+
subprocess.
290+
291+
.. note::
292+
293+
Output produced by the subprocess before the attach request MAY be lost
294+
unless the subprocess server has buffered it or logged it to its own
295+
log stream.
296+
297+
The :program:`attach` response stream SHALL follow the same format as
298+
streaming :program:`exec` responses. The server SHALL send:
299+
300+
- :program:`exec attached` response to indicate successful attachment
301+
- :program:`exec output` responses for any buffered output and subsequent
302+
output matching the target subprocess flags
303+
- :program:`exec stopped` response if the subprocess is stopped
304+
- :program:`exec finished` response when the subprocess terminates
305+
- :program:`exec error` response with ENODATA (61) to indicate successful
306+
stream termination
307+
308+
.. object:: exec attached response
309+
310+
The client has successfully attached to the subprocess.
311+
312+
The response SHALL consist of a JSON object with the following keys:
313+
314+
.. object:: type
315+
316+
(*string*, REQUIRED) The response type with a value of ``attached``.
317+
318+
.. object:: pid
319+
320+
(*integer*, REQUIRED) The process ID of the subprocess.
321+
322+
.. object:: flags
323+
324+
(*integer*, REQUIRED) The flags that the subprocess was originally
325+
started with. This allows the client to determine which output streams
326+
and responses to expect.
327+
328+
329+
If the subprocess was started with the waitable flag and has already
330+
terminated before the attach request is processed, the server SHALL send an
331+
:program:`exec attached` response, followed by :program:`exec output` responses
332+
with the EOF flag set for each output stream that was being forwarded,
333+
followed by the :program:`exec finished` response, and finally ENODATA to
334+
terminate the stream.
335+
336+
.. note::
337+
338+
A waitable background subprocess that is successfully attached to and
339+
receives the :program:`exec finished` response SHALL be considered reaped
340+
and cannot be waited on or attached to again.
341+
342+
If the requesting process disconnects, the subprocess SHALL revert to
343+
background mode and continue running until it terminates or the server
344+
is shut down.
345+
346+
.. object:: attach error response
347+
348+
The :program:`attach` RPC SHALL return an error response if:
349+
350+
- The subprocess does not exist or has exited without the waitable flag (ENOENT)
351+
- The subprocess is not currently in background mode or already has a
352+
client attached (EBUSY)
353+
- The client lacks authorization to attach to the subprocess (EPERM)
354+
264355
write
265356
=====
266357

0 commit comments

Comments
 (0)