Skip to content

Commit 18341a7

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 and convert it to a streaming 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 18341a7

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed

spec_42.rst

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

0 commit comments

Comments
 (0)