Session Lifecycle
Source: qar-streaming-c/examples/session_lifecycle.c. API details: When the hub's session changes.
Session Lifecycle
What You Will Learn
- Read the session's lifecycle state with qar_session_get_lifecycle_state()
- Subscribe to its changes with qar_session_subscribe_lifecycle(), keep the callback short and react on the app's own thread
- Notice a move into a new session by a change of the session id, not by one particular state, and get or create what the app owns again
- Recover from QAR_SESSION_LIFECYCLE_STATE_RE_PAIR_REQUIRED: destroy the session, onboard again, recreate what the app owns
When It Matters
A session follows its hub by itself. When the hub's operator starts a new session, or the hub joins another hub as its member, the same QarSession moves into the new session and keeps everything the app created or took over with a get_or_create call: its app volumes, GUI panels and rooms are proposed again, senders renegotiate and frames pause for a few seconds. Objects of other peers disappear until their owners have moved too. Only the session id changes.
On each new session id the app still calls get_or_create again for every common name it owns. The calls are idempotent: each returns the same id, takes the object over when it is there (a previous run of the app may have left it) and re-creates it when it is missing. After onboarding again they are the only way the app's objects come back.
A hub reset to a new install, a revoked approval, or a peer that missed too many rotations ends the session for good: RE_PAIR_REQUIRED. Nothing is retried, and the app must onboard again.
Build and Run
While it runs (default 300 s), start a new session in the QAROS Visualizer to see a move, or revoke the app's approval to see RE_PAIR_REQUIRED and the new approval prompt.
Step 1: A Short Callback
The callback runs on a library thread. It copies the state into a mailbox and returns; info and its reason are valid only during the call. Never destroy the session or the library from it.
Step 2: Subscribe
The first callback carries the current state. The subscription belongs to its cancel token: destroying the token ends it and waits for a running callback.
Step 3: React on the App's Thread
- A move reports MIGRATING with the old id, then CONNECTED with the new id, the previous id and a cause. A move whose new session answers late reports HUB_UNREACHABLE with the new id first, and a callback that falls behind sees only the newest state. Compare the session id with the last one seen.
- The cause (ROTATED, JOINED_PRIMARY) is for logs only: the move is the same, and a join can be reported as ROTATED.
- HUB_UNREACHABLE alone needs nothing: the session keeps trying.
- On a new session id, get or create what the app owns again.
EnterCriticalSection(&mailbox.lock);const bool has_news = mailbox.has_news;const QarSessionLifecycleInfo info = mailbox.latest;char reason[sizeof(mailbox.reason)];memcpy(reason, mailbox.reason, sizeof(reason));mailbox.has_news = false;LeaveCriticalSection(&mailbox.lock);if(!has_news){continue;}printf("State %s%s%s\n", state_name(info.state), reason[0] != '\0' ? ": " : "", reason);if(memcmp(info.session_id.data, seen_id.data, sizeof(seen_id.data)) != 0){/* A new session: re-read whatever the app keyed by the session id,* and get or create what the app owns again (the same ids; a* missing object comes back). Handles and senders are still there. */seen_id = info.session_id;print_session_id("Now in session", &info.session_id);if(info.has_previous_session){print_session_id(" moved from", &info.previous_session_id);printf(" because %s\n", cause_name(info.cause));}&& !get_or_create_owned_objects(session)){printf("Could not get or create the app's volume in the new session.\n");}}
Step 4: Onboard Again After RE_PAIR_REQUIRED
Generated via doxygen2docusaurus 2.2.1 by Doxygen 1.9.8.