cocoonstack-sandbox 0.1.22__tar.gz → 0.1.23__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (36) hide show
  1. {cocoonstack_sandbox-0.1.22 → cocoonstack_sandbox-0.1.23}/PKG-INFO +1 -1
  2. {cocoonstack_sandbox-0.1.22 → cocoonstack_sandbox-0.1.23}/cocoonsandbox/__init__.py +1 -10
  3. {cocoonstack_sandbox-0.1.22 → cocoonstack_sandbox-0.1.23}/cocoonsandbox/client.py +5 -14
  4. {cocoonstack_sandbox-0.1.22 → cocoonstack_sandbox-0.1.23}/cocoonsandbox/conn.py +3 -5
  5. {cocoonstack_sandbox-0.1.22 → cocoonstack_sandbox-0.1.23}/cocoonsandbox/errors.py +9 -1
  6. {cocoonstack_sandbox-0.1.22 → cocoonstack_sandbox-0.1.23}/cocoonsandbox/frames.py +3 -6
  7. {cocoonstack_sandbox-0.1.22 → cocoonstack_sandbox-0.1.23}/cocoonsandbox/sandbox.py +17 -33
  8. {cocoonstack_sandbox-0.1.22 → cocoonstack_sandbox-0.1.23}/cocoonsandbox/template.py +2 -6
  9. {cocoonstack_sandbox-0.1.22 → cocoonstack_sandbox-0.1.23}/cocoonstack_sandbox.egg-info/PKG-INFO +1 -1
  10. {cocoonstack_sandbox-0.1.22 → cocoonstack_sandbox-0.1.23}/pyproject.toml +1 -1
  11. {cocoonstack_sandbox-0.1.22 → cocoonstack_sandbox-0.1.23}/tests/test_client.py +2 -31
  12. {cocoonstack_sandbox-0.1.22 → cocoonstack_sandbox-0.1.23}/tests/test_hardening.py +4 -3
  13. {cocoonstack_sandbox-0.1.22 → cocoonstack_sandbox-0.1.23}/tests/test_keepalive.py +45 -27
  14. {cocoonstack_sandbox-0.1.22 → cocoonstack_sandbox-0.1.23}/tests/test_proc.py +21 -21
  15. {cocoonstack_sandbox-0.1.22 → cocoonstack_sandbox-0.1.23}/tests/test_stream.py +6 -6
  16. {cocoonstack_sandbox-0.1.22 → cocoonstack_sandbox-0.1.23}/tests/test_upgrade.py +18 -8
  17. {cocoonstack_sandbox-0.1.22 → cocoonstack_sandbox-0.1.23}/LICENSE +0 -0
  18. {cocoonstack_sandbox-0.1.22 → cocoonstack_sandbox-0.1.23}/README.md +0 -0
  19. {cocoonstack_sandbox-0.1.22 → cocoonstack_sandbox-0.1.23}/cocoonsandbox/checkpoint.py +0 -0
  20. {cocoonstack_sandbox-0.1.22 → cocoonstack_sandbox-0.1.23}/cocoonsandbox/endpoint.py +0 -0
  21. {cocoonstack_sandbox-0.1.22 → cocoonstack_sandbox-0.1.23}/cocoonsandbox/py.typed +0 -0
  22. {cocoonstack_sandbox-0.1.22 → cocoonstack_sandbox-0.1.23}/cocoonsandbox/volumes.py +0 -0
  23. {cocoonstack_sandbox-0.1.22 → cocoonstack_sandbox-0.1.23}/cocoonstack_sandbox.egg-info/SOURCES.txt +0 -0
  24. {cocoonstack_sandbox-0.1.22 → cocoonstack_sandbox-0.1.23}/cocoonstack_sandbox.egg-info/dependency_links.txt +0 -0
  25. {cocoonstack_sandbox-0.1.22 → cocoonstack_sandbox-0.1.23}/cocoonstack_sandbox.egg-info/top_level.txt +0 -0
  26. {cocoonstack_sandbox-0.1.22 → cocoonstack_sandbox-0.1.23}/setup.cfg +0 -0
  27. {cocoonstack_sandbox-0.1.22 → cocoonstack_sandbox-0.1.23}/tests/test_checkpoint.py +0 -0
  28. {cocoonstack_sandbox-0.1.22 → cocoonstack_sandbox-0.1.23}/tests/test_endpoint.py +0 -0
  29. {cocoonstack_sandbox-0.1.22 → cocoonstack_sandbox-0.1.23}/tests/test_fault_matrix.py +0 -0
  30. {cocoonstack_sandbox-0.1.22 → cocoonstack_sandbox-0.1.23}/tests/test_fixtures.py +0 -0
  31. {cocoonstack_sandbox-0.1.22 → cocoonstack_sandbox-0.1.23}/tests/test_frames.py +0 -0
  32. {cocoonstack_sandbox-0.1.22 → cocoonstack_sandbox-0.1.23}/tests/test_packaging.py +0 -0
  33. {cocoonstack_sandbox-0.1.22 → cocoonstack_sandbox-0.1.23}/tests/test_proxy.py +0 -0
  34. {cocoonstack_sandbox-0.1.22 → cocoonstack_sandbox-0.1.23}/tests/test_volume_controller.py +0 -0
  35. {cocoonstack_sandbox-0.1.22 → cocoonstack_sandbox-0.1.23}/tests/test_volumes.py +0 -0
  36. {cocoonstack_sandbox-0.1.22 → cocoonstack_sandbox-0.1.23}/tests/test_wire_binding.py +21 -21
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: cocoonstack-sandbox
3
- Version: 0.1.22
3
+ Version: 0.1.23
4
4
  Summary: Python SDK for the cocoon sandbox control plane
5
5
  License: Apache-2.0
6
6
  Requires-Python: >=3.9
@@ -1,13 +1,4 @@
1
- """Python SDK for the cocoon sandbox control plane: claim a microVM from a
2
- sandboxd node, run commands over the relayed silkd protocol, branch it with
3
- checkpoints, release it. stdlib-only.
4
-
5
- from cocoonsandbox import Client
6
-
7
- client = Client("node:7777", api_token="...")
8
- with client.new("ghcr.io/cocoonstack/sandbox/rt:24.04") as sb:
9
- print(sb.exec("echo", "hello"))
10
- """
1
+ """Python SDK for the cocoon sandbox control plane; stdlib-only."""
11
2
 
12
3
  from .checkpoint import Checkpoint
13
4
  from .client import Client
@@ -15,7 +15,7 @@ from typing import Any, TypeVar, cast
15
15
  from .checkpoint import Checkpoint
16
16
  from .conn import Conn, dial_agent, is_quiet, remaining_timeout
17
17
  from .endpoint import _endpoint_url
18
- from .errors import APIError, SandboxTimeout, require_field
18
+ from .errors import APIError, SandboxTimeout, _error_message, require_field
19
19
  from .sandbox import Sandbox
20
20
  from .template import _claim_body, _template_query
21
21
  from .volumes import Volume
@@ -38,7 +38,7 @@ class Client:
38
38
  ssl_context: ssl.SSLContext | None = None,
39
39
  keep_alive: float = 30.0,
40
40
  ) -> None:
41
- """keep_alive keeps a handle's idle relay connection, which holds the sandbox's idle clock; 0 dials per call.
41
+ """keep_alive parks idle control and relay connections, which hold the sandbox's idle clock; 0 dials per call.
42
42
 
43
43
  ssl_context is set to offer ALPN http/1.1, which the relay upgrade needs; pass a dedicated context.
44
44
  """
@@ -62,12 +62,11 @@ class Client:
62
62
  ttl_seconds: int = 0,
63
63
  claim_ref: str = "",
64
64
  volumes: list[str | Volume] | None = None,
65
- mount: bool = True,
66
65
  *,
67
66
  deadline: float | None = None,
68
67
  ) -> Sandbox:
69
68
  """Claims a sandbox; a warm hit is milliseconds. deadline is a time.monotonic() bound over the whole claim."""
70
- claim = _claim_body(template, net, size, ttl_seconds, volumes, mount, claim_ref)
69
+ claim = _claim_body(template, net, size, ttl_seconds, volumes, claim_ref)
71
70
  return self._claim_from(self.addr, claim, deadline=deadline)
72
71
 
73
72
  def delete_template(self, template: str, net: str = "", size: str = "") -> None:
@@ -75,7 +74,7 @@ class Client:
75
74
  query = _template_query(template, net, size)
76
75
  path = "/v1/templates?" + urllib.parse.urlencode(query)
77
76
  reply = self._request(self.addr, "DELETE", path, None, "delete template")
78
- candidates = (reply or {}).get("redirect") or []
77
+ candidates = reply.get("redirect") or []
79
78
  if not candidates:
80
79
  return
81
80
  query["no_redirect"] = "1"
@@ -83,7 +82,7 @@ class Client:
83
82
  _try_each(candidates, lambda peer: self._request(peer, "DELETE", path, None, "delete template"))
84
83
 
85
84
  def lookup(self, id: str, token: str) -> Sandbox:
86
- """Finds the owner by probing the entry and peers concurrently."""
85
+ """Finds the owner by listing the peers, then probing the entry and every peer concurrently."""
87
86
 
88
87
  def probe(addr: str) -> Sandbox:
89
88
  reply = self._request(
@@ -259,7 +258,6 @@ class Client:
259
258
  return resp.status, raw
260
259
 
261
260
  def _take(self, key: tuple[str, str], timeout: float) -> http.client.HTTPConnection:
262
- """Reuses a parked connection the node has not closed within keep_alive, else dials."""
263
261
  with self._idle_lock:
264
262
  idle = self._idle.get(key, [])
265
263
  while idle:
@@ -280,13 +278,6 @@ class Client:
280
278
  return self._ssl_context
281
279
 
282
280
 
283
- def _error_message(raw: bytes) -> str:
284
- try:
285
- return cast(str, json.loads(raw)["error"])
286
- except (ValueError, KeyError, TypeError):
287
- return raw.decode(errors="replace").strip()
288
-
289
-
290
281
  def _retry_miss(exc: APIError) -> bool:
291
282
  return exc.status in (404, 0)
292
283
 
@@ -12,7 +12,7 @@ import urllib.parse
12
12
  from collections.abc import Callable, Iterator
13
13
  from typing import Any, BinaryIO, Protocol, TypeVar, cast
14
14
 
15
- from .errors import APIError, ProtocolError, SandboxError, SandboxTimeout, SilkdError
15
+ from .errors import APIError, ProtocolError, SandboxError, SandboxTimeout, SilkdError, _error_message
16
16
  from .frames import MAX_FRAME, decode_response, encode_request
17
17
 
18
18
  KEEP_ALIVE_CONNS = 8
@@ -24,8 +24,6 @@ _CloseableT = TypeVar("_CloseableT", bound="_Closeable")
24
24
 
25
25
 
26
26
  class _Closeable(Protocol):
27
- """Context-manager mixin for handles whose exit is just close()."""
28
-
29
27
  def __enter__(self: _CloseableT) -> _CloseableT:
30
28
  return self
31
29
 
@@ -294,5 +292,5 @@ def _read_upgrade(reader: BinaryIO, arm: Callable[[], None]) -> None:
294
292
  raise ProtocolError("invalid content-length in upgrade reply") from exc
295
293
  if code != 101:
296
294
  arm()
297
- body = reader.read(min(body_len, MAX_FRAME)).decode(errors="replace") if body_len else ""
298
- raise APIError("agent upgrade", code, body.strip() or status.strip())
295
+ body = reader.read(min(body_len, MAX_FRAME)) if body_len else b""
296
+ raise APIError("agent upgrade", code, _error_message(body) or status.strip())
@@ -2,7 +2,8 @@
2
2
 
3
3
  from __future__ import annotations
4
4
 
5
- from typing import TYPE_CHECKING, Any
5
+ import json
6
+ from typing import TYPE_CHECKING, Any, cast
6
7
 
7
8
  if TYPE_CHECKING:
8
9
  from .volumes import SandboxVolumesResponse
@@ -63,3 +64,10 @@ def require_field(reply: dict[str, Any], key: str, verb: str) -> Any:
63
64
  if key not in reply:
64
65
  raise APIError(verb, 0, f"reply without {key}")
65
66
  return reply[key]
67
+
68
+
69
+ def _error_message(raw: bytes) -> str:
70
+ try:
71
+ return cast(str, json.loads(raw)["error"])
72
+ except (ValueError, KeyError, TypeError):
73
+ return raw.decode(errors="replace").strip()
@@ -10,19 +10,17 @@ import sys
10
10
  from typing import Any
11
11
 
12
12
  PROTO_VERSION = 1
13
- # the info proto from which silkd serves RPCs back to back on one connection
14
- KEEP_ALIVE_PROTO = 2
15
13
  MAX_FRAME = 8 * 1024 * 1024
16
- FS_CHUNK = 256 * 1024
14
+ BULK_CHUNK = 256 * 1024
17
15
  # tar and port streams chunk at 1 MiB, still under MAX_FRAME after base64
18
- BULK_CHUNK = 1 << 20
16
+ STREAM_CHUNK = 1 << 20
19
17
  _FAST_DATA = sys.version_info >= (3, 11)
20
18
 
21
19
 
22
20
  def encode_request(op: str, **fields: object) -> bytes:
23
21
  """Encodes one request, omitting None fields and base64-encoding byte values."""
24
22
  if len(fields) == 1 and isinstance(data := fields.get("data"), (bytes, bytearray, memoryview)):
25
- # base64 needs no JSON escaping, so a bare data frame renders without a str round trip
23
+ # base64 needs no JSON escaping
26
24
  return b'{"v":%d,"op":"%s","data":"%s"}\n' % (PROTO_VERSION, op.encode(), base64.b64encode(data))
27
25
  frame = {"v": PROTO_VERSION, "op": op}
28
26
  for key, value in fields.items():
@@ -36,7 +34,6 @@ def encode_request(op: str, **fields: object) -> bytes:
36
34
 
37
35
  def decode_response(line: bytes) -> dict[str, Any]:
38
36
  """Decodes one response with binary payloads under data."""
39
- # base64 is JSON-escape-free, so an exactly-shaped data frame slices without json.loads.
40
37
  if _FAST_DATA and line.startswith(b'{"type":"'):
41
38
  te = line.find(b'"', 9)
42
39
  if te > 0 and line[9:te] in (b"stdout", b"stderr", b"data") and line.startswith(b'","data":"', te):
@@ -22,7 +22,7 @@ from .errors import (
22
22
  VolumeError,
23
23
  require_field,
24
24
  )
25
- from .frames import BULK_CHUNK, FS_CHUNK, KEEP_ALIVE_PROTO
25
+ from .frames import BULK_CHUNK, STREAM_CHUNK
26
26
  from .template import Template
27
27
  from .volumes import SandboxVolumesResponse, Volume, _volume_body, _volume_response
28
28
 
@@ -57,7 +57,6 @@ class Sandbox:
57
57
  self.volumes = [volume.copy() for volume in volumes or []]
58
58
  self.volumes_want = [volume.copy() for volume in self.volumes]
59
59
  self._pool = ConnPool(client.keep_alive)
60
- self._proto = 0
61
60
 
62
61
  def __enter__(self) -> Sandbox:
63
62
  return self
@@ -184,6 +183,8 @@ class Sandbox:
184
183
  if watchdog is not None:
185
184
  watchdog.cancel()
186
185
  if pump is not None:
186
+ if pump.is_alive():
187
+ conn.abort()
187
188
  pump.join() # the pump's frames must not land on the next RPC
188
189
  if code is None:
189
190
  raise ProtocolError("exec stream ended without an exit frame")
@@ -225,7 +226,7 @@ class Sandbox:
225
226
  def write_file(self, path: str, data: bytes, mode: int | None = None) -> None:
226
227
  """Writes data to path atomically (temp + rename on the guest)."""
227
228
  with self._lease(self._connect("fs_write", path=path, mode=mode)) as conn:
228
- _upload(conn, data, FS_CHUNK)
229
+ _upload(conn, data, BULK_CHUNK)
229
230
 
230
231
  def read_file(self, path: str) -> bytes:
231
232
  with self._lease(self._connect("fs_read", path=path)) as conn:
@@ -254,7 +255,7 @@ class Sandbox:
254
255
  def push(self, dest: str, tar_stream: bytes) -> None:
255
256
  """Extracts a tar stream into dest; a truncated stream leaves dest untouched."""
256
257
  with self._lease(self._connect("fs_push", dest=dest)) as conn:
257
- _upload(conn, tar_stream, BULK_CHUNK)
258
+ _upload(conn, tar_stream, STREAM_CHUNK)
258
259
 
259
260
  def pull(self, path: str) -> bytes:
260
261
  """Returns path (file or tree) as a tar archive."""
@@ -418,11 +419,9 @@ class Sandbox:
418
419
  unreachable = exc if exc.status == 0 else None
419
420
  try:
420
421
  moved = self._client.lookup(self.id, self.token)
421
- except APIError as exc:
422
+ except APIError:
422
423
  if unreachable is not None:
423
424
  raise unreachable from None
424
- if exc.status != 404:
425
- raise
426
425
  return
427
426
  try:
428
427
  self._release_at(moved.owner)
@@ -468,29 +467,20 @@ class Sandbox:
468
467
  return self._client._dial(self.owner, self.id, self.token, deadline)
469
468
 
470
469
  def _connect(self, op: str, deadline: float | None = None, /, **fields: object) -> Conn:
471
- """Sends the request on a parked or new connection, behind an info probe on a handle's first kept dial."""
470
+ """Sends the request on a parked or new connection."""
472
471
  conn = self._pool.take()
473
472
  if conn is None:
474
473
  conn = self._dial(deadline)
475
- probe = not self._proto and self._client.keep_alive > 0
476
- bounded = probe or deadline is not None
477
474
  try:
478
- if bounded:
475
+ if deadline is not None:
479
476
  conn.settimeout(remaining_timeout(self._client.timeout, deadline, "agent request"))
480
- if probe:
481
- conn.send("info")
482
477
  conn.send(op, **fields)
483
- if probe:
484
- self._proto = int(_expect(conn, "info").get("proto") or 1)
485
- if bounded:
478
+ if deadline is not None:
486
479
  conn.settimeout(None)
487
480
  except BaseException:
488
481
  conn.close()
489
482
  raise
490
- if not probe or self._proto >= KEEP_ALIVE_PROTO:
491
- return conn
492
- conn.close()
493
- return self._connect(op, deadline, **fields)
483
+ return conn
494
484
 
495
485
  @contextlib.contextmanager
496
486
  def _lease(self, conn: Conn) -> Iterator[Conn]:
@@ -498,25 +488,19 @@ class Sandbox:
498
488
  try:
499
489
  yield conn
500
490
  except SilkdError:
501
- self._park(conn)
491
+ self._pool.park(conn)
502
492
  raise
503
493
  except BaseException:
504
494
  conn.close()
505
495
  raise
506
- self._park(conn)
507
-
508
- def _park(self, conn: Conn) -> None:
509
- if self._proto >= KEEP_ALIVE_PROTO:
510
- self._pool.park(conn)
511
- else:
512
- conn.close()
496
+ self._pool.park(conn)
513
497
 
514
498
  def _open_stream(self, op: str, expect: str = "ready", **fields: object) -> tuple[Conn, dict[str, Any]]:
515
499
  conn = self._connect(op, **fields)
516
500
  try:
517
501
  frame = _expect(conn, expect)
518
502
  except SilkdError:
519
- self._park(conn)
503
+ self._pool.park(conn)
520
504
  raise
521
505
  except BaseException:
522
506
  conn.close()
@@ -556,7 +540,7 @@ class Sandbox:
556
540
  try:
557
541
  with contextlib.suppress(SandboxError, OSError):
558
542
  while True:
559
- chunk = local.recv(FS_CHUNK)
543
+ chunk = local.recv(BULK_CHUNK)
560
544
  if not chunk:
561
545
  break
562
546
  guest.send(chunk)
@@ -616,7 +600,7 @@ class Session(_Closeable):
616
600
 
617
601
 
618
602
  class Watcher(_Closeable):
619
- """A live filesystem event stream; iterate for {kind, path} events."""
603
+ """A live filesystem event stream; iterate for {type, kind, path} events."""
620
604
 
621
605
  def __init__(self, conn: Conn) -> None:
622
606
  self._conn = conn
@@ -697,7 +681,7 @@ class PortConn(_Closeable):
697
681
  self._eof = False
698
682
 
699
683
  def send(self, data: bytes) -> None:
700
- _send_chunks(self._conn, data, chunk=BULK_CHUNK)
684
+ _send_chunks(self._conn, data, chunk=STREAM_CHUNK)
701
685
 
702
686
  def recv(self) -> bytes:
703
687
  """Returns the next chunk from the guest; b'' on stream end."""
@@ -717,7 +701,7 @@ class PortConn(_Closeable):
717
701
  self._conn.close()
718
702
 
719
703
 
720
- def _send_chunks(conn: Conn, data: bytes, op: str = "data", chunk: int = FS_CHUNK) -> None:
704
+ def _send_chunks(conn: Conn, data: bytes, op: str = "data", chunk: int = BULK_CHUNK) -> None:
721
705
  view = memoryview(data)
722
706
  for off in range(0, len(view), chunk):
723
707
  conn.send(op, data=view[off : off + chunk])
@@ -27,12 +27,11 @@ class Template:
27
27
  self,
28
28
  ttl_seconds: int = 0,
29
29
  volumes: list[str | Volume] | None = None,
30
- mount: bool = True,
31
30
  *,
32
31
  deadline: float | None = None,
33
32
  ) -> Sandbox:
34
- """Claims the template, following placement when volumes require it; mount=False attaches them unmounted."""
35
- claim = _claim_body(self.name, self.net, self.size, ttl_seconds, volumes, mount)
33
+ """Claims the template, following placement when volumes require it."""
34
+ claim = _claim_body(self.name, self.net, self.size, ttl_seconds, volumes)
36
35
  claim["require_promoted"] = True
37
36
  if volumes:
38
37
  return self._client._claim_from(self._addr, claim, deadline=deadline)
@@ -54,7 +53,6 @@ def _claim_body(
54
53
  size: str,
55
54
  ttl_seconds: int,
56
55
  volumes: list[str | Volume] | None = None,
57
- mount: bool = True,
58
56
  claim_ref: str = "",
59
57
  ) -> dict[str, Any]:
60
58
  claim: dict[str, Any] = {"template": template}
@@ -68,8 +66,6 @@ def _claim_body(
68
66
  raise TypeError("volumes must be a list")
69
67
  if volumes:
70
68
  claim["volumes"] = [_volume_body(volume) for volume in volumes]
71
- if not mount:
72
- claim["volumes_attach_only"] = True
73
69
  if claim_ref:
74
70
  claim["claim_ref"] = claim_ref
75
71
  return claim
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: cocoonstack-sandbox
3
- Version: 0.1.22
3
+ Version: 0.1.23
4
4
  Summary: Python SDK for the cocoon sandbox control plane
5
5
  License: Apache-2.0
6
6
  Requires-Python: >=3.9
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "cocoonstack-sandbox"
7
- version = "0.1.22"
7
+ version = "0.1.23"
8
8
  description = "Python SDK for the cocoon sandbox control plane"
9
9
  requires-python = ">=3.9"
10
10
  license = { text = "Apache-2.0" }
@@ -114,43 +114,14 @@ def test_claim_preserves_explicit_volume_mode(node):
114
114
  assert seen[1]["volumes"] == [{"name": "imagenet", "mode": ""}]
115
115
 
116
116
 
117
- def test_claim_attaches_volumes_without_mounting(node):
118
- seen = recording_claim(
119
- {"id": "sb_1", "token": "tok", "volumes": [{"name": "imagenet"}, {"name": "scratch", "mode": "rw"}]}
120
- )
121
- sb = Client(node).new("rt:24.04", volumes=["imagenet", {"name": "scratch", "mode": "rw"}], mount=False)
122
- assert seen == [
123
- {
124
- "template": "rt:24.04",
125
- "volumes": [{"name": "imagenet"}, {"name": "scratch", "mode": "rw"}],
126
- "volumes_attach_only": True,
127
- }
128
- ]
129
- assert sb.volumes == [{"name": "imagenet"}, {"name": "scratch", "mode": "rw"}]
130
-
131
-
132
- def test_template_claim_attaches_volumes_without_mounting(node):
133
- seen = recording_claim({"id": "sb_2", "token": "tok", "volumes": [{"name": "imagenet"}]})
134
- sb = Template(Client(node), node, "task:v1", "none", "small").new(volumes=["imagenet"], mount=False)
135
- assert seen[0]["volumes_attach_only"] is True
136
- assert seen[0]["volumes"] == [{"name": "imagenet"}]
137
- assert sb.volumes == [{"name": "imagenet"}]
138
-
139
-
140
117
  def test_claim_leaves_volume_policy_to_server(node):
141
118
  seen = recording_claim({"id": "sb_1", "token": "tok"})
142
119
  entry = {"name": "imagenet", "mount": "/datasets", "attach_only": True, "mode": "future-mode"}
143
- Client(node).new("rt:24.04", volumes=[entry], mount=False)
144
- Template(Client(node), node, "task:v1", "none", "small").new(volumes=[entry], mount=False)
120
+ Client(node).new("rt:24.04", volumes=[entry])
121
+ Template(Client(node), node, "task:v1", "none", "small").new(volumes=[entry])
145
122
  assert [request["volumes"] for request in seen] == [[entry], [entry]]
146
123
 
147
124
 
148
- def test_claim_keeps_mounting_by_default(node):
149
- seen = recording_claim({"id": "sb_1", "token": "tok"})
150
- Client(node).new("rt:24.04", volumes=["imagenet"])
151
- assert "volumes_attach_only" not in seen[0]
152
-
153
-
154
125
  def test_template_claim_asks_for_the_promoted_template(node):
155
126
  seen = recording_claim({"id": "sb_9", "token": "tok"})
156
127
  Template(Client(node), node, "task:v1", "none", "small").new()
@@ -22,7 +22,7 @@ from cocoonsandbox import (
22
22
  Watcher,
23
23
  )
24
24
  from cocoonsandbox.conn import Conn, dial_agent, remaining_timeout
25
- from cocoonsandbox.frames import FS_CHUNK
25
+ from cocoonsandbox.frames import BULK_CHUNK
26
26
 
27
27
 
28
28
  def test_dial_agent_rejects_control_chars_in_identity():
@@ -72,6 +72,7 @@ def test_watcher_ends_cleanly_on_garbage_frame(conn_pair):
72
72
  guest_sock.close()
73
73
  watcher = Watcher(conn)
74
74
  assert list(watcher) == []
75
+ assert isinstance(watcher.error, ProtocolError), watcher.error
75
76
 
76
77
 
77
78
  @pytest.mark.parametrize(
@@ -125,10 +126,10 @@ def test_pty_write_chunks_a_large_buffer(conn_pair):
125
126
 
126
127
  reader_thread = threading.Thread(target=drain, daemon=True)
127
128
  reader_thread.start()
128
- pty.write(b"x" * (3 * FS_CHUNK))
129
+ pty.write(b"x" * (3 * BULK_CHUNK))
129
130
  reader_thread.join(5)
130
131
  assert [f["op"] for f in frames] == ["stdin"] * 3
131
- assert all(len(base64.b64decode(f["data"])) == FS_CHUNK for f in frames)
132
+ assert all(len(base64.b64decode(f["data"])) == BULK_CHUNK for f in frames)
132
133
 
133
134
 
134
135
  def test_session_and_lsp_close_twice(monkeypatch):
@@ -10,16 +10,14 @@ import pytest
10
10
  from conftest import accept_upgrade, sandbox_at, wait_until
11
11
 
12
12
  from cocoonsandbox import SilkdError
13
- from cocoonsandbox.frames import KEEP_ALIVE_PROTO
14
13
 
15
14
  INPUT_OPS = ("stdin", "stdin_close", "data", "data_end")
16
15
 
17
16
 
18
17
  class FakeAgent:
19
- """A relay-side silkd stand-in; proto 1 half-closes after one RPC and drains; slam closes as a failed relay does."""
18
+ """A relay-side silkd stand-in; hang_up_after half-closes after N RPCs, slam closes as a failed relay does."""
20
19
 
21
- def __init__(self, proto: int = KEEP_ALIVE_PROTO, hang_up_after: int = 0, slam: bool = False) -> None:
22
- self.proto = proto
20
+ def __init__(self, hang_up_after: int = 0, slam: bool = False) -> None:
23
21
  self.hang_up_after = hang_up_after
24
22
  self.slam = slam
25
23
  self.upgrades = 0
@@ -55,7 +53,7 @@ class FakeAgent:
55
53
  replies += 1
56
54
  if self.slam:
57
55
  break
58
- if self.proto < KEEP_ALIVE_PROTO or replies == self.hang_up_after:
56
+ if replies == self.hang_up_after:
59
57
  conn.shutdown(socket.SHUT_WR)
60
58
  self.hangups += 1
61
59
  while reader.readline():
@@ -65,7 +63,7 @@ class FakeAgent:
65
63
 
66
64
  def _answer(self, op: str) -> list:
67
65
  if op == "info":
68
- return [{"type": "info", "version": "fake", "proto": self.proto, "uptime_secs": 0, "procs": 0}]
66
+ return [{"type": "info", "version": "fake", "uptime_secs": 0, "procs": 0}]
69
67
  if op == "fs_stat":
70
68
  return [{"type": "stat", "info": {"kind": "dir", "size": 0, "mode": 0o755, "mtime_epoch_secs": 0}}]
71
69
  if op == "exec":
@@ -91,20 +89,6 @@ def test_a_refused_stream_open_keeps_its_connection(relay):
91
89
  assert agent.upgrades == 1
92
90
 
93
91
 
94
- def test_old_daemon_dials_per_call(relay):
95
- agent, sb = relay(proto=1)
96
- for _ in range(3):
97
- assert sb.exec("echo", "42") == "42\n"
98
- assert agent.upgrades == 4, "the proto probe plus one dial per call"
99
-
100
-
101
- def test_old_daemon_that_closes_after_info_gets_the_request_on_a_redial(relay):
102
- agent, sb = relay(proto=1, slam=True)
103
- for _ in range(2):
104
- assert sb.stat("/")["kind"] == "dir"
105
- assert agent.upgrades == 3, "the proto probe plus one dial per call"
106
-
107
-
108
92
  def test_keep_alive_off_dials_per_call(relay):
109
93
  agent, sb = relay(keep_alive=0)
110
94
  for _ in range(3):
@@ -119,14 +103,14 @@ def test_idle_connection_closes(relay):
119
103
 
120
104
 
121
105
  def test_peer_hang_up_is_noticed_before_reuse(fd_floor, relay):
122
- agent, sb = relay(hang_up_after=2)
106
+ agent, sb = relay(hang_up_after=1)
123
107
  sb.stat("/")
124
108
  wait_until(lambda: agent.hangups == 1, "the fake never hung up")
125
109
  assert sb.stat("/")["kind"] == "dir"
126
110
  assert agent.upgrades == 2
127
111
 
128
112
 
129
- def test_run_timeout_bounds_the_proto_probe():
113
+ def test_run_timeout_bounds_an_unanswered_request():
130
114
  server = socket.create_server(("127.0.0.1", 0))
131
115
  cut = threading.Event()
132
116
 
@@ -144,8 +128,42 @@ def test_run_timeout_bounds_the_proto_probe():
144
128
  try:
145
129
  with pytest.raises(TimeoutError):
146
130
  sb.run(["true"], timeout=0.5)
147
- assert cut.wait(1), "the unanswered info probe outlived the run timeout"
131
+ assert cut.wait(1), "the unanswered request outlived the run timeout"
132
+ finally:
133
+ server.close()
134
+
135
+
136
+ def test_run_stops_feeding_stdin_once_the_command_exits():
137
+ server = socket.create_server(("127.0.0.1", 0))
138
+ release = threading.Event()
139
+
140
+ def serve():
141
+ conn, _ = server.accept()
142
+ with conn, contextlib.suppress(OSError):
143
+ reader = accept_upgrade(conn)
144
+ for line in iter(reader.readline, b""):
145
+ op = json.loads(line)["op"]
146
+ if op == "info":
147
+ info = {"type": "info", "version": "fake", "uptime_secs": 0}
148
+ conn.sendall(json.dumps(info).encode() + b"\n")
149
+ elif op == "exec":
150
+ conn.sendall(b'{"type":"started","pid":1}\n{"type":"exit","code":0}\n')
151
+ release.wait(10)
152
+ return
153
+
154
+ threading.Thread(target=serve, daemon=True).start()
155
+ sb = sandbox_at(f"127.0.0.1:{server.getsockname()[1]}")
156
+ codes = []
157
+ call = threading.Thread(
158
+ target=lambda: codes.append(sb.run(["head", "-c", "10"], stdin=bytes(64 << 20), timeout=0.5)), daemon=True
159
+ )
160
+ try:
161
+ call.start()
162
+ call.join(2)
163
+ assert codes == [0], "run outlived its timeout behind a stdin the guest stopped reading"
164
+ assert sb._pool.take() is None
148
165
  finally:
166
+ release.set()
149
167
  server.close()
150
168
 
151
169
 
@@ -157,8 +175,8 @@ def test_close_drains_the_parked_connection(monkeypatch, relay):
157
175
  wait_until(lambda: agent.closed == 1, "parked connection survived close")
158
176
 
159
177
 
160
- def test_an_upload_an_old_daemon_rejects_raises_its_error_frame(relay):
161
- _, sb = relay(proto=1, slam=True)
178
+ def test_an_upload_the_daemon_rejects_at_open_raises_its_error_frame(relay):
179
+ _, sb = relay(slam=True)
162
180
  with pytest.raises(SilkdError):
163
181
  sb.write_file("/missing/f", bytes(64 << 20))
164
182
 
@@ -167,8 +185,8 @@ def test_an_upload_an_old_daemon_rejects_raises_its_error_frame(relay):
167
185
  def relay():
168
186
  started = []
169
187
 
170
- def start(proto=KEEP_ALIVE_PROTO, hang_up_after=0, slam=False, **client_kwargs):
171
- agent = FakeAgent(proto, hang_up_after, slam)
188
+ def start(hang_up_after=0, slam=False, **client_kwargs):
189
+ agent = FakeAgent(hang_up_after, slam)
172
190
  sb = sandbox_at(agent.addr, **client_kwargs)
173
191
  started.append((agent, sb))
174
192
  return agent, sb
@@ -6,7 +6,26 @@ import pytest
6
6
  from conftest import FakeConn, sandbox_at
7
7
 
8
8
  from cocoonsandbox import ProtocolError
9
- from cocoonsandbox.frames import FS_CHUNK
9
+ from cocoonsandbox.frames import BULK_CHUNK
10
+
11
+
12
+ class BlockingStdinConn(FakeConn):
13
+ """A guest that stops draining stdin until its output is read, so send blocks past the buffer."""
14
+
15
+ def __init__(self, frames, buffer_frames):
16
+ super().__init__(frames)
17
+ self._room = threading.Semaphore(buffer_frames)
18
+ self._read = False
19
+
20
+ def send(self, op, **fields):
21
+ if op == "stdin" and not self._read and not self._room.acquire(blocking=False):
22
+ assert self._room.acquire(timeout=5), "stdin send deadlocked"
23
+ super().send(op, **fields)
24
+
25
+ def recv(self):
26
+ self._read = True
27
+ self._room.release()
28
+ return super().recv()
10
29
 
11
30
 
12
31
  def test_spawn_returns_pid(monkeypatch):
@@ -56,7 +75,7 @@ def test_run_pumps_stdin_while_reading_output(monkeypatch):
56
75
  blocking = BlockingStdinConn([{"type": "exit", "code": 0}], buffer_frames=1)
57
76
  monkeypatch.setattr(sb, "_dial", lambda deadline=None: blocking)
58
77
 
59
- assert sb.run(["cat"], stdin=b"x" * (FS_CHUNK * 3)) == 0
78
+ assert sb.run(["cat"], stdin=b"x" * (BULK_CHUNK * 3)) == 0
60
79
  assert [op for op, _ in blocking.sent].count("stdin") == 3
61
80
  assert blocking.sent[-1][0] == "stdin_close"
62
81
 
@@ -75,25 +94,6 @@ def test_a_text_stdin_is_refused_before_dialing(monkeypatch, call):
75
94
  assert dials == []
76
95
 
77
96
 
78
- class BlockingStdinConn(FakeConn):
79
- """A guest that stops draining stdin until its output is read, so send blocks past the buffer."""
80
-
81
- def __init__(self, frames, buffer_frames):
82
- super().__init__(frames)
83
- self._room = threading.Semaphore(buffer_frames)
84
- self._read = False
85
-
86
- def send(self, op, **fields):
87
- if op == "stdin" and not self._read and not self._room.acquire(blocking=False):
88
- assert self._room.acquire(timeout=5), "stdin send deadlocked"
89
- super().send(op, **fields)
90
-
91
- def recv(self):
92
- self._read = True
93
- self._room.release()
94
- return super().recv()
95
-
96
-
97
97
  def fake_sandbox(monkeypatch, frames):
98
98
  sb = sandbox_at("127.0.0.1:1", keep_alive=0)
99
99
  conn = FakeConn(frames)
@@ -21,7 +21,7 @@ def test_port_stream_outlives_the_client_timeout():
21
21
  addr = f"127.0.0.1:{server.getsockname()[1]}"
22
22
  ops: list[str] = []
23
23
  threading.Thread(target=serve_port_forward, args=(server, 3 * TIMEOUT, ops), daemon=True).start()
24
- sb = legacy_sandbox(addr)
24
+ sb = short_timeout_sandbox(addr)
25
25
  try:
26
26
  with sb.dial_port(5000) as port:
27
27
  assert port.recv() == b"late"
@@ -36,7 +36,7 @@ def test_run_timeout_cuts_a_silent_command_and_kills_it():
36
36
  addr = f"127.0.0.1:{server.getsockname()[1]}"
37
37
  kills: list[dict] = []
38
38
  threading.Thread(target=serve_started_then_hang, args=(server, 5 * TIMEOUT, kills), daemon=True).start()
39
- sb = legacy_sandbox(addr)
39
+ sb = short_timeout_sandbox(addr)
40
40
  started = time.monotonic()
41
41
  try:
42
42
  with pytest.raises(TimeoutError):
@@ -53,7 +53,7 @@ def test_run_timeout_bounds_the_kill_it_sends(monkeypatch):
53
53
  addr = f"127.0.0.1:{server.getsockname()[1]}"
54
54
  kills: list[dict] = []
55
55
  threading.Thread(target=serve_started_then_trickle_the_kill_reply, args=(server, kills), daemon=True).start()
56
- sb = legacy_sandbox(addr)
56
+ sb = short_timeout_sandbox(addr)
57
57
  started = time.monotonic()
58
58
  try:
59
59
  with pytest.raises(TimeoutError):
@@ -73,7 +73,7 @@ def test_run_timeout_cuts_a_blocked_exec_send(black_hole):
73
73
 
74
74
 
75
75
  def test_run_rejects_a_non_positive_timeout():
76
- sb = legacy_sandbox("127.0.0.1:1")
76
+ sb = short_timeout_sandbox("127.0.0.1:1")
77
77
  with pytest.raises(ValueError):
78
78
  sb.run(["true"], timeout=0)
79
79
 
@@ -82,7 +82,7 @@ def test_dial_is_still_bounded_by_the_client_timeout():
82
82
  server = socket.create_server(("127.0.0.1", 0))
83
83
  addr = f"127.0.0.1:{server.getsockname()[1]}"
84
84
  threading.Thread(target=serve_silence, args=(server,), daemon=True).start()
85
- sb = legacy_sandbox(addr)
85
+ sb = short_timeout_sandbox(addr)
86
86
  started = time.monotonic()
87
87
  try:
88
88
  with pytest.raises(SandboxTimeout):
@@ -97,7 +97,7 @@ def test_handshake_failures_are_typed(exc, want):
97
97
  assert isinstance(conn_module._handshake_error("agent upgrade", exc), want)
98
98
 
99
99
 
100
- def legacy_sandbox(addr: str) -> Sandbox:
100
+ def short_timeout_sandbox(addr: str) -> Sandbox:
101
101
  return sandbox_at(addr, timeout=TIMEOUT, keep_alive=0)
102
102
 
103
103
 
@@ -10,10 +10,9 @@ import pytest
10
10
  from conftest import sandbox_at
11
11
 
12
12
  from cocoonsandbox import APIError, SandboxTimeout
13
- from cocoonsandbox.frames import KEEP_ALIVE_PROTO
14
13
 
15
14
  REPLIES = {
16
- "info": {"type": "info", "version": "fake", "proto": KEEP_ALIVE_PROTO, "uptime_secs": 0, "procs": 0},
15
+ "info": {"type": "info", "version": "fake", "uptime_secs": 0, "procs": 0},
17
16
  "fs_mkdir": {"type": "done"},
18
17
  }
19
18
  REJECT = b"HTTP/1.1 404 Not Found\r\nContent-Type: text/plain\r\nContent-Length: 15\r\n\r\nunknown sandbox"
@@ -48,18 +47,16 @@ class EarlyFrameAgent:
48
47
  conn.sendall(json.dumps(REPLIES[json.loads(line)["op"]]).encode() + b"\n")
49
48
 
50
49
 
51
- @pytest.mark.parametrize(
52
- ("keep_alive", "early"), [(0, ["fs_mkdir"]), (30, ["info", "fs_mkdir"])], ids=["no probe", "proto probe"]
53
- )
54
- def test_first_frames_ride_ahead_of_the_101(keep_alive, early):
55
- agent = EarlyFrameAgent(frames=len(early))
50
+ @pytest.mark.parametrize("keep_alive", [0, 30], ids=["dial per call", "kept connection"])
51
+ def test_the_first_frame_rides_ahead_of_the_101(keep_alive):
52
+ agent = EarlyFrameAgent(frames=1)
56
53
  sb = sandbox_at(agent.addr, timeout=5, keep_alive=keep_alive)
57
54
  try:
58
55
  sb.mkdir("/w")
59
56
  finally:
60
57
  sb._pool.drain()
61
58
  agent.stop()
62
- assert agent.early == early, agent.early
59
+ assert agent.early == ["fs_mkdir"], agent.early
63
60
 
64
61
 
65
62
  def test_rejected_upgrade_is_an_api_error():
@@ -74,6 +71,19 @@ def test_rejected_upgrade_is_an_api_error():
74
71
  assert agent.early == ["fs_mkdir"], agent.early
75
72
 
76
73
 
74
+ def test_rejected_upgrade_reports_the_servers_error_text():
75
+ body = b'{"error":"unknown sandbox"}'
76
+ head = b"HTTP/1.1 404 Not Found\r\nContent-Type: application/json\r\nContent-Length: %d\r\n\r\n" % len(body)
77
+ agent = EarlyFrameAgent(head + body)
78
+ sb = sandbox_at(agent.addr, timeout=5, keep_alive=0)
79
+ try:
80
+ with pytest.raises(APIError) as refused:
81
+ sb.mkdir("/w")
82
+ finally:
83
+ agent.stop()
84
+ assert (refused.value.status, refused.value.message) == (404, "unknown sandbox"), refused.value
85
+
86
+
77
87
  def test_run_timeout_cuts_an_unanswered_upgrade(black_hole):
78
88
  sb = sandbox_at(black_hole, timeout=30, keep_alive=0)
79
89
  started = time.monotonic()
@@ -80,6 +80,27 @@ CASES = [
80
80
  UNSENT = {"req_session_create": {"id"}}
81
81
 
82
82
 
83
+ class BranchActionConn(FakeConn):
84
+ """Records the action each git_branch verb puts on the wire."""
85
+
86
+ def __init__(self, sent):
87
+ super().__init__([])
88
+ self.sent = sent
89
+ self.action = ""
90
+
91
+ def send(self, op, **fields):
92
+ self.action = fields.get("action")
93
+ self.sent.append(self.action)
94
+
95
+ def recv(self):
96
+ if self.action == "list":
97
+ return {"type": "git_branches", "branches": [], "current": ""}
98
+ return {"type": "done"}
99
+
100
+ def recv_until(self, *terminal):
101
+ yield self.recv()
102
+
103
+
83
104
  @pytest.mark.parametrize("stem,replies,invoke", CASES, ids=lambda c: c if isinstance(c, str) else "")
84
105
  def test_call_site_matches_fixture(monkeypatch, stem, replies, invoke):
85
106
  fixture = json.loads((FIXTURES / f"{stem}.json").read_text())
@@ -122,27 +143,6 @@ def test_git_branch_actions_come_from_the_corpus(monkeypatch):
122
143
  assert set(sent) == set(enums["git_branch_action"])
123
144
 
124
145
 
125
- class BranchActionConn(FakeConn):
126
- """Records the action each git_branch verb puts on the wire."""
127
-
128
- def __init__(self, sent):
129
- super().__init__([])
130
- self.sent = sent
131
- self.action = ""
132
-
133
- def send(self, op, **fields):
134
- self.action = fields.get("action")
135
- self.sent.append(self.action)
136
-
137
- def recv(self):
138
- if self.action == "list":
139
- return {"type": "git_branches", "branches": [], "current": ""}
140
- return {"type": "done"}
141
-
142
- def recv_until(self, *terminal):
143
- yield self.recv()
144
-
145
-
146
146
  def fake_sandbox(monkeypatch, replies):
147
147
  """A Sandbox whose _dial yields a real Conn over a socketpair to a scripted guest thread."""
148
148
  sent = []