|
| 1 | +# Concurrency |
| 2 | + |
| 3 | +The async client is built to be shared. This guide covers what the SDK |
| 4 | +guarantees when several tasks use it at once, and — just as important — |
| 5 | +what it cannot guarantee, because the Codesphere API offers no way to. |
| 6 | + |
| 7 | +## Share one client |
| 8 | + |
| 9 | +Create one `CodesphereSDK` and use it from as many tasks as you like. |
| 10 | +Sharing is preferred: each instance owns a connection pool, so creating |
| 11 | +one per task throws away connection reuse. |
| 12 | + |
| 13 | +```python |
| 14 | +import asyncio |
| 15 | +from codesphere import CodesphereSDK |
| 16 | + |
| 17 | +async def main(): |
| 18 | + async with CodesphereSDK() as sdk: |
| 19 | + teams = await sdk.teams.list() |
| 20 | + # One client, many concurrent calls. |
| 21 | + workspaces = await asyncio.gather( |
| 22 | + *(sdk.workspaces.list(team.id) for team in teams) |
| 23 | + ) |
| 24 | +``` |
| 25 | + |
| 26 | +Scopes are reference counted, so nested and concurrent `async with` |
| 27 | +blocks are safe — the transport closes when the last one exits, not the |
| 28 | +first: |
| 29 | + |
| 30 | +```python |
| 31 | +async def worker(sdk): |
| 32 | + async with sdk: # each worker holds its own scope |
| 33 | + await sdk.teams.list() |
| 34 | + |
| 35 | +async with CodesphereSDK() as sdk: |
| 36 | + await asyncio.gather(worker(sdk), worker(sdk)) |
| 37 | + await sdk.teams.list() # still open |
| 38 | +``` |
| 39 | + |
| 40 | +Using the client after it is fully closed raises |
| 41 | +[`ClientStateError`][codesphere.ClientStateError], which is both a |
| 42 | +`CodesphereError` and a `RuntimeError`. |
| 43 | + |
| 44 | +The synchronous client offers the same guarantees across threads. |
| 45 | + |
| 46 | +## Models go stale after a write |
| 47 | + |
| 48 | +The platform applies writes in the order they arrive; your program sees |
| 49 | +them in the order responses come back. Those orders can differ, so the |
| 50 | +SDK cannot know an entity's state after a write it did not read back. |
| 51 | +Rather than report a value the platform may not hold, a write |
| 52 | +**invalidates** the instance: |
| 53 | + |
| 54 | +```python |
| 55 | +workspace = await sdk.workspaces.get(72678) |
| 56 | + |
| 57 | +await workspace.update(WorkspaceUpdate(name="renamed")) |
| 58 | + |
| 59 | +workspace.id # fine: a write cannot change an entity's identity |
| 60 | +workspace.name # raises StaleModelError |
| 61 | +``` |
| 62 | + |
| 63 | +Call `refresh()` to re-read the server's actual state: |
| 64 | + |
| 65 | +```python |
| 66 | +await workspace.refresh() |
| 67 | +workspace.name # server-authoritative |
| 68 | +``` |
| 69 | + |
| 70 | +This applies to `Workspace.update()`, `Domain.update()`, |
| 71 | +`Domain.update_workspace_connections()` and `Domain.verify_status()`. |
| 72 | +`to_dict()`, `to_json()` and `to_yaml()` raise on a stale model too. |
| 73 | +The `Domain` methods return the server's response — prefer that returned |
| 74 | +object over re-reading the instance you called them on. |
| 75 | + |
| 76 | +## What the SDK cannot protect you from |
| 77 | + |
| 78 | +The workspace you are reading can be changed at the same moment by the |
| 79 | +web IDE, a CI pipeline, a teammate, or another process of your own. The |
| 80 | +Codesphere API has **no ETags, no `If-Match`, and no idempotency keys**, |
| 81 | +so there is no way for the SDK to detect or reject a conflicting write. |
| 82 | +These are real limitations, not oversights, and the SDK does not pretend |
| 83 | +otherwise. |
| 84 | + |
| 85 | +### Read-modify-write loses concurrent changes |
| 86 | + |
| 87 | +Any read, edit, write cycle has a window in which someone else's change |
| 88 | +is silently overwritten: |
| 89 | + |
| 90 | +```python |
| 91 | +# NOT safe against concurrent writers |
| 92 | +current = await workspace.env_vars.get() |
| 93 | +await workspace.env_vars.set([*current, EnvVar(name="NEW", value="1")]) |
| 94 | +``` |
| 95 | + |
| 96 | +`env_vars.set()` is a full replacement (`PUT`), so a variable added by |
| 97 | +someone else between the two calls is erased. The same applies to |
| 98 | +`landscape.get_profile()` → edit → `save_profile()`, which writes through |
| 99 | +a shell redirect and is not an atomic file replacement. |
| 100 | + |
| 101 | +If you must do this, narrow the window and verify afterwards by reading |
| 102 | +back. There is no way to make it atomic. |
| 103 | + |
| 104 | +### Retried writes can execute twice |
| 105 | + |
| 106 | +`PUT` and `DELETE` are idempotent as HTTP methods but not always in |
| 107 | +effect. If the platform receives a request and only the response is lost |
| 108 | +(a gateway `503`, a dropped connection), the retry runs the operation |
| 109 | +again — a landscape teardown can happen twice. Without idempotency keys |
| 110 | +the SDK cannot deduplicate this. |
| 111 | + |
| 112 | +The most common symptom is handled: a `404` on a **retried** `DELETE` is |
| 113 | +treated as success, since the resource being gone is what you asked for. |
| 114 | +A `404` on the first attempt still raises `NotFoundError`. |
| 115 | + |
| 116 | +If duplicate execution is unacceptable, disable retries for those calls: |
| 117 | + |
| 118 | +```python |
| 119 | +sdk = CodesphereSDK(retry=RetryConfig(max_retries=0)) |
| 120 | +``` |
| 121 | + |
| 122 | +### Waiting on a pipeline someone else restarted |
| 123 | + |
| 124 | +`wait_for_stage()` pins the run it is watching by its start time. If |
| 125 | +someone redeploys mid-wait, it raises |
| 126 | +[`ConflictError`][codesphere.ConflictError] instead of reporting the |
| 127 | +outcome of a run you never started: |
| 128 | + |
| 129 | +```python |
| 130 | +try: |
| 131 | + await workspace.landscape.wait_for_stage("run", timeout=600) |
| 132 | +except ConflictError: |
| 133 | + # Someone else redeployed. Decide whether to wait on the new run. |
| 134 | + ... |
| 135 | +``` |
| 136 | + |
| 137 | +Timeouts on `wait_for_stage()` and `wait_until_running()` are wall clock: |
| 138 | +time spent inside the status requests counts against your budget. |
| 139 | + |
| 140 | +## Log streams are single-use |
| 141 | + |
| 142 | +`logs.open()` returns a stream backed by one SSE response body, which can |
| 143 | +only be read once. Opening or iterating the same stream twice raises |
| 144 | +`ClientStateError`. Call `logs.open()` again for a second stream — |
| 145 | +concurrent streams over one client are fine: |
| 146 | + |
| 147 | +```python |
| 148 | +async def tail(target): |
| 149 | + async for entry in workspace.logs.stream(target): |
| 150 | + print(entry.message) |
| 151 | + |
| 152 | +await asyncio.gather( |
| 153 | + tail(ServerTarget(step=1, server="web")), |
| 154 | + tail(ServerTarget(step=1, server="api")), |
| 155 | +) |
| 156 | +``` |
| 157 | + |
| 158 | +## Feature flags |
| 159 | + |
| 160 | +The flags snapshot is fetched once per client and cached. Concurrent |
| 161 | +gated calls collapse into a single request, and `invalidate()` is honored |
| 162 | +even against a fetch that is already in flight. |
0 commit comments