+
+ /*
+ * If a rotation is ongoing, use a copy of the
+ * relay stream's chunk to ensure the stream
+ * files exist.
+ *
+ * Otherwise, the viewer session's current trace
+ * chunk can be used safely.
+ */
+ if ((relay_stream->ongoing_rotation.is_set ||
+ relay_session->ongoing_rotation) &&
+ relay_stream->trace_chunk) {
+ viewer_stream_trace_chunk = lttng_trace_chunk_copy(
+ relay_stream->trace_chunk);
+ if (!viewer_stream_trace_chunk) {
+ ret = -1;
+ ctf_trace_put(ctf_trace);
+ goto error_unlock;
+ }
+ } else {
+ bool reference_acquired;
+
+ /*
+ * Transition the viewer session into the newest trace chunk available.
+ */
+ if (!lttng_trace_chunk_ids_equal(viewer_session->current_trace_chunk,
+ relay_stream->trace_chunk)) {
+
+ ret = viewer_session_set_trace_chunk_copy(
+ viewer_session,
+ relay_stream->trace_chunk);
+ if (ret) {
+ ret = -1;
+ ctf_trace_put(ctf_trace);
+ goto error_unlock;
+ }
+ }
+
+ reference_acquired = lttng_trace_chunk_get(
+ viewer_session->current_trace_chunk);
+ assert(reference_acquired);
+ viewer_stream_trace_chunk =
+ viewer_session->current_trace_chunk;
+ }
+
+ viewer_stream = viewer_stream_create(
+ relay_stream,
+ viewer_stream_trace_chunk,
+ seek_t);
+ lttng_trace_chunk_put(viewer_stream_trace_chunk);
+ viewer_stream_trace_chunk = NULL;
+ if (!viewer_stream) {