mirror of
https://github.com/unshackle-dl/unshackle.git
synced 2026-12-01 21:45:10 +00:00
fix(remote): end a remote session from before its create to after its DELETE
This commit is contained in:
@@ -246,7 +246,8 @@ hood the client walks a remote session through its lifecycle.
|
||||
[remote session channel](#the-remote-session-channel) keeps the remote session alive,
|
||||
so a long download does not let the remote session expire. On
|
||||
completion, and also when `dl` exits early (`--list`, Ctrl+C, an error), it
|
||||
deletes the remote session. If the login belongs to the client, the `DELETE`
|
||||
deletes the remote session (see [When the client ends a remote session](#when-the-client-ends-a-remote-session)).
|
||||
If the login belongs to the client, the `DELETE`
|
||||
request returns the updated cache files (for example a refreshed token) and the
|
||||
client saves them locally, so the **next** remote session can skip interactive
|
||||
authentication. The login belongs to the client when it succeeds and the
|
||||
@@ -480,7 +481,9 @@ The server mounts all these routes, even in `--remote-only` mode. Paths use the
|
||||
|
||||
### Create: request and response
|
||||
|
||||
`POST /api/session/create` requires `service` and `title_id`. It also accepts
|
||||
`POST /api/session/create` requires `service` and `title_id`. It also accepts `session_id` (the ID of the remote session,
|
||||
as the canonical text of a version 4 UUID; a create of the same API key and IP with the same ID replaces that remote
|
||||
session when the new one is stored, and the server answers the ID of another API key or IP with `403 FORBIDDEN`),
|
||||
`credentials`, `cookies` (base64 of zlib-compressed Netscape cookie file), `proxy`,
|
||||
`no_proxy`, `profile`, `cache` (a map of `filename → base64(zlib(bytes))`),
|
||||
`client_region`, `server_cdm` (`false` when the user picked the local CDM), `cdm_type`, `cdm_security_level`, `cdm_relay` (`true` when the client
|
||||
@@ -614,6 +617,8 @@ Source: `unshackle/core/api/session_store.py`. A singleton you get from
|
||||
- **Config-driven limits.** `serve.session_ttl` (default `300`) and
|
||||
`serve.max_sessions` (default `100`) are read as properties, so config changes
|
||||
take effect without recreating the store.
|
||||
- **`creating()`** holds the ID of a remote session while its `create` handler works, and refuses an ID that another API
|
||||
key or IP has. **`end_pending()`** ends such a create for a `DELETE`, and then `create()` stores nothing.
|
||||
- **`create()`** evicts the least-recently-accessed remote session of the API key that holds the most when at capacity,
|
||||
then stores a complete `SessionEntry` from the fields of the `create` handler and publishes the `create` event.
|
||||
- **Expiry.** `get()` and `cleanup_expired()` use one rule. A remote session that no request holds
|
||||
@@ -635,7 +640,7 @@ Source: `unshackle/core/api/session_store.py`. A singleton you get from
|
||||
|
||||
| Field | Description |
|
||||
|---|---|
|
||||
| `session_id` | UUID4 string |
|
||||
| `session_id` | UUID4 string: the one of the create request, else one that the server makes |
|
||||
| `service_tag` | Normalized service tag |
|
||||
| `service_instance` | The authenticated service object kept alive between calls |
|
||||
| `titles` / `title_map` | Result of `get_titles()` and a `title_id → Title` map |
|
||||
@@ -651,9 +656,12 @@ Source: `unshackle/core/api/session_store.py`. A singleton you get from
|
||||
The `create` handler builds a `Cacher` namespaced as:
|
||||
|
||||
```text
|
||||
_sessions/<pbkdf2_hmac(sha256, X-Secret-Key, "unshackle-session-ns", 100000)[:12]>/<session_id>/<service>
|
||||
_sessions/<pbkdf2_hmac(sha256, X-Secret-Key, "unshackle-session-ns", 100000)[:12]>/<uuid4>/<service>
|
||||
```
|
||||
|
||||
The server makes a new UUID for each `create`, so a `create` that takes over a session ID never shares a directory
|
||||
with the one before it.
|
||||
|
||||
The handler writes forwarded `cache` files into that directory before authentication.
|
||||
On `DELETE`, when the login belongs to the client (`client_auth`) and the remote
|
||||
session does not run on the server's device, the handler harvests updated cache files from the whole directory tree (compressing each
|
||||
@@ -732,10 +740,12 @@ the error codes and the request and reply shapes that it shares with the server
|
||||
(merging returned `session_headers`/`session_cookie_jar`, re-parsing `manifests`) →
|
||||
get the licence in proxy mode or `server_cdm` mode → `close()` (`DELETE`, saving any returned
|
||||
`cache`).
|
||||
- **ID.** The client gives the remote session its ID: a version 4 UUID in the `session_id` of `create`. It uses
|
||||
the ID of the reply, so a server that ignores the field also works.
|
||||
- **Server restarts.** The server keeps remote sessions in memory only, so a restart
|
||||
loses them all. The client recovers in two ways:
|
||||
- A drain announced through `draining`: the client finishes the current title, then
|
||||
closes its remote session, so the drain can end and the refreshed cache comes back.
|
||||
deletes its remote session, so the drain can end and the refreshed cache comes back.
|
||||
It waits until `/api/health` reports the restarted server without `draining`,
|
||||
opens a new remote session, and continues with the next title.
|
||||
- A lost remote session (`SESSION_NOT_FOUND`, a connection error, an answer cut off
|
||||
@@ -743,7 +753,8 @@ the error codes and the request and reply shapes that it shares with the server
|
||||
for `/api/health`, then checks the old remote session. A live remote session takes the
|
||||
call again only after a connection error, because a timed-out call can already have run.
|
||||
A cut-off answer counts as a connection error, so the server can run that call twice. A lost one is replaced: the
|
||||
client opens a new remote session, sends `titles` and the current title's `tracks`
|
||||
client deletes the old one when the server did not answer `SESSION_NOT_FOUND` for the call, and
|
||||
saves the cache that comes back. It then opens a new remote session, sends `titles` and the current title's `tracks`
|
||||
again, and then sends the failed call again. A title or track that the new remote session
|
||||
gives another ID keeps its old ID on the client, and the client sends the new ID. The client matches a title by season and episode number, and a track by its
|
||||
attributes.
|
||||
@@ -760,6 +771,31 @@ the error codes and the request and reply shapes that it shares with the server
|
||||
with no `error_code`. Any other error stops the call: a segment filter that fails on the
|
||||
server stops the track download, and the client never keeps every segment in its place.
|
||||
|
||||
### When the client ends a remote session
|
||||
|
||||
Source: `unshackle/core/remote_lifetime.py`. One owner holds the remote session, its tunnel and its channel
|
||||
from before the first request until the server answers the `DELETE`. It closes the channel, sends the `DELETE`,
|
||||
saves the returned cache and then stops the tunnel. It does so:
|
||||
|
||||
- when `dl` has finished every title, before the run post-scripts
|
||||
- when the process exits: `--list`, an error, or Ctrl+C in any step, also before `create` answers
|
||||
- when the login or any other step of the opening fails, or `create` times out or its answer is cut off
|
||||
- when a serve that relays for a REST client closes its pooled remote session
|
||||
|
||||
The `DELETE` has a connect and read timeout of 15s each. The client sends it again only once, when a gateway answers with
|
||||
an error page or asks for the API key in the other header. It does not wait for a rate limit. A process that ends without its exit hooks (`SIGHUP`,
|
||||
`SIGTERM`, `SIGKILL`, a closed console window) sends no `DELETE`, so the remote session stays until its TTL. Ctrl+C
|
||||
waits for the download threads before it ends the remote session.
|
||||
|
||||
While the client ends a remote session, it ignores the first Ctrl+C, so it does not lose the returned cache. The next
|
||||
Ctrl+C stops the wait at once, and the process then waits for no other end. The count of Ctrl+C is one for the process.
|
||||
When the run itself called the end, after the last title, that Ctrl+C also stops the run. On Windows, a Ctrl+C
|
||||
counts when the network read that blocks returns, so the 15s timeout limits the wait. After the end, no thread of the
|
||||
client opens a remote session again.
|
||||
|
||||
The `DELETE` can arrive while the server still works on `create`. The server then answers `200` with no cache, and
|
||||
the `create` makes no remote session, starts no login and answers `404 SESSION_NOT_FOUND`.
|
||||
|
||||
---
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
Reference in New Issue
Block a user