Skip to main content
Version: Dev (main)

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​

 cmake --build --preset x64-windows-debug --target session_lifecycle
 ./build/x64-windows/Debug/session_lifecycle.exe [run-seconds]

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.

 /** \brief The newest lifecycle state, handed from the library thread to main. */
 typedef struct LifecycleMailbox
 {
  CRITICAL_SECTION lock;
  bool has_news;
  QarSessionLifecycleInfo latest; /* `reason` is not kept: see below */
  char reason[256];
 
 static void
 on_lifecycle(const QarSessionLifecycleInfo* info, void* user_state)
 {
  LifecycleMailbox* mailbox = (LifecycleMailbox*)user_state;
  EnterCriticalSection(&mailbox->lock);
  mailbox->latest = *info;
  mailbox->latest.reason = NULL; /* valid only during this callback */
  snprintf(
  mailbox->reason, sizeof(mailbox->reason), "%s", info->reason != NULL ? info->reason : ""
  );
  mailbox->has_news = true;
  LeaveCriticalSection(&mailbox->lock);
 }

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.

 /** \brief Subscribe to the session's lifecycle. Returns the token owning the
  * subscription, or NULL on failure. */
 static QarCancelToken*
 {
  QarCancelToken* token = NULL;
  QarResult created = qar_cancel_token_create(&token);
  if(qar_result_is_error(created) || token == NULL)
  {
  log_result("qar_cancel_token_create", created);
  return NULL;
  }
  QarResult subscribed = qar_session_subscribe_lifecycle(session, on_lifecycle, mailbox, token);
  if(qar_result_is_error(subscribed))
  {
  log_result("qar_session_subscribe_lifecycle", subscribed);
  return NULL;
  }
  return token;
 }

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);
      {
      print_session_id(" moved from", &info.previous_session_id);
      printf(" because %s\n", cause_name(info.cause));
      }
      {
      printf("Could not get or create the app's volume in the new session.\n");
      }
      }

Step 4: Onboard Again After RE_PAIR_REQUIRED​

  {
  /* Terminal: nothing is retried. Leave the old session, join again
  * (the user may be asked to approve the app again) and recreate
  * what the app owns. Here, on the app's thread, never in the
  * callback. A hub that keeps refusing is not asked forever. */
  printf("The hub no longer admits this app. Joining again ...\n");
  stop_session(session, token, &mailbox);
  session = NULL;
  if(++joins_again > 3)
  {
  printf("Still refused after 3 joins; giving up.\n");
  exit_code = 6;
  }
  else if(!start_session(runtime, &mailbox, &session, &token, &seen_id))
  {
  exit_code = 6;
  }
  }

Generated via doxygen2docusaurus 2.2.1 by Doxygen 1.9.8.