cocoonstack-sandbox 0.1.37__tar.gz → 0.1.38__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.37 → cocoonstack_sandbox-0.1.38}/PKG-INFO +2 -2
  2. {cocoonstack_sandbox-0.1.37 → cocoonstack_sandbox-0.1.38}/cocoonsandbox/client.py +22 -13
  3. {cocoonstack_sandbox-0.1.37 → cocoonstack_sandbox-0.1.38}/cocoonsandbox/conn.py +2 -4
  4. {cocoonstack_sandbox-0.1.37 → cocoonstack_sandbox-0.1.38}/cocoonsandbox/errors.py +2 -4
  5. {cocoonstack_sandbox-0.1.37 → cocoonstack_sandbox-0.1.38}/cocoonsandbox/frames.py +1 -3
  6. {cocoonstack_sandbox-0.1.37 → cocoonstack_sandbox-0.1.38}/cocoonsandbox/sandbox.py +10 -8
  7. {cocoonstack_sandbox-0.1.37 → cocoonstack_sandbox-0.1.38}/cocoonstack_sandbox.egg-info/PKG-INFO +2 -2
  8. {cocoonstack_sandbox-0.1.37 → cocoonstack_sandbox-0.1.38}/pyproject.toml +2 -2
  9. {cocoonstack_sandbox-0.1.37 → cocoonstack_sandbox-0.1.38}/tests/test_fault_matrix.py +76 -3
  10. {cocoonstack_sandbox-0.1.37 → cocoonstack_sandbox-0.1.38}/tests/test_hardening.py +1 -1
  11. {cocoonstack_sandbox-0.1.37 → cocoonstack_sandbox-0.1.38}/LICENSE +0 -0
  12. {cocoonstack_sandbox-0.1.37 → cocoonstack_sandbox-0.1.38}/README.md +0 -0
  13. {cocoonstack_sandbox-0.1.37 → cocoonstack_sandbox-0.1.38}/cocoonsandbox/__init__.py +0 -0
  14. {cocoonstack_sandbox-0.1.37 → cocoonstack_sandbox-0.1.38}/cocoonsandbox/checkpoint.py +0 -0
  15. {cocoonstack_sandbox-0.1.37 → cocoonstack_sandbox-0.1.38}/cocoonsandbox/endpoint.py +0 -0
  16. {cocoonstack_sandbox-0.1.37 → cocoonstack_sandbox-0.1.38}/cocoonsandbox/py.typed +0 -0
  17. {cocoonstack_sandbox-0.1.37 → cocoonstack_sandbox-0.1.38}/cocoonsandbox/template.py +0 -0
  18. {cocoonstack_sandbox-0.1.37 → cocoonstack_sandbox-0.1.38}/cocoonsandbox/volumes.py +0 -0
  19. {cocoonstack_sandbox-0.1.37 → cocoonstack_sandbox-0.1.38}/cocoonstack_sandbox.egg-info/SOURCES.txt +0 -0
  20. {cocoonstack_sandbox-0.1.37 → cocoonstack_sandbox-0.1.38}/cocoonstack_sandbox.egg-info/dependency_links.txt +0 -0
  21. {cocoonstack_sandbox-0.1.37 → cocoonstack_sandbox-0.1.38}/cocoonstack_sandbox.egg-info/top_level.txt +0 -0
  22. {cocoonstack_sandbox-0.1.37 → cocoonstack_sandbox-0.1.38}/setup.cfg +0 -0
  23. {cocoonstack_sandbox-0.1.37 → cocoonstack_sandbox-0.1.38}/tests/test_checkpoint.py +0 -0
  24. {cocoonstack_sandbox-0.1.37 → cocoonstack_sandbox-0.1.38}/tests/test_client.py +0 -0
  25. {cocoonstack_sandbox-0.1.37 → cocoonstack_sandbox-0.1.38}/tests/test_endpoint.py +0 -0
  26. {cocoonstack_sandbox-0.1.37 → cocoonstack_sandbox-0.1.38}/tests/test_fixtures.py +0 -0
  27. {cocoonstack_sandbox-0.1.37 → cocoonstack_sandbox-0.1.38}/tests/test_frames.py +0 -0
  28. {cocoonstack_sandbox-0.1.37 → cocoonstack_sandbox-0.1.38}/tests/test_keepalive.py +0 -0
  29. {cocoonstack_sandbox-0.1.37 → cocoonstack_sandbox-0.1.38}/tests/test_packaging.py +0 -0
  30. {cocoonstack_sandbox-0.1.37 → cocoonstack_sandbox-0.1.38}/tests/test_proc.py +0 -0
  31. {cocoonstack_sandbox-0.1.37 → cocoonstack_sandbox-0.1.38}/tests/test_proxy.py +0 -0
  32. {cocoonstack_sandbox-0.1.37 → cocoonstack_sandbox-0.1.38}/tests/test_stream.py +0 -0
  33. {cocoonstack_sandbox-0.1.37 → cocoonstack_sandbox-0.1.38}/tests/test_upgrade.py +0 -0
  34. {cocoonstack_sandbox-0.1.37 → cocoonstack_sandbox-0.1.38}/tests/test_volume_controller.py +0 -0
  35. {cocoonstack_sandbox-0.1.37 → cocoonstack_sandbox-0.1.38}/tests/test_volumes.py +0 -0
  36. {cocoonstack_sandbox-0.1.37 → cocoonstack_sandbox-0.1.38}/tests/test_wire_binding.py +0 -0
@@ -1,9 +1,9 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: cocoonstack-sandbox
3
- Version: 0.1.37
3
+ Version: 0.1.38
4
4
  Summary: Python SDK for the cocoon sandbox control plane
5
5
  License: Apache-2.0
6
- Requires-Python: >=3.10
6
+ Requires-Python: >=3.12
7
7
  Description-Content-Type: text/markdown
8
8
  License-File: LICENSE
9
9
  Dynamic: license-file
@@ -10,7 +10,7 @@ import threading
10
10
  import time
11
11
  import urllib.parse
12
12
  from collections.abc import Callable, Iterable, Sequence
13
- from typing import Any, TypeVar, cast
13
+ from typing import Any, cast
14
14
 
15
15
  from .checkpoint import Checkpoint
16
16
  from .conn import Conn, _Closeable, dial_agent, is_quiet, remaining_timeout
@@ -20,8 +20,6 @@ from .sandbox import Sandbox
20
20
  from .template import _claim_body, _template_query
21
21
  from .volumes import Volume
22
22
 
23
- _T = TypeVar("_T")
24
-
25
23
  _PEERS_TIMEOUT = 5.0
26
24
  _IDLE_CONNS = 8
27
25
 
@@ -96,7 +94,11 @@ class Client(_Closeable):
96
94
  peers, peers_err = self._peers()
97
95
  try:
98
96
  return _scatter([self.addr, *peers], probe)
99
- except APIError:
97
+ except APIError as exc:
98
+ if exc.status >= 500 or exc.status == 0:
99
+ raise
100
+ if peers_err is not None and (peers_err.status >= 500 or peers_err.status == 0):
101
+ raise peers_err from None
100
102
  reason = f"no owner found for {id}" if peers_err is None else f"no owner found for {id}; {peers_err}"
101
103
  raise APIError("lookup", 404, reason) from None
102
104
 
@@ -297,9 +299,9 @@ def _retry_miss(exc: APIError) -> bool:
297
299
  return exc.status in (404, 0)
298
300
 
299
301
 
300
- def _try_each(
301
- candidates: Iterable[str], call: Callable[[str], _T], retry: Callable[[APIError], bool] = _retry_miss
302
- ) -> _T:
302
+ def _try_each[T](
303
+ candidates: Iterable[str], call: Callable[[str], T], retry: Callable[[APIError], bool] = _retry_miss
304
+ ) -> T:
303
305
  last_error = None
304
306
  for addr in candidates:
305
307
  try:
@@ -342,8 +344,14 @@ def _redirect_fallback(
342
344
  raise APIError(verb, origin_exc.status, combined) from origin_exc
343
345
 
344
346
 
345
- def _scatter(addrs: Sequence[str], probe: Callable[[str], _T]) -> _T:
346
- results: queue.Queue[tuple[_T | None, Exception | None]] = queue.Queue(maxsize=len(addrs))
347
+ def _failure_rank(exc: Exception | None) -> int:
348
+ if not isinstance(exc, APIError):
349
+ return 0 if exc is None else 1
350
+ return 3 if exc.status >= 500 else 2 if exc.status == 0 else 1
351
+
352
+
353
+ def _scatter[T](addrs: Sequence[str], probe: Callable[[str], T]) -> T:
354
+ results: queue.Queue[tuple[T | None, Exception | None]] = queue.Queue(maxsize=len(addrs))
347
355
 
348
356
  def run(addr: str) -> None:
349
357
  try:
@@ -353,10 +361,11 @@ def _scatter(addrs: Sequence[str], probe: Callable[[str], _T]) -> _T:
353
361
 
354
362
  for addr in addrs:
355
363
  threading.Thread(target=run, args=(addr,), daemon=True).start()
356
- last_error = None
364
+ failure = None
357
365
  for _ in addrs:
358
366
  value, error = results.get()
359
367
  if error is None:
360
- return cast(_T, value)
361
- last_error = error
362
- raise cast(Exception, last_error)
368
+ return cast(T, value)
369
+ if _failure_rank(error) > _failure_rank(failure):
370
+ failure = error
371
+ raise cast(Exception, failure)
@@ -10,7 +10,7 @@ import threading
10
10
  import time
11
11
  import urllib.parse
12
12
  from collections.abc import Callable, Iterator
13
- from typing import Any, BinaryIO, Protocol, TypeVar, cast
13
+ from typing import Any, BinaryIO, Protocol, Self, cast
14
14
 
15
15
  from .errors import APIError, ProtocolError, SandboxError, SandboxTimeout, SilkdError, _error_message, _mark_gone
16
16
  from .frames import MAX_FRAME, decode_response, encode_request
@@ -21,11 +21,9 @@ _READ_BUFFER = 64 * 1024
21
21
  _SEND_SLICE = 16 * 1024
22
22
  _TCP_KEEPALIVE = {"TCP_KEEPIDLE": 15, "TCP_KEEPALIVE": 15, "TCP_KEEPINTVL": 15, "TCP_KEEPCNT": 9}
23
23
 
24
- _CloseableT = TypeVar("_CloseableT", bound="_Closeable")
25
-
26
24
 
27
25
  class _Closeable(Protocol):
28
- def __enter__(self: _CloseableT) -> _CloseableT:
26
+ def __enter__(self) -> Self:
29
27
  return self
30
28
 
31
29
  def __exit__(self, *exc: object) -> None:
@@ -3,13 +3,11 @@
3
3
  from __future__ import annotations
4
4
 
5
5
  import json
6
- from typing import TYPE_CHECKING, Any, TypeVar, cast
6
+ from typing import TYPE_CHECKING, Any, cast
7
7
 
8
8
  if TYPE_CHECKING:
9
9
  from .volumes import SandboxVolumesResponse
10
10
 
11
- _E = TypeVar("_E", bound="SandboxError")
12
-
13
11
 
14
12
  class SandboxError(Exception):
15
13
  """Base class for all SDK errors."""
@@ -81,7 +79,7 @@ def _error_message(raw: bytes) -> str:
81
79
  return raw.decode(errors="replace").strip()
82
80
 
83
81
 
84
- def _mark_gone(exc: _E, tag: tuple[str, str]) -> _E:
82
+ def _mark_gone[E: SandboxError](exc: E, tag: tuple[str, str]) -> E:
85
83
  """Tags an owner's 404 or refused dial with its (sandbox id, owner address)."""
86
84
  exc._owner_gone = tag
87
85
  return exc
@@ -6,7 +6,6 @@ import base64
6
6
  import binascii
7
7
  import contextlib
8
8
  import json
9
- import sys
10
9
  from typing import Any
11
10
 
12
11
  PROTO_VERSION = 1
@@ -14,7 +13,6 @@ MAX_FRAME = 8 * 1024 * 1024
14
13
  BULK_CHUNK = 256 * 1024
15
14
  # tar and port streams chunk at 1 MiB, still under MAX_FRAME after base64
16
15
  STREAM_CHUNK = 1 << 20
17
- _FAST_DATA = sys.version_info >= (3, 11)
18
16
 
19
17
 
20
18
  def encode_request(op: str, **fields: object) -> bytes:
@@ -34,7 +32,7 @@ def encode_request(op: str, **fields: object) -> bytes:
34
32
 
35
33
  def decode_response(line: bytes) -> dict[str, Any]:
36
34
  """Decodes one response with binary payloads under data."""
37
- if _FAST_DATA and line.startswith(b'{"type":"'):
35
+ if line.startswith(b'{"type":"'):
38
36
  te = line.find(b'"', 9)
39
37
  if te > 0 and line[9:te] in (b"stdout", b"stderr", b"data") and line.startswith(b'","data":"', te):
40
38
  de = line.find(b'"', te + 10)
@@ -9,7 +9,7 @@ import threading
9
9
  import time
10
10
  from collections import deque
11
11
  from collections.abc import Callable, Iterator
12
- from typing import TYPE_CHECKING, Any, TypeVar, cast
12
+ from typing import TYPE_CHECKING, Any, cast
13
13
 
14
14
  from .checkpoint import Checkpoint
15
15
  from .conn import Conn, ConnPool, _Closeable, remaining_timeout
@@ -35,10 +35,8 @@ if TYPE_CHECKING:
35
35
  _ACCEPT_POLL_SECONDS = 1.0
36
36
  _KILL_WAIT_SECONDS = 5.0
37
37
 
38
- _F = TypeVar("_F", bound=Callable[..., Any])
39
38
 
40
-
41
- def _follows_move(method: _F) -> _F:
39
+ def _follows_move[F: Callable[..., Any]](method: F) -> F:
42
40
  """Runs a call once more on the owner a 404 points to; its arguments must be re-sendable."""
43
41
 
44
42
  @functools.wraps(method)
@@ -50,7 +48,7 @@ def _follows_move(method: _F) -> _F:
50
48
  raise
51
49
  return method(self, *args, **kwargs)
52
50
 
53
- return cast(_F, wrapper)
51
+ return cast(F, wrapper)
54
52
 
55
53
 
56
54
  class Sandbox:
@@ -316,7 +314,7 @@ class Sandbox:
316
314
  self._done_rpc("git_branch", unbounded=True, path=path, action="delete", name=name)
317
315
 
318
316
  def watch(self, path: str, recursive: bool = False) -> Watcher:
319
- """Streams filesystem events under path; events after the returned Watcher exists are guaranteed captured."""
317
+ """Returns an armed watcher that streams events under path, excluding directories whose own watch fails."""
320
318
  conn, _ = self._open_stream("fs_watch", path=path, recursive=recursive or None)
321
319
  return Watcher(conn)
322
320
 
@@ -418,9 +416,11 @@ class Sandbox:
418
416
  unreachable = exc if exc.status == 0 else None
419
417
  try:
420
418
  moved = self._client.lookup(self.id, self.token)
421
- except APIError:
419
+ except APIError as exc:
422
420
  if unreachable is not None:
423
421
  raise unreachable from None
422
+ if exc.status != 404:
423
+ raise
424
424
  return
425
425
  try:
426
426
  self._release_at(moved.owner)
@@ -473,7 +473,9 @@ class Sandbox:
473
473
  return True
474
474
  try:
475
475
  moved = self._client.lookup(self.id, self.token)
476
- except APIError:
476
+ except APIError as lookup_exc:
477
+ if lookup_exc.status >= 500:
478
+ raise
477
479
  return False
478
480
  if moved.owner == self.owner:
479
481
  return False
@@ -1,9 +1,9 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: cocoonstack-sandbox
3
- Version: 0.1.37
3
+ Version: 0.1.38
4
4
  Summary: Python SDK for the cocoon sandbox control plane
5
5
  License: Apache-2.0
6
- Requires-Python: >=3.10
6
+ Requires-Python: >=3.12
7
7
  Description-Content-Type: text/markdown
8
8
  License-File: LICENSE
9
9
  Dynamic: license-file
@@ -4,9 +4,9 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "cocoonstack-sandbox"
7
- version = "0.1.37"
7
+ version = "0.1.38"
8
8
  description = "Python SDK for the cocoon sandbox control plane"
9
- requires-python = ">=3.10"
9
+ requires-python = ">=3.12"
10
10
  license = { text = "Apache-2.0" }
11
11
  readme = "README.md"
12
12
 
@@ -7,7 +7,7 @@ import threading
7
7
 
8
8
  import cocoonsandbox.client
9
9
  import pytest
10
- from cocoonsandbox import APIError, Client
10
+ from cocoonsandbox import APIError, Client, Sandbox
11
11
  from conftest import bounded
12
12
 
13
13
  ENTRY_POINTS = pytest.mark.parametrize(
@@ -210,7 +210,7 @@ def test_lookup_returns_the_first_owner_without_waiting_for_a_slow_peer(spawn_no
210
210
  assert sb.owner == "10.0.0.9:7777"
211
211
 
212
212
 
213
- def test_lookup_all_miss(spawn_node, dead_addr):
213
+ def test_lookup_raises_a_peer_it_could_not_reach_instead_of_no_owner(spawn_node, dead_addr):
214
214
  entry = spawn_node(
215
215
  {
216
216
  ("GET", "/v1/sandboxes/sb_1/owner"): lambda body, path: (404, {"error": "not here"}),
@@ -219,7 +219,29 @@ def test_lookup_all_miss(spawn_node, dead_addr):
219
219
  )
220
220
  with pytest.raises(APIError) as exc:
221
221
  Client(entry).lookup("sb_1", "tok")
222
- assert exc.value.status == 404 and "no owner found" in exc.value.message
222
+ assert exc.value.status == 0, exc.value
223
+
224
+
225
+ def test_lookup_raises_a_node_error_instead_of_no_owner(spawn_node):
226
+ gate = threading.Event()
227
+
228
+ def late_miss(body, path):
229
+ gate.wait(60)
230
+ return 404, {"error": "not here"}
231
+
232
+ down = spawn_node(
233
+ {("GET", "/v1/sandboxes/sb_1/owner"): lambda body, path: (503, {"error": "archive registry down; retry"})}
234
+ )
235
+ entry = spawn_node(
236
+ {
237
+ ("GET", "/v1/sandboxes/sb_1/owner"): late_miss,
238
+ ("GET", "/v1/peers"): lambda body, path: (200, {"peers": [down]}),
239
+ }
240
+ )
241
+ threading.Timer(0.3, gate.set).start()
242
+ with pytest.raises(APIError) as exc:
243
+ Client(entry).lookup("sb_1", "tok")
244
+ assert exc.value.status == 503 and "retry" in exc.value.message, exc.value
223
245
 
224
246
 
225
247
  @pytest.mark.parametrize("retired", [False, True], ids=["old_node_404", "old_node_retired"])
@@ -299,6 +321,57 @@ def test_close_of_a_sandbox_no_node_holds_is_not_an_error(spawn_node):
299
321
  Client(gone).attach(gone, "sb_1", "tok").close()
300
322
 
301
323
 
324
+ def test_close_raises_a_peer_error_instead_of_already_gone(spawn_node):
325
+ with pytest.raises(APIError) as exc:
326
+ gone_sandbox(spawn_node, 503).close()
327
+ assert exc.value.status == 503, exc.value
328
+
329
+
330
+ def test_close_raises_when_a_peer_cannot_be_reached(spawn_node, dead_addr):
331
+ old = spawn_node({("GET", "/v1/peers"): lambda body, path: (200, {"peers": [dead_addr]})})
332
+ with pytest.raises(APIError) as info:
333
+ Client(old).attach(old, "sb_1", "tok").close()
334
+ assert info.value.status == 0, info.value
335
+
336
+
337
+ def test_close_raises_when_the_peer_listing_gets_no_answer(spawn_node):
338
+ def drop(body, path):
339
+ raise ConnectionResetError("peer listing dropped")
340
+
341
+ old = spawn_node({("GET", "/v1/peers"): drop})
342
+ with pytest.raises(APIError) as info:
343
+ Client(old).attach(old, "sb_1", "tok").close()
344
+ assert info.value.status == 0, info.value
345
+
346
+
347
+ def test_close_of_a_sandbox_no_peer_holds_is_not_an_error(spawn_node):
348
+ gone_sandbox(spawn_node, 404).close()
349
+
350
+
351
+ def test_lookup_raises_a_failed_peer_discovery_instead_of_no_owner(spawn_node):
352
+ entry = spawn_node(
353
+ {
354
+ ("GET", "/v1/sandboxes/sb_1/owner"): lambda body, path: (404, {"error": "not here"}),
355
+ ("GET", "/v1/peers"): lambda body, path: (503, {"error": "gossip not ready"}),
356
+ }
357
+ )
358
+ with pytest.raises(APIError) as exc:
359
+ Client(entry).lookup("sb_1", "tok")
360
+ assert exc.value.status == 503 and exc.value.verb == "peers", exc.value
361
+
362
+
363
+ def test_a_call_raises_a_peer_error_instead_of_the_old_owners_404(spawn_node):
364
+ with pytest.raises(APIError) as exc:
365
+ gone_sandbox(spawn_node, 503).stat("/")
366
+ assert exc.value.status == 503, exc.value
367
+
368
+
369
+ def gone_sandbox(spawn_node, peer_status: int) -> Sandbox:
370
+ peer = spawn_node({("GET", "/v1/sandboxes/sb_1/owner"): lambda body, path: (peer_status, {"error": "not ready"})})
371
+ old = spawn_node({("GET", "/v1/peers"): lambda body, path: (200, {"peers": [peer]})})
372
+ return Client(old).attach(old, "sb_1", "tok")
373
+
374
+
302
375
  def drop_reply(server: socket.socket) -> None:
303
376
  conn, _ = server.accept()
304
377
  conn.recv(65536)
@@ -282,7 +282,7 @@ def test_a_run_deadline_bounds_a_slowly_drained_request(monkeypatch):
282
282
  elapsed = time.monotonic() - started
283
283
  finally:
284
284
  theirs.close()
285
- assert elapsed < 0.6, elapsed
285
+ assert elapsed < 1.0, elapsed
286
286
 
287
287
 
288
288
  def test_a_send_cut_by_tcp_keepalive_stays_a_protocol_error():