• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

realm / realm-core / 1861

23 Nov 2023 10:15PM UTC coverage: 91.708% (+0.02%) from 91.692%
1861

push

Evergreen

web-flow
Update baas with the fix for "Test client migration and rollback with recovery" test (#7164)

92426 of 169288 branches covered (0.0%)

231763 of 252718 relevant lines covered (91.71%)

6792444.72 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.93
/src/realm/sync/client.cpp
1

2
#include <memory>
3
#include <tuple>
4
#include <atomic>
5

6
#include "realm/sync/client_base.hpp"
7
#include "realm/sync/protocol.hpp"
8
#include "realm/util/optional.hpp"
9
#include <realm/sync/client.hpp>
10
#include <realm/sync/config.hpp>
11
#include <realm/sync/noinst/client_reset.hpp>
12
#include <realm/sync/noinst/client_history_impl.hpp>
13
#include <realm/sync/noinst/client_impl_base.hpp>
14
#include <realm/sync/noinst/pending_bootstrap_store.hpp>
15
#include <realm/sync/subscriptions.hpp>
16
#include <realm/util/bind_ptr.hpp>
17
#include <realm/util/circular_buffer.hpp>
18
#include <realm/util/platform_info.hpp>
19
#include <realm/util/thread.hpp>
20
#include <realm/util/uri.hpp>
21
#include <realm/util/value_reset_guard.hpp>
22
#include <realm/version.hpp>
23

24
namespace realm {
25
namespace sync {
26

27
namespace {
28
using namespace realm::util;
29

30

31
// clang-format off
32
using SessionImpl                     = ClientImpl::Session;
33
using SyncTransactCallback            = Session::SyncTransactCallback;
34
using ProgressHandler                 = Session::ProgressHandler;
35
using WaitOperCompletionHandler       = Session::WaitOperCompletionHandler;
36
using ConnectionStateChangeListener   = Session::ConnectionStateChangeListener;
37
using port_type                       = Session::port_type;
38
using connection_ident_type           = std::int_fast64_t;
39
using ProxyConfig                     = SyncConfig::ProxyConfig;
40
// clang-format on
41

42
} // unnamed namespace
43

44

45
// Life cycle states of a session wrapper:
46
//
47
//  - Uninitiated
48
//  - Unactualized
49
//  - Actualized
50
//  - Finalized
51
//
52
// The session wrapper moves from the Uninitiated to the Unactualized state when
53
// it is initiated, i.e., when initiate() is called. This may happen on any
54
// thread.
55
//
56
// The session wrapper moves from the Unactualized to the Actualized state when
57
// it is associated with a session object, i.e., when `m_sess` is made to refer
58
// to an object of type SessionImpl. This always happens on the event loop
59
// thread.
60
//
61
// The session wrapper moves from the Actualized to the Finalized state when it
62
// is dissociated from the session object. This happens in response to the
63
// session wrapper having been abandoned by the application. This always happens
64
// on the event loop thread.
65
//
66
// The session wrapper will exist in the Finalized state only while referenced
67
// from a post handler waiting to be executed.
68
//
69
// If the session wrapper is abandoned by the application while in the
70
// Uninitiated state, it will be destroyed immediately, since no post handlers
71
// can have been scheduled prior to initiation.
72
//
73
// If the session wrapper is abandoned while in the Unactivated state, it will
74
// move immediately to the Finalized state. This may happen on any thread.
75
//
76
// The moving of a session wrapper to, or from the Actualized state always
77
// happen on the event loop thread. All other state transitions may happen on
78
// any thread.
79
//
80
// NOTE: Activation of the session happens no later than during actualization,
81
// and initiation of deactivation happens no earlier than during
82
// finalization. See also activate_session() and initiate_session_deactivation()
83
// in ClientImpl::Connection.
84
class SessionWrapper final : public util::AtomicRefCountBase, DB::CommitListener {
85
public:
86
    SessionWrapper(ClientImpl&, DBRef db, std::shared_ptr<SubscriptionStore>, std::shared_ptr<MigrationStore>,
87
                   Session::Config);
88
    ~SessionWrapper() noexcept;
89

90
    ClientReplication& get_replication() noexcept;
91
    ClientImpl& get_client() noexcept;
92

93
    bool has_flx_subscription_store() const;
94
    SubscriptionStore* get_flx_subscription_store();
95
    PendingBootstrapStore* get_flx_pending_bootstrap_store();
96

97
    MigrationStore* get_migration_store();
98

99
    void set_progress_handler(util::UniqueFunction<ProgressHandler>);
100
    void set_connection_state_change_listener(util::UniqueFunction<ConnectionStateChangeListener>);
101

102
    void initiate();
103

104
    void force_close();
105

106
    void on_commit(version_type new_version) override;
107
    void cancel_reconnect_delay();
108

109
    void async_wait_for(bool upload_completion, bool download_completion, WaitOperCompletionHandler);
110
    bool wait_for_upload_complete_or_client_stopped();
111
    bool wait_for_download_complete_or_client_stopped();
112

113
    void refresh(std::string signed_access_token);
114

115
    static void abandon(util::bind_ptr<SessionWrapper>) noexcept;
116

117
    // These are called from ClientImpl
118
    void actualize(ServerEndpoint);
119
    void finalize();
120
    void finalize_before_actualization() noexcept;
121

122
    util::Future<std::string> send_test_command(std::string body);
123

124
    void handle_pending_client_reset_acknowledgement();
125

126
    void update_subscription_version_info();
127

128
    std::string get_appservices_connection_id();
129

130
private:
131
    ClientImpl& m_client;
132
    DBRef m_db;
133
    Replication* m_replication;
134

135
    const ProtocolEnvelope m_protocol_envelope;
136
    const std::string m_server_address;
137
    const port_type m_server_port;
138
    const std::string m_user_id;
139
    const SyncServerMode m_sync_mode;
140
    const std::string m_authorization_header_name;
141
    const std::map<std::string, std::string> m_custom_http_headers;
142
    const bool m_verify_servers_ssl_certificate;
143
    const bool m_simulate_integration_error;
144
    const Optional<std::string> m_ssl_trust_certificate_path;
145
    const std::function<SyncConfig::SSLVerifyCallback> m_ssl_verify_callback;
146
    const size_t m_flx_bootstrap_batch_size_bytes;
147

148
    // This one is different from null when, and only when the session wrapper
149
    // is in ClientImpl::m_abandoned_session_wrappers.
150
    SessionWrapper* m_next = nullptr;
151

152
    // After initiation, these may only be accessed by the event loop thread.
153
    std::string m_http_request_path_prefix;
154
    std::string m_virt_path;
155
    std::string m_signed_access_token;
156

157
    util::Optional<ClientReset> m_client_reset_config;
158

159
    util::Optional<ProxyConfig> m_proxy_config;
160

161
    uint_fast64_t m_last_reported_uploadable_bytes = 0;
162
    util::UniqueFunction<ProgressHandler> m_progress_handler;
163
    util::UniqueFunction<ConnectionStateChangeListener> m_connection_state_change_listener;
164

165
    std::function<SyncClientHookAction(SyncClientHookData data)> m_debug_hook;
166
    bool m_in_debug_hook = false;
167

168
    SessionReason m_session_reason;
169

170
    std::shared_ptr<SubscriptionStore> m_flx_subscription_store;
171
    int64_t m_flx_active_version = 0;
172
    int64_t m_flx_last_seen_version = 0;
173
    int64_t m_flx_pending_mark_version = 0;
174
    std::unique_ptr<PendingBootstrapStore> m_flx_pending_bootstrap_store;
175

176
    std::shared_ptr<MigrationStore> m_migration_store;
177

178
    bool m_initiated = false;
179

180
    // Set to true when this session wrapper is actualized (or when it is
181
    // finalized before proper actualization). It is then never modified again.
182
    //
183
    // A session specific post handler submitted after the initiation of the
184
    // session wrapper (initiate()) will always find that `m_actualized` is
185
    // true. This is the case, because the scheduling of such a post handler
186
    // will have been preceded by the triggering of
187
    // `ClientImpl::m_actualize_and_finalize` (in
188
    // ClientImpl::register_unactualized_session_wrapper()), which ensures that
189
    // ClientImpl::actualize_and_finalize_session_wrappers() gets to execute
190
    // before the post handler. If the session wrapper is no longer in
191
    // `ClientImpl::m_unactualized_session_wrappers` when
192
    // ClientImpl::actualize_and_finalize_session_wrappers() executes, it must
193
    // have been abandoned already, but in that case,
194
    // finalize_before_actualization() has already been called.
195
    bool m_actualized = false;
196

197
    bool m_force_closed = false;
198

199
    bool m_suspended = false;
200

201
    // Has the SessionWrapper been finalized?
202
    bool m_finalized = false;
203

204
    // Set to true when the first DOWNLOAD message is received to indicate that
205
    // the byte-level download progress parameters can be considered reasonable
206
    // reliable. Before that, a lot of time may have passed, so our record of
207
    // the download progress is likely completely out of date.
208
    bool m_reliable_download_progress = false;
209

210
    // Set to point to an activated session object during actualization of the
211
    // session wrapper. Set to null during finalization of the session
212
    // wrapper. Both modifications are guaranteed to be performed by the event
213
    // loop thread.
214
    //
215
    // If a session specific post handler, that is submitted after the
216
    // initiation of the session wrapper, sees that `m_sess` is null, it can
217
    // conclude that the session wrapper has been both abandoned and
218
    // finalized. This is true, because the scheduling of such a post handler
219
    // will have been preceded by the triggering of
220
    // `ClientImpl::m_actualize_and_finalize` (in
221
    // ClientImpl::register_unactualized_session_wrapper()), which ensures that
222
    // ClientImpl::actualize_and_finalize_session_wrappers() gets to execute
223
    // before the post handler, so the session wrapper must have been actualized
224
    // unless it was already abandoned by the application. If it was abandoned
225
    // before it was actualized, it will already have been finalized by
226
    // finalize_before_actualization().
227
    //
228
    // Must only be accessed from the event loop thread.
229
    SessionImpl* m_sess = nullptr;
230

231
    // These must only be accessed from the event loop thread.
232
    std::vector<WaitOperCompletionHandler> m_upload_completion_handlers;
233
    std::vector<WaitOperCompletionHandler> m_download_completion_handlers;
234
    std::vector<WaitOperCompletionHandler> m_sync_completion_handlers;
235

236
    // `m_target_*load_mark` and `m_reached_*load_mark` are protected by
237
    // `m_client.m_mutex`. `m_staged_*load_mark` must only be accessed by the
238
    // event loop thread.
239
    std::int_fast64_t m_target_upload_mark = 0, m_target_download_mark = 0;
240
    std::int_fast64_t m_staged_upload_mark = 0, m_staged_download_mark = 0;
241
    std::int_fast64_t m_reached_upload_mark = 0, m_reached_download_mark = 0;
242

243
    void on_sync_progress();
244
    void on_upload_completion();
245
    void on_download_completion();
246
    void on_suspended(const SessionErrorInfo& error_info);
247
    void on_resumed();
248
    void on_connection_state_changed(ConnectionState, const util::Optional<SessionErrorInfo>&);
249
    void on_flx_sync_progress(int64_t new_version, DownloadBatchState batch_state);
250
    void on_flx_sync_error(int64_t version, std::string_view err_msg);
251
    void on_flx_sync_version_complete(int64_t version);
252

253
    void report_progress(bool only_if_new_uploadable_data = false);
254

255
    friend class SessionWrapperStack;
256
    friend class ClientImpl::Session;
257
};
258

259

260
// ################ SessionWrapperStack ################
261

262
inline bool SessionWrapperStack::empty() const noexcept
263
{
×
264
    return !m_back;
×
265
}
×
266

267

268
inline void SessionWrapperStack::push(util::bind_ptr<SessionWrapper> w) noexcept
269
{
9,710✔
270
    REALM_ASSERT(!w->m_next);
9,710✔
271
    w->m_next = m_back;
9,710✔
272
    m_back = w.release();
9,710✔
273
}
9,710✔
274

275

276
inline util::bind_ptr<SessionWrapper> SessionWrapperStack::pop() noexcept
277
{
23,952✔
278
    util::bind_ptr<SessionWrapper> w{m_back, util::bind_ptr_base::adopt_tag{}};
23,952✔
279
    if (m_back) {
23,952✔
280
        m_back = m_back->m_next;
9,708✔
281
        w->m_next = nullptr;
9,708✔
282
    }
9,708✔
283
    return w;
23,952✔
284
}
23,952✔
285

286

287
inline void SessionWrapperStack::clear() noexcept
288
{
23,720✔
289
    while (m_back) {
23,720✔
290
        util::bind_ptr<SessionWrapper> w{m_back, util::bind_ptr_base::adopt_tag{}};
×
291
        m_back = w->m_next;
×
292
    }
×
293
}
23,720✔
294

295

296
inline SessionWrapperStack::SessionWrapperStack(SessionWrapperStack&& q) noexcept
297
    : m_back{q.m_back}
298
{
299
    q.m_back = nullptr;
300
}
301

302

303
SessionWrapperStack::~SessionWrapperStack()
304
{
23,720✔
305
    clear();
23,720✔
306
}
23,720✔
307

308

309
// ################ ClientImpl ################
310

311

312
ClientImpl::~ClientImpl()
313
{
9,482✔
314
    // Since no other thread is allowed to be accessing this client or any of
4,672✔
315
    // its subobjects at this time, no mutex locking is necessary.
4,672✔
316

4,672✔
317
    shutdown_and_wait();
9,482✔
318
    // Session wrappers are removed from m_unactualized_session_wrappers as they
4,672✔
319
    // are abandoned.
4,672✔
320
    REALM_ASSERT(m_stopped);
9,482✔
321
    REALM_ASSERT(m_unactualized_session_wrappers.empty());
9,482✔
322
}
9,482✔
323

324

325
void ClientImpl::cancel_reconnect_delay()
326
{
1,536✔
327
    // Thread safety required
862✔
328
    post([this](Status status) {
1,536✔
329
        if (status == ErrorCodes::OperationAborted)
1,536✔
330
            return;
×
331
        else if (!status.is_ok())
1,536✔
332
            throw Exception(status);
×
333

862✔
334
        for (auto& p : m_server_slots) {
1,536✔
335
            ServerSlot& slot = p.second;
1,536✔
336
            if (m_one_connection_per_session) {
1,536✔
337
                REALM_ASSERT(!slot.connection);
×
338
                for (const auto& p : slot.alt_connections) {
×
339
                    ClientImpl::Connection& conn = *p.second;
×
340
                    conn.resume_active_sessions(); // Throws
×
341
                    conn.cancel_reconnect_delay(); // Throws
×
342
                }
×
343
            }
×
344
            else {
1,536✔
345
                REALM_ASSERT(slot.alt_connections.empty());
1,536✔
346
                if (slot.connection) {
1,536✔
347
                    ClientImpl::Connection& conn = *slot.connection;
1,532✔
348
                    conn.resume_active_sessions(); // Throws
1,532✔
349
                    conn.cancel_reconnect_delay(); // Throws
1,532✔
350
                }
1,532✔
351
                else {
4✔
352
                    slot.reconnect_info.reset();
4✔
353
                }
4✔
354
            }
1,536✔
355
        }
1,536✔
356
    }); // Throws
1,536✔
357
}
1,536✔
358

359

360
void ClientImpl::voluntary_disconnect_all_connections()
361
{
12✔
362
    auto done_pf = util::make_promise_future<void>();
12✔
363
    post([this, promise = std::move(done_pf.promise)](Status status) mutable {
12✔
364
        if (status == ErrorCodes::OperationAborted) {
12✔
365
            return;
×
366
        }
×
367

6✔
368
        REALM_ASSERT(status.is_ok());
12✔
369

6✔
370
        try {
12✔
371
            for (auto& p : m_server_slots) {
12✔
372
                ServerSlot& slot = p.second;
12✔
373
                if (m_one_connection_per_session) {
12✔
374
                    REALM_ASSERT(!slot.connection);
×
375
                    for (const auto& p : slot.alt_connections) {
×
376
                        ClientImpl::Connection& conn = *p.second;
×
377
                        if (conn.get_state() == ConnectionState::disconnected) {
×
378
                            continue;
×
379
                        }
×
380
                        conn.voluntary_disconnect();
×
381
                    }
×
382
                }
×
383
                else {
12✔
384
                    REALM_ASSERT(slot.alt_connections.empty());
12✔
385
                    if (!slot.connection) {
12✔
386
                        continue;
×
387
                    }
×
388
                    ClientImpl::Connection& conn = *slot.connection;
12✔
389
                    if (conn.get_state() == ConnectionState::disconnected) {
12✔
390
                        continue;
×
391
                    }
×
392
                    conn.voluntary_disconnect();
12✔
393
                }
12✔
394
            }
12✔
395
        }
12✔
396
        catch (...) {
6✔
397
            promise.set_error(exception_to_status());
×
398
            return;
×
399
        }
×
400
        promise.emplace_value();
12✔
401
    });
12✔
402
    done_pf.future.get();
12✔
403
}
12✔
404

405

406
bool ClientImpl::wait_for_session_terminations_or_client_stopped()
407
{
372✔
408
    // Thread safety required
34✔
409

34✔
410
    {
372✔
411
        std::lock_guard lock{m_mutex};
372✔
412
        m_sessions_terminated = false;
372✔
413
    }
372✔
414

34✔
415
    // The technique employed here relies on the fact that
34✔
416
    // actualize_and_finalize_session_wrappers() must get to execute at least
34✔
417
    // once before the post handler submitted below gets to execute, but still
34✔
418
    // at a time where all session wrappers, that are abandoned prior to the
34✔
419
    // execution of wait_for_session_terminations_or_client_stopped(), have been
34✔
420
    // added to `m_abandoned_session_wrappers`.
34✔
421
    //
34✔
422
    // To see that this is the case, consider a session wrapper that was
34✔
423
    // abandoned before wait_for_session_terminations_or_client_stopped() was
34✔
424
    // invoked. Then the session wrapper will have been added to
34✔
425
    // `m_abandoned_session_wrappers`, and an invocation of
34✔
426
    // actualize_and_finalize_session_wrappers() will have been scheduled. The
34✔
427
    // guarantees mentioned in the documentation of Trigger then ensure
34✔
428
    // that at least one execution of actualize_and_finalize_session_wrappers()
34✔
429
    // will happen after the session wrapper has been added to
34✔
430
    // `m_abandoned_session_wrappers`, but before the post handler submitted
34✔
431
    // below gets to execute.
34✔
432
    post([this](Status status) mutable {
372✔
433
        if (status == ErrorCodes::OperationAborted)
372✔
434
            return;
×
435
        else if (!status.is_ok())
372✔
436
            throw Exception(status);
×
437

34✔
438
        std::lock_guard lock{m_mutex};
372✔
439
        m_sessions_terminated = true;
372✔
440
        m_wait_or_client_stopped_cond.notify_all();
372✔
441
    }); // Throws
372✔
442

34✔
443
    bool completion_condition_was_satisfied;
372✔
444
    {
372✔
445
        std::unique_lock lock{m_mutex};
372✔
446
        while (!m_sessions_terminated && !m_stopped)
710✔
447
            m_wait_or_client_stopped_cond.wait(lock);
338✔
448
        completion_condition_was_satisfied = !m_stopped;
372✔
449
    }
372✔
450
    return completion_condition_was_satisfied;
372✔
451
}
372✔
452

453

454
void ClientImpl::drain_connections_on_loop()
455
{
9,482✔
456
    post([this](Status status) mutable {
9,482✔
457
        REALM_ASSERT(status.is_ok());
9,482✔
458
        drain_connections();
9,482✔
459
    });
9,482✔
460
}
9,482✔
461

462
void ClientImpl::shutdown_and_wait()
463
{
10,242✔
464
    shutdown();
10,242✔
465
    std::unique_lock lock{m_drain_mutex};
10,242✔
466
    if (m_drained) {
10,242✔
467
        return;
756✔
468
    }
756✔
469

4,674✔
470
    logger.debug("Waiting for %1 connections to drain", m_num_connections);
9,486✔
471
    m_drain_cv.wait(lock, [&] {
20,604✔
472
        return m_num_connections == 0 && m_outstanding_posts == 0;
20,604✔
473
    });
20,604✔
474

4,674✔
475
    m_drained = true;
9,486✔
476
}
9,486✔
477

478
void ClientImpl::shutdown() noexcept
479
{
19,802✔
480
    {
19,802✔
481
        std::lock_guard lock{m_mutex};
19,802✔
482
        if (m_stopped)
19,802✔
483
            return;
10,320✔
484
        m_stopped = true;
9,482✔
485
        m_wait_or_client_stopped_cond.notify_all();
9,482✔
486
    }
9,482✔
487

4,672✔
488
    drain_connections_on_loop();
9,482✔
489
}
9,482✔
490

491

492
void ClientImpl::register_unactualized_session_wrapper(SessionWrapper* wrapper, ServerEndpoint endpoint)
493
{
9,876✔
494
    // Thread safety required.
4,766✔
495

4,766✔
496
    std::lock_guard lock{m_mutex};
9,876✔
497
    REALM_ASSERT(m_actualize_and_finalize);
9,876✔
498
    m_unactualized_session_wrappers.emplace(wrapper, std::move(endpoint)); // Throws
9,876✔
499
    bool retrigger = !m_actualize_and_finalize_needed;
9,876✔
500
    m_actualize_and_finalize_needed = true;
9,876✔
501
    // The conditional triggering needs to happen before releasing the mutex,
4,766✔
502
    // because if two threads call register_unactualized_session_wrapper()
4,766✔
503
    // roughly concurrently, then only the first one is guaranteed to be asked
4,766✔
504
    // to retrigger, but that retriggering must have happened before the other
4,766✔
505
    // thread returns from register_unactualized_session_wrapper().
4,766✔
506
    //
4,766✔
507
    // Note that a similar argument applies when two threads call
4,766✔
508
    // register_abandoned_session_wrapper(), and when one thread calls one of
4,766✔
509
    // them and another thread call the other.
4,766✔
510
    if (retrigger)
9,876✔
511
        m_actualize_and_finalize->trigger();
5,262✔
512
}
9,876✔
513

514

515
void ClientImpl::register_abandoned_session_wrapper(util::bind_ptr<SessionWrapper> wrapper) noexcept
516
{
9,876✔
517
    // Thread safety required.
4,766✔
518

4,766✔
519
    std::lock_guard lock{m_mutex};
9,876✔
520
    REALM_ASSERT(m_actualize_and_finalize);
9,876✔
521

4,766✔
522
    // If the session wrapper has not yet been actualized (on the event loop
4,766✔
523
    // thread), it can be immediately finalized. This ensures that we will
4,766✔
524
    // generally not actualize a session wrapper that has already been
4,766✔
525
    // abandoned.
4,766✔
526
    auto i = m_unactualized_session_wrappers.find(wrapper.get());
9,876✔
527
    if (i != m_unactualized_session_wrappers.end()) {
9,876✔
528
        m_unactualized_session_wrappers.erase(i);
166✔
529
        wrapper->finalize_before_actualization();
166✔
530
        return;
166✔
531
    }
166✔
532
    m_abandoned_session_wrappers.push(std::move(wrapper));
9,710✔
533
    bool retrigger = !m_actualize_and_finalize_needed;
9,710✔
534
    m_actualize_and_finalize_needed = true;
9,710✔
535
    // The conditional triggering needs to happen before releasing the
4,680✔
536
    // mutex. See implementation of register_unactualized_session_wrapper() for
4,680✔
537
    // details.
4,680✔
538
    if (retrigger)
9,710✔
539
        m_actualize_and_finalize->trigger();
8,980✔
540
}
9,710✔
541

542

543
// Must be called from the event loop thread.
544
void ClientImpl::actualize_and_finalize_session_wrappers()
545
{
14,240✔
546
    std::map<SessionWrapper*, ServerEndpoint> unactualized_session_wrappers;
14,240✔
547
    SessionWrapperStack abandoned_session_wrappers;
14,240✔
548
    bool stopped;
14,240✔
549
    {
14,240✔
550
        std::lock_guard lock{m_mutex};
14,240✔
551
        m_actualize_and_finalize_needed = false;
14,240✔
552
        swap(m_unactualized_session_wrappers, unactualized_session_wrappers);
14,240✔
553
        swap(m_abandoned_session_wrappers, abandoned_session_wrappers);
14,240✔
554
        stopped = m_stopped;
14,240✔
555
    }
14,240✔
556
    // Note, we need to finalize old session wrappers before we actualize new
6,468✔
557
    // ones. This ensures that deactivation of old sessions is initiated before
6,468✔
558
    // new session are activated. This, in turn, ensures that the server does
6,468✔
559
    // not see two overlapping sessions for the same local Realm file.
6,468✔
560
    while (util::bind_ptr<SessionWrapper> wrapper = abandoned_session_wrappers.pop())
23,948✔
561
        wrapper->finalize(); // Throws
9,708✔
562
    if (stopped) {
14,240✔
563
        for (auto& p : unactualized_session_wrappers) {
406✔
564
            SessionWrapper& wrapper = *p.first;
6✔
565
            wrapper.finalize_before_actualization();
6✔
566
        }
6✔
567
        return;
766✔
568
    }
766✔
569
    for (auto& p : unactualized_session_wrappers) {
13,474✔
570
        SessionWrapper& wrapper = *p.first;
9,704✔
571
        ServerEndpoint server_endpoint = std::move(p.second);
9,704✔
572
        wrapper.actualize(std::move(server_endpoint)); // Throws
9,704✔
573
    }
9,704✔
574
}
13,474✔
575

576

577
ClientImpl::Connection& ClientImpl::get_connection(ServerEndpoint endpoint,
578
                                                   const std::string& authorization_header_name,
579
                                                   const std::map<std::string, std::string>& custom_http_headers,
580
                                                   bool verify_servers_ssl_certificate,
581
                                                   Optional<std::string> ssl_trust_certificate_path,
582
                                                   std::function<SyncConfig::SSLVerifyCallback> ssl_verify_callback,
583
                                                   Optional<ProxyConfig> proxy_config, bool& was_created)
584
{
9,700✔
585
    auto&& [server_slot_it, inserted] =
9,700✔
586
        m_server_slots.try_emplace(endpoint, ReconnectInfo(m_reconnect_mode, m_reconnect_backoff_info, get_random()));
9,700✔
587
    ServerSlot& server_slot = server_slot_it->second; // Throws
9,700✔
588

4,676✔
589
    // TODO: enable multiplexing with proxies
4,676✔
590
    if (server_slot.connection && !m_one_connection_per_session && !proxy_config) {
9,700✔
591
        // Use preexisting connection
3,486✔
592
        REALM_ASSERT(server_slot.alt_connections.empty());
7,182✔
593
        return *server_slot.connection;
7,182✔
594
    }
7,182✔
595

1,190✔
596
    // Create a new connection
1,190✔
597
    REALM_ASSERT(!server_slot.connection);
2,518✔
598
    connection_ident_type ident = m_prev_connection_ident + 1;
2,518✔
599
    std::unique_ptr<ClientImpl::Connection> conn_2 = std::make_unique<ClientImpl::Connection>(
2,518✔
600
        *this, ident, std::move(endpoint), authorization_header_name, custom_http_headers,
2,518✔
601
        verify_servers_ssl_certificate, std::move(ssl_trust_certificate_path), std::move(ssl_verify_callback),
2,518✔
602
        std::move(proxy_config), server_slot.reconnect_info); // Throws
2,518✔
603
    ClientImpl::Connection& conn = *conn_2;
2,518✔
604
    if (!m_one_connection_per_session) {
2,518✔
605
        server_slot.connection = std::move(conn_2);
2,506✔
606
    }
2,506✔
607
    else {
12✔
608
        server_slot.alt_connections[ident] = std::move(conn_2); // Throws
12✔
609
    }
12✔
610
    m_prev_connection_ident = ident;
2,518✔
611
    was_created = true;
2,518✔
612
    {
2,518✔
613
        std::lock_guard lk(m_drain_mutex);
2,518✔
614
        ++m_num_connections;
2,518✔
615
    }
2,518✔
616
    return conn;
2,518✔
617
}
2,518✔
618

619

620
void ClientImpl::remove_connection(ClientImpl::Connection& conn) noexcept
621
{
2,516✔
622
    const ServerEndpoint& endpoint = conn.get_server_endpoint();
2,516✔
623
    auto i = m_server_slots.find(endpoint);
2,516✔
624
    REALM_ASSERT(i != m_server_slots.end()); // Must be found
2,516✔
625
    ServerSlot& server_slot = i->second;
2,516✔
626
    if (!m_one_connection_per_session) {
2,516✔
627
        REALM_ASSERT(server_slot.alt_connections.empty());
2,502✔
628
        REALM_ASSERT(&*server_slot.connection == &conn);
2,502✔
629
        server_slot.reconnect_info = conn.get_reconnect_info();
2,502✔
630
        server_slot.connection.reset();
2,502✔
631
    }
2,502✔
632
    else {
14✔
633
        REALM_ASSERT(!server_slot.connection);
14✔
634
        connection_ident_type ident = conn.get_ident();
14✔
635
        auto j = server_slot.alt_connections.find(ident);
14✔
636
        REALM_ASSERT(j != server_slot.alt_connections.end()); // Must be found
14✔
637
        REALM_ASSERT(&*j->second == &conn);
14✔
638
        server_slot.alt_connections.erase(j);
14✔
639
    }
14✔
640

1,188✔
641
    {
2,516✔
642
        std::lock_guard lk(m_drain_mutex);
2,516✔
643
        REALM_ASSERT(m_num_connections);
2,516✔
644
        --m_num_connections;
2,516✔
645
        m_drain_cv.notify_all();
2,516✔
646
    }
2,516✔
647
}
2,516✔
648

649

650
// ################ SessionImpl ################
651

652
void SessionImpl::force_close()
653
{
152✔
654
    // Allow force_close() if session is active or hasn't been activated yet.
78✔
655
    if (m_state == SessionImpl::Active || m_state == SessionImpl::Unactivated) {
152!
656
        m_wrapper.force_close();
152✔
657
    }
152✔
658
}
152✔
659

660
void SessionImpl::on_connection_state_changed(ConnectionState state,
661
                                              const util::Optional<SessionErrorInfo>& error_info)
662
{
10,112✔
663
    // Only used to report errors back to the SyncSession while the Session is active
5,184✔
664
    if (m_state == SessionImpl::Active) {
10,112✔
665
        m_wrapper.on_connection_state_changed(state, error_info); // Throws
10,112✔
666
    }
10,112✔
667
}
10,112✔
668

669

670
const std::string& SessionImpl::get_virt_path() const noexcept
671
{
8,696✔
672
    // Can only be called if the session is active or being activated
3,762✔
673
    REALM_ASSERT_EX(m_state == State::Active || m_state == State::Unactivated, m_state);
8,696!
674
    return m_wrapper.m_virt_path;
8,696✔
675
}
8,696✔
676

677
const std::string& SessionImpl::get_realm_path() const noexcept
678
{
9,972✔
679
    // Can only be called if the session is active or being activated
4,812✔
680
    REALM_ASSERT_EX(m_state == State::Active || m_state == State::Unactivated, m_state);
9,972✔
681
    return m_wrapper.m_db->get_path();
9,972✔
682
}
9,972✔
683

684
DBRef SessionImpl::get_db() const noexcept
685
{
23,012✔
686
    // Can only be called if the session is active or being activated
12,768✔
687
    REALM_ASSERT_EX(m_state == State::Active || m_state == State::Unactivated, m_state);
23,012!
688
    return m_wrapper.m_db;
23,012✔
689
}
23,012✔
690

691
ClientReplication& SessionImpl::get_repl() const noexcept
692
{
114,638✔
693
    // Can only be called if the session is active or being activated
57,442✔
694
    REALM_ASSERT_EX(m_state == State::Active || m_state == State::Unactivated, m_state);
114,638✔
695
    return m_wrapper.get_replication();
114,638✔
696
}
114,638✔
697

698
ClientHistory& SessionImpl::get_history() const noexcept
699
{
113,154✔
700
    return get_repl().get_history();
113,154✔
701
}
113,154✔
702

703
util::Optional<ClientReset>& SessionImpl::get_client_reset_config() noexcept
704
{
12,950✔
705
    // Can only be called if the session is active or being activated
6,218✔
706
    REALM_ASSERT_EX(m_state == State::Active || m_state == State::Unactivated, m_state);
12,950✔
707
    return m_wrapper.m_client_reset_config;
12,950✔
708
}
12,950✔
709

710
SessionReason SessionImpl::get_session_reason() noexcept
711
{
1,396✔
712
    // Can only be called if the session is active or being activated
718✔
713
    REALM_ASSERT_EX(m_state == State::Active || m_state == State::Unactivated, m_state);
1,396!
714
    return m_wrapper.m_session_reason;
1,396✔
715
}
1,396✔
716

717
void SessionImpl::initiate_integrate_changesets(std::uint_fast64_t downloadable_bytes, DownloadBatchState batch_state,
718
                                                const SyncProgress& progress, const ReceivedChangesets& changesets)
719
{
43,740✔
720
    // Ignore the call if the session is not active
22,754✔
721
    if (m_state != State::Active) {
43,740✔
722
        return;
×
723
    }
×
724

22,754✔
725
    try {
43,740✔
726
        bool simulate_integration_error = (m_wrapper.m_simulate_integration_error && !changesets.empty());
43,740!
727
        if (simulate_integration_error) {
43,740✔
728
            throw IntegrationException(ErrorCodes::BadChangeset, "simulated failure", ProtocolError::bad_changeset);
×
729
        }
×
730
        version_type client_version;
43,740✔
731
        if (REALM_LIKELY(!get_client().is_dry_run())) {
43,740✔
732
            VersionInfo version_info;
43,740✔
733
            integrate_changesets(progress, downloadable_bytes, changesets, version_info, batch_state); // Throws
43,740✔
734
            client_version = version_info.realm_version;
43,740✔
735
        }
43,740✔
736
        else {
×
737
            // Fake it for "dry run" mode
738
            client_version = m_last_version_available + 1;
×
739
        }
×
740
        on_changesets_integrated(client_version, progress); // Throws
43,740✔
741
    }
43,740✔
742
    catch (const IntegrationException& e) {
22,766✔
743
        on_integration_failure(e);
24✔
744
    }
24✔
745
    m_wrapper.on_sync_progress(); // Throws
43,738✔
746
}
43,738✔
747

748

749
void SessionImpl::on_upload_completion()
750
{
14,670✔
751
    // Ignore the call if the session is not active
7,252✔
752
    if (m_state == State::Active) {
14,670✔
753
        m_wrapper.on_upload_completion(); // Throws
14,670✔
754
    }
14,670✔
755
}
14,670✔
756

757

758
void SessionImpl::on_download_completion()
759
{
17,032✔
760
    // Ignore the call if the session is not active
7,644✔
761
    if (m_state == State::Active) {
17,032✔
762
        m_wrapper.on_download_completion(); // Throws
17,032✔
763
    }
17,032✔
764
}
17,032✔
765

766

767
void SessionImpl::on_suspended(const SessionErrorInfo& error_info)
768
{
924✔
769
    // Ignore the call if the session is not active
478✔
770
    if (m_state == State::Active) {
924✔
771
        m_wrapper.on_suspended(error_info); // Throws
924✔
772
    }
924✔
773
}
924✔
774

775

776
void SessionImpl::on_resumed()
777
{
382✔
778
    // Ignore the call if the session is not active
208✔
779
    if (m_state == State::Active) {
382✔
780
        m_wrapper.on_resumed(); // Throws
382✔
781
    }
382✔
782
}
382✔
783

784
void SessionImpl::handle_pending_client_reset_acknowledgement()
785
{
292✔
786
    // Ignore the call if the session is not active
146✔
787
    if (m_state == State::Active) {
292✔
788
        m_wrapper.handle_pending_client_reset_acknowledgement();
292✔
789
    }
292✔
790
}
292✔
791

792
void SessionImpl::update_subscription_version_info()
793
{
272✔
794
    // Ignore the call if the session is not active
136✔
795
    if (m_state == State::Active) {
272✔
796
        m_wrapper.update_subscription_version_info();
272✔
797
    }
272✔
798
}
272✔
799

800
bool SessionImpl::process_flx_bootstrap_message(const SyncProgress& progress, DownloadBatchState batch_state,
801
                                                int64_t query_version, const ReceivedChangesets& received_changesets)
802
{
45,378✔
803
    // Ignore the call if the session is not active
23,574✔
804
    if (m_state != State::Active) {
45,378✔
805
        return false;
×
806
    }
×
807

23,574✔
808
    if (is_steady_state_download_message(batch_state, query_version)) {
45,378✔
809
        return false;
43,740✔
810
    }
43,740✔
811

820✔
812
    auto bootstrap_store = m_wrapper.get_flx_pending_bootstrap_store();
1,638✔
813
    util::Optional<SyncProgress> maybe_progress;
1,638✔
814
    if (batch_state == DownloadBatchState::LastInBatch) {
1,638✔
815
        maybe_progress = progress;
1,478✔
816
    }
1,478✔
817

820✔
818
    bool new_batch = false;
1,638✔
819
    try {
1,638✔
820
        bootstrap_store->add_batch(query_version, std::move(maybe_progress), received_changesets, &new_batch);
1,638✔
821
    }
1,638✔
822
    catch (const LogicError& ex) {
820✔
823
        if (ex.code() == ErrorCodes::LimitExceeded) {
×
824
            IntegrationException ex(ErrorCodes::LimitExceeded,
×
825
                                    "bootstrap changeset too large to store in pending bootstrap store",
×
826
                                    ProtocolError::bad_changeset_size);
×
827
            on_integration_failure(ex);
×
828
            return true;
×
829
        }
×
830
        throw;
×
831
    }
×
832

820✔
833
    // If we've started a new batch and there is more to come, call on_flx_sync_progress to mark the subscription as
820✔
834
    // bootstrapping.
820✔
835
    if (new_batch && batch_state == DownloadBatchState::MoreToCome) {
1,638✔
836
        on_flx_sync_progress(query_version, DownloadBatchState::MoreToCome);
40✔
837
    }
40✔
838

820✔
839
    auto hook_action = call_debug_hook(SyncClientHookEvent::BootstrapMessageProcessed, progress, query_version,
1,638✔
840
                                       batch_state, received_changesets.size());
1,638✔
841
    if (hook_action == SyncClientHookAction::EarlyReturn) {
1,638✔
842
        return true;
12✔
843
    }
12✔
844
    REALM_ASSERT_EX(hook_action == SyncClientHookAction::NoAction, hook_action);
1,626✔
845

814✔
846
    if (batch_state == DownloadBatchState::MoreToCome) {
1,626✔
847
        return true;
156✔
848
    }
156✔
849

736✔
850
    try {
1,470✔
851
        process_pending_flx_bootstrap();
1,470✔
852
    }
1,470✔
853
    catch (const IntegrationException& e) {
740✔
854
        on_integration_failure(e);
8✔
855
    }
8✔
856

736✔
857
    return true;
1,470✔
858
}
1,470✔
859

860

861
void SessionImpl::process_pending_flx_bootstrap()
862
{
11,170✔
863
    // Ignore the call if not a flx session or session is not active
5,412✔
864
    if (!m_is_flx_sync_session || m_state != State::Active) {
11,170✔
865
        return;
8,662✔
866
    }
8,662✔
867
    // Should never be called if session is not active
1,256✔
868
    REALM_ASSERT_EX(m_state == SessionImpl::Active, m_state);
2,508✔
869
    auto bootstrap_store = m_wrapper.get_flx_pending_bootstrap_store();
2,508✔
870
    if (!bootstrap_store->has_pending()) {
2,508✔
871
        return;
1,022✔
872
    }
1,022✔
873

744✔
874
    auto pending_batch_stats = bootstrap_store->pending_stats();
1,486✔
875
    logger.info("Begin processing pending FLX bootstrap for query version %1. (changesets: %2, original total "
1,486✔
876
                "changeset size: %3)",
1,486✔
877
                pending_batch_stats.query_version, pending_batch_stats.pending_changesets,
1,486✔
878
                pending_batch_stats.pending_changeset_bytes);
1,486✔
879
    auto& history = get_repl().get_history();
1,486✔
880
    VersionInfo new_version;
1,486✔
881
    SyncProgress progress;
1,486✔
882
    int64_t query_version = -1;
1,486✔
883
    size_t changesets_processed = 0;
1,486✔
884

744✔
885
    // Used to commit each batch after it was transformed.
744✔
886
    TransactionRef transact = get_db()->start_write();
1,486✔
887
    while (bootstrap_store->has_pending()) {
3,088✔
888
        auto start_time = std::chrono::steady_clock::now();
1,618✔
889
        auto pending_batch = bootstrap_store->peek_pending(m_wrapper.m_flx_bootstrap_batch_size_bytes);
1,618✔
890
        if (!pending_batch.progress) {
1,618✔
891
            logger.info("Incomplete pending bootstrap found for query version %1", pending_batch.query_version);
8✔
892
            // Close the write transation before clearing the bootstrap store to avoid a deadlock because the
4✔
893
            // bootstrap store requires a write transaction itself.
4✔
894
            transact->close();
8✔
895
            bootstrap_store->clear();
8✔
896
            return;
8✔
897
        }
8✔
898

806✔
899
        auto batch_state =
1,610✔
900
            pending_batch.remaining_changesets > 0 ? DownloadBatchState::MoreToCome : DownloadBatchState::LastInBatch;
1,542✔
901
        uint64_t downloadable_bytes = 0;
1,610✔
902
        query_version = pending_batch.query_version;
1,610✔
903
        bool simulate_integration_error =
1,610✔
904
            (m_wrapper.m_simulate_integration_error && !pending_batch.changesets.empty());
1,610✔
905
        if (simulate_integration_error) {
1,610✔
906
            throw IntegrationException(ErrorCodes::BadChangeset, "simulated failure", ProtocolError::bad_changeset);
8✔
907
        }
8✔
908

802✔
909
        history.integrate_server_changesets(
1,602✔
910
            *pending_batch.progress, &downloadable_bytes, pending_batch.changesets, new_version, batch_state, logger,
1,602✔
911
            transact, [&](const TransactionRef& tr, util::Span<Changeset> changesets_applied) {
1,600✔
912
                REALM_ASSERT_3(changesets_applied.size(), <=, pending_batch.changesets.size());
1,598✔
913
                bootstrap_store->pop_front_pending(tr, changesets_applied.size());
1,598✔
914
            });
1,598✔
915
        progress = *pending_batch.progress;
1,602✔
916
        changesets_processed += pending_batch.changesets.size();
1,602✔
917
        auto duration = std::chrono::steady_clock::now() - start_time;
1,602✔
918

802✔
919
        auto action = call_debug_hook(SyncClientHookEvent::DownloadMessageIntegrated, progress, query_version,
1,602✔
920
                                      batch_state, pending_batch.changesets.size());
1,602✔
921
        REALM_ASSERT_EX(action == SyncClientHookAction::NoAction, action);
1,602✔
922

802✔
923
        logger.info("Integrated %1 changesets from pending bootstrap for query version %2, producing client version "
1,602✔
924
                    "%3 in %4 ms. %5 changesets remaining in bootstrap",
1,602✔
925
                    pending_batch.changesets.size(), pending_batch.query_version, new_version.realm_version,
1,602✔
926
                    std::chrono::duration_cast<std::chrono::milliseconds>(duration).count(),
1,602✔
927
                    pending_batch.remaining_changesets);
1,602✔
928
    }
1,602✔
929
    on_changesets_integrated(new_version.realm_version, progress);
1,478✔
930

736✔
931
    REALM_ASSERT_3(query_version, !=, -1);
1,470✔
932
    m_wrapper.on_sync_progress();
1,470✔
933
    on_flx_sync_progress(query_version, DownloadBatchState::LastInBatch);
1,470✔
934

736✔
935
    auto action = call_debug_hook(SyncClientHookEvent::BootstrapProcessed, progress, query_version,
1,470✔
936
                                  DownloadBatchState::LastInBatch, changesets_processed);
1,470✔
937
    // NoAction/EarlyReturn are both valid no-op actions to take here.
736✔
938
    REALM_ASSERT_EX(action == SyncClientHookAction::NoAction || action == SyncClientHookAction::EarlyReturn, action);
1,470✔
939
}
1,470✔
940

941
void SessionImpl::on_flx_sync_error(int64_t version, std::string_view err_msg)
942
{
16✔
943
    // Ignore the call if the session is not active
8✔
944
    if (m_state == State::Active) {
16✔
945
        m_wrapper.on_flx_sync_error(version, err_msg);
16✔
946
    }
16✔
947
}
16✔
948

949
void SessionImpl::on_flx_sync_progress(int64_t version, DownloadBatchState batch_state)
950
{
1,506✔
951
    // Ignore the call if the session is not active
754✔
952
    if (m_state == State::Active) {
1,506✔
953
        m_wrapper.on_flx_sync_progress(version, batch_state);
1,506✔
954
    }
1,506✔
955
}
1,506✔
956

957
SubscriptionStore* SessionImpl::get_flx_subscription_store()
958
{
13,560✔
959
    // Should never be called if session is not active
6,944✔
960
    REALM_ASSERT_EX(m_state == State::Active, m_state);
13,560✔
961
    return m_wrapper.get_flx_subscription_store();
13,560✔
962
}
13,560✔
963

964
MigrationStore* SessionImpl::get_migration_store()
965
{
60,386✔
966
    // Should never be called if session is not active
31,378✔
967
    REALM_ASSERT_EX(m_state == State::Active, m_state);
60,386✔
968
    return m_wrapper.get_migration_store();
60,386✔
969
}
60,386✔
970

971
void SessionImpl::on_flx_sync_version_complete(int64_t version)
972
{
232✔
973
    // Ignore the call if the session is not active
116✔
974
    if (m_state == State::Active) {
232✔
975
        m_wrapper.on_flx_sync_version_complete(version);
232✔
976
    }
232✔
977
}
232✔
978

979
SyncClientHookAction SessionImpl::call_debug_hook(const SyncClientHookData& data)
980
{
832✔
981
    // Should never be called if session is not active
420✔
982
    REALM_ASSERT_EX(m_state == State::Active, m_state);
832✔
983

420✔
984
    // Make sure we don't call the debug hook recursively.
420✔
985
    if (m_wrapper.m_in_debug_hook) {
832✔
986
        return SyncClientHookAction::NoAction;
×
987
    }
×
988
    m_wrapper.m_in_debug_hook = true;
832✔
989
    auto in_hook_guard = util::make_scope_exit([&]() noexcept {
832✔
990
        m_wrapper.m_in_debug_hook = false;
832✔
991
    });
832✔
992

420✔
993
    auto action = m_wrapper.m_debug_hook(data);
832✔
994
    switch (action) {
832✔
995
        case realm::SyncClientHookAction::SuspendWithRetryableError: {
✔
996
            SessionErrorInfo err_info(Status{ErrorCodes::RuntimeError, "hook requested error"}, IsFatal{false});
×
997
            err_info.server_requests_action = ProtocolErrorInfo::Action::Transient;
×
998

999
            auto err_processing_err = receive_error_message(err_info);
×
1000
            REALM_ASSERT_EX(err_processing_err.is_ok(), err_processing_err);
×
1001
            return SyncClientHookAction::EarlyReturn;
×
1002
        }
×
1003
        case realm::SyncClientHookAction::TriggerReconnect: {
24✔
1004
            get_connection().voluntary_disconnect();
24✔
1005
            return SyncClientHookAction::EarlyReturn;
24✔
1006
        }
×
1007
        default:
808✔
1008
            return action;
808✔
1009
    }
832✔
1010
}
832✔
1011

1012
SyncClientHookAction SessionImpl::call_debug_hook(SyncClientHookEvent event, const SyncProgress& progress,
1013
                                                  int64_t query_version, DownloadBatchState batch_state,
1014
                                                  size_t num_changesets)
1015
{
93,830✔
1016
    if (REALM_LIKELY(!m_wrapper.m_debug_hook)) {
93,830✔
1017
        return SyncClientHookAction::NoAction;
93,074✔
1018
    }
93,074✔
1019
    if (REALM_UNLIKELY(m_state != State::Active)) {
756✔
1020
        return SyncClientHookAction::NoAction;
×
1021
    }
×
1022

380✔
1023
    SyncClientHookData data;
756✔
1024
    data.event = event;
756✔
1025
    data.batch_state = batch_state;
756✔
1026
    data.progress = progress;
756✔
1027
    data.num_changesets = num_changesets;
756✔
1028
    data.query_version = query_version;
756✔
1029

380✔
1030
    return call_debug_hook(data);
756✔
1031
}
756✔
1032

1033
SyncClientHookAction SessionImpl::call_debug_hook(SyncClientHookEvent event, const ProtocolErrorInfo& error_info)
1034
{
900✔
1035
    if (REALM_LIKELY(!m_wrapper.m_debug_hook)) {
900✔
1036
        return SyncClientHookAction::NoAction;
824✔
1037
    }
824✔
1038
    if (REALM_UNLIKELY(m_state != State::Active)) {
76✔
1039
        return SyncClientHookAction::NoAction;
×
1040
    }
×
1041

40✔
1042
    SyncClientHookData data;
76✔
1043
    data.event = event;
76✔
1044
    data.batch_state = DownloadBatchState::SteadyState;
76✔
1045
    data.progress = m_progress;
76✔
1046
    data.num_changesets = 0;
76✔
1047
    data.query_version = 0;
76✔
1048
    data.error_info = &error_info;
76✔
1049

40✔
1050
    return call_debug_hook(data);
76✔
1051
}
76✔
1052

1053
bool SessionImpl::is_steady_state_download_message(DownloadBatchState batch_state, int64_t query_version)
1054
{
90,766✔
1055
    // Should never be called if session is not active
47,152✔
1056
    REALM_ASSERT_EX(m_state == State::Active, m_state);
90,766✔
1057
    if (batch_state == DownloadBatchState::SteadyState) {
90,766✔
1058
        return true;
43,740✔
1059
    }
43,740✔
1060

24,398✔
1061
    if (!m_is_flx_sync_session) {
47,026✔
1062
        return true;
42,832✔
1063
    }
42,832✔
1064

2,092✔
1065
    // If this is a steady state DOWNLOAD, no need for special handling.
2,092✔
1066
    if (batch_state == DownloadBatchState::LastInBatch && query_version == m_wrapper.m_flx_active_version) {
4,194✔
1067
        return true;
910✔
1068
    }
910✔
1069

1,644✔
1070
    return false;
3,284✔
1071
}
3,284✔
1072

1073
util::Future<std::string> SessionImpl::send_test_command(std::string body)
1074
{
52✔
1075
    if (m_state != State::Active) {
52✔
1076
        return Status{ErrorCodes::RuntimeError, "Cannot send a test command for a session that is not active"};
×
1077
    }
×
1078

26✔
1079
    try {
52✔
1080
        auto json_body = nlohmann::json::parse(body.begin(), body.end());
52✔
1081
        if (auto it = json_body.find("command"); it == json_body.end() || !it->is_string()) {
52✔
1082
            return Status{ErrorCodes::LogicError,
4✔
1083
                          "Must supply command name in \"command\" field of test command json object"};
4✔
1084
        }
4✔
1085
        if (json_body.size() > 1 && json_body.find("args") == json_body.end()) {
48✔
1086
            return Status{ErrorCodes::LogicError, "Only valid fields in a test command are \"command\" and \"args\""};
×
1087
        }
×
1088
    }
4✔
1089
    catch (const nlohmann::json::parse_error& e) {
4✔
1090
        return Status{ErrorCodes::LogicError, util::format("Invalid json input to send_test_command: %1", e.what())};
4✔
1091
    }
4✔
1092

22✔
1093
    auto pf = util::make_promise_future<std::string>();
44✔
1094

22✔
1095
    get_client().post([this, promise = std::move(pf.promise), body = std::move(body)](Status status) mutable {
44✔
1096
        // Includes operation_aborted
22✔
1097
        if (!status.is_ok())
44✔
1098
            promise.set_error(status);
×
1099

22✔
1100
        auto id = ++m_last_pending_test_command_ident;
44✔
1101
        m_pending_test_commands.push_back(PendingTestCommand{id, std::move(body), std::move(promise)});
44✔
1102
        ensure_enlisted_to_send();
44✔
1103
    });
44✔
1104

22✔
1105
    return std::move(pf.future);
44✔
1106
}
44✔
1107

1108
// ################ SessionWrapper ################
1109

1110
// The SessionWrapper class is held by a sync::Session (which is owned by the SyncSession instance) and
1111
// provides a link to the ClientImpl::Session that creates and receives messages with the server with
1112
// the ClientImpl::Connection that owns the ClientImpl::Session.
1113
SessionWrapper::SessionWrapper(ClientImpl& client, DBRef db, std::shared_ptr<SubscriptionStore> flx_sub_store,
1114
                               std::shared_ptr<MigrationStore> migration_store, Session::Config config)
1115
    : m_client{client}
1116
    , m_db(std::move(db))
1117
    , m_replication(m_db->get_replication())
1118
    , m_protocol_envelope{config.protocol_envelope}
1119
    , m_server_address{std::move(config.server_address)}
1120
    , m_server_port{config.server_port}
1121
    , m_user_id(std::move(config.user_id))
1122
    , m_sync_mode(flx_sub_store ? SyncServerMode::FLX : SyncServerMode::PBS)
1123
    , m_authorization_header_name{config.authorization_header_name}
1124
    , m_custom_http_headers{config.custom_http_headers}
1125
    , m_verify_servers_ssl_certificate{config.verify_servers_ssl_certificate}
1126
    , m_simulate_integration_error{config.simulate_integration_error}
1127
    , m_ssl_trust_certificate_path{std::move(config.ssl_trust_certificate_path)}
1128
    , m_ssl_verify_callback{std::move(config.ssl_verify_callback)}
1129
    , m_flx_bootstrap_batch_size_bytes(config.flx_bootstrap_batch_size_bytes)
1130
    , m_http_request_path_prefix{std::move(config.service_identifier)}
1131
    , m_virt_path{std::move(config.realm_identifier)}
1132
    , m_signed_access_token{std::move(config.signed_user_token)}
1133
    , m_client_reset_config{std::move(config.client_reset_config)}
1134
    , m_proxy_config{config.proxy_config} // Throws
1135
    , m_debug_hook(std::move(config.on_sync_client_event_hook))
1136
    , m_session_reason(config.session_reason)
1137
    , m_flx_subscription_store(std::move(flx_sub_store))
1138
    , m_migration_store(std::move(migration_store))
1139
{
11,028✔
1140
    REALM_ASSERT(m_db);
11,028✔
1141
    REALM_ASSERT(m_db->get_replication());
11,028✔
1142
    REALM_ASSERT(dynamic_cast<ClientReplication*>(m_db->get_replication()));
11,028✔
1143
    if (m_client_reset_config) {
11,028✔
1144
        m_session_reason = SessionReason::ClientReset;
348✔
1145
    }
348✔
1146

5,342✔
1147
    update_subscription_version_info();
11,028✔
1148
}
11,028✔
1149

1150
SessionWrapper::~SessionWrapper() noexcept
1151
{
11,028✔
1152
    if (m_db && m_actualized) {
11,028✔
1153
        m_db->remove_commit_listener(this);
166✔
1154
        m_db->release_sync_agent();
166✔
1155
    }
166✔
1156
}
11,028✔
1157

1158

1159
inline ClientReplication& SessionWrapper::get_replication() noexcept
1160
{
114,638✔
1161
    REALM_ASSERT(m_db);
114,638✔
1162
    return static_cast<ClientReplication&>(*m_replication);
114,638✔
1163
}
114,638✔
1164

1165

1166
inline ClientImpl& SessionWrapper::get_client() noexcept
1167
{
72✔
1168
    return m_client;
72✔
1169
}
72✔
1170

1171
bool SessionWrapper::has_flx_subscription_store() const
1172
{
1,506✔
1173
    return static_cast<bool>(m_flx_subscription_store);
1,506✔
1174
}
1,506✔
1175

1176
void SessionWrapper::on_flx_sync_error(int64_t version, std::string_view err_msg)
1177
{
16✔
1178
    REALM_ASSERT(!m_finalized);
16✔
1179
    auto mut_subs = get_flx_subscription_store()->get_mutable_by_version(version);
16✔
1180
    mut_subs.update_state(SubscriptionSet::State::Error, err_msg);
16✔
1181
    mut_subs.commit();
16✔
1182
}
16✔
1183

1184
void SessionWrapper::on_flx_sync_version_complete(int64_t version)
1185
{
1,698✔
1186
    REALM_ASSERT(!m_finalized);
1,698✔
1187
    m_flx_last_seen_version = version;
1,698✔
1188
    m_flx_active_version = version;
1,698✔
1189
}
1,698✔
1190

1191
void SessionWrapper::on_flx_sync_progress(int64_t new_version, DownloadBatchState batch_state)
1192
{
1,506✔
1193
    if (!has_flx_subscription_store()) {
1,506✔
1194
        return;
×
1195
    }
×
1196
    REALM_ASSERT(!m_finalized);
1,506✔
1197
    REALM_ASSERT(new_version >= m_flx_last_seen_version);
1,506✔
1198
    REALM_ASSERT(new_version >= m_flx_active_version);
1,506✔
1199
    REALM_ASSERT(batch_state != DownloadBatchState::SteadyState);
1,506✔
1200

754✔
1201
    SubscriptionSet::State new_state = SubscriptionSet::State::Uncommitted; // Initialize to make compiler happy
1,506✔
1202

754✔
1203
    switch (batch_state) {
1,506✔
1204
        case DownloadBatchState::SteadyState:
✔
1205
            // Cannot be called with this value.
1206
            REALM_UNREACHABLE();
1207
        case DownloadBatchState::LastInBatch:
1,466✔
1208
            if (m_flx_active_version == new_version) {
1,466✔
1209
                return;
×
1210
            }
×
1211
            on_flx_sync_version_complete(new_version);
1,466✔
1212
            if (new_version == 0) {
1,466✔
1213
                new_state = SubscriptionSet::State::Complete;
672✔
1214
            }
672✔
1215
            else {
794✔
1216
                new_state = SubscriptionSet::State::AwaitingMark;
794✔
1217
                m_flx_pending_mark_version = new_version;
794✔
1218
            }
794✔
1219
            break;
1,466✔
1220
        case DownloadBatchState::MoreToCome:
754✔
1221
            if (m_flx_last_seen_version == new_version) {
40✔
1222
                return;
×
1223
            }
×
1224

20✔
1225
            m_flx_last_seen_version = new_version;
40✔
1226
            new_state = SubscriptionSet::State::Bootstrapping;
40✔
1227
            break;
40✔
1228
    }
1,506✔
1229

754✔
1230
    auto mut_subs = get_flx_subscription_store()->get_mutable_by_version(new_version);
1,506✔
1231
    mut_subs.update_state(new_state);
1,506✔
1232
    mut_subs.commit();
1,506✔
1233
}
1,506✔
1234

1235
SubscriptionStore* SessionWrapper::get_flx_subscription_store()
1236
{
15,082✔
1237
    REALM_ASSERT(!m_finalized);
15,082✔
1238
    return m_flx_subscription_store.get();
15,082✔
1239
}
15,082✔
1240

1241
PendingBootstrapStore* SessionWrapper::get_flx_pending_bootstrap_store()
1242
{
4,146✔
1243
    REALM_ASSERT(!m_finalized);
4,146✔
1244
    return m_flx_pending_bootstrap_store.get();
4,146✔
1245
}
4,146✔
1246

1247
MigrationStore* SessionWrapper::get_migration_store()
1248
{
60,386✔
1249
    REALM_ASSERT(!m_finalized);
60,386✔
1250
    return m_migration_store.get();
60,386✔
1251
}
60,386✔
1252

1253
inline void SessionWrapper::set_progress_handler(util::UniqueFunction<ProgressHandler> handler)
1254
{
3,496✔
1255
    REALM_ASSERT(!m_initiated);
3,496✔
1256
    m_progress_handler = std::move(handler);
3,496✔
1257
}
3,496✔
1258

1259

1260
inline void
1261
SessionWrapper::set_connection_state_change_listener(util::UniqueFunction<ConnectionStateChangeListener> listener)
1262
{
11,124✔
1263
    REALM_ASSERT(!m_initiated);
11,124✔
1264
    m_connection_state_change_listener = std::move(listener);
11,124✔
1265
}
11,124✔
1266

1267

1268
void SessionWrapper::initiate()
1269
{
9,876✔
1270
    REALM_ASSERT(!m_initiated);
9,876✔
1271
    ServerEndpoint server_endpoint{m_protocol_envelope, m_server_address, m_server_port, m_user_id, m_sync_mode};
9,876✔
1272
    m_client.register_unactualized_session_wrapper(this, std::move(server_endpoint)); // Throws
9,876✔
1273
    m_initiated = true;
9,876✔
1274
    m_db->add_commit_listener(this);
9,876✔
1275
}
9,876✔
1276

1277

1278
void SessionWrapper::on_commit(version_type new_version)
1279
{
107,294✔
1280
    // Thread safety required
53,668✔
1281
    REALM_ASSERT(m_initiated);
107,294✔
1282

53,668✔
1283
    if (REALM_UNLIKELY(m_finalized || m_force_closed)) {
107,294✔
1284
        return;
4✔
1285
    }
4✔
1286

53,666✔
1287
    util::bind_ptr<SessionWrapper> self{this};
107,290✔
1288
    m_client.post([self = std::move(self), new_version](Status status) {
107,304✔
1289
        if (status == ErrorCodes::OperationAborted)
107,298✔
1290
            return;
×
1291
        else if (!status.is_ok())
107,298✔
1292
            throw Exception(status);
×
1293

53,660✔
1294
        REALM_ASSERT(self->m_actualized);
107,298✔
1295
        if (REALM_UNLIKELY(!self->m_sess))
107,298✔
1296
            return; // Already finalized
54,124✔
1297
        SessionImpl& sess = *self->m_sess;
106,474✔
1298
        sess.recognize_sync_version(new_version); // Throws
106,474✔
1299
        bool only_if_new_uploadable_data = true;
106,474✔
1300
        self->report_progress(only_if_new_uploadable_data); // Throws
106,474✔
1301
    });
106,474✔
1302
}
107,290✔
1303

1304

1305
void SessionWrapper::cancel_reconnect_delay()
1306
{
12✔
1307
    // Thread safety required
6✔
1308
    REALM_ASSERT(m_initiated);
12✔
1309

6✔
1310
    if (REALM_UNLIKELY(m_finalized || m_force_closed)) {
12✔
1311
        return;
×
1312
    }
×
1313

6✔
1314
    util::bind_ptr<SessionWrapper> self{this};
12✔
1315
    m_client.post([self = std::move(self)](Status status) {
12✔
1316
        if (status == ErrorCodes::OperationAborted)
12✔
1317
            return;
×
1318
        else if (!status.is_ok())
12✔
1319
            throw Exception(status);
×
1320

6✔
1321
        REALM_ASSERT(self->m_actualized);
12✔
1322
        if (REALM_UNLIKELY(!self->m_sess))
12✔
1323
            return; // Already finalized
6✔
1324
        SessionImpl& sess = *self->m_sess;
12✔
1325
        sess.cancel_resumption_delay(); // Throws
12✔
1326
        ClientImpl::Connection& conn = sess.get_connection();
12✔
1327
        conn.cancel_reconnect_delay(); // Throws
12✔
1328
    });                                // Throws
12✔
1329
}
12✔
1330

1331
void SessionWrapper::async_wait_for(bool upload_completion, bool download_completion,
1332
                                    WaitOperCompletionHandler handler)
1333
{
4,390✔
1334
    REALM_ASSERT(upload_completion || download_completion);
4,390✔
1335
    REALM_ASSERT(m_initiated);
4,390✔
1336
    REALM_ASSERT(!m_finalized);
4,390✔
1337

2,108✔
1338
    util::bind_ptr<SessionWrapper> self{this};
4,390✔
1339
    m_client.post([self = std::move(self), handler = std::move(handler), upload_completion,
4,390✔
1340
                   download_completion](Status status) mutable {
4,390✔
1341
        if (status == ErrorCodes::OperationAborted)
4,390✔
1342
            return;
×
1343
        else if (!status.is_ok())
4,390✔
1344
            throw Exception(status);
×
1345

2,108✔
1346
        REALM_ASSERT(self->m_actualized);
4,390✔
1347
        if (REALM_UNLIKELY(!self->m_sess)) {
4,390✔
1348
            // Already finalized
36✔
1349
            handler({ErrorCodes::OperationAborted, "Session finalized before callback could run"}); // Throws
70✔
1350
            return;
70✔
1351
        }
70✔
1352
        if (upload_completion) {
4,320✔
1353
            if (download_completion) {
2,294✔
1354
                // Wait for upload and download completion
140✔
1355
                self->m_sync_completion_handlers.push_back(std::move(handler)); // Throws
278✔
1356
            }
278✔
1357
            else {
2,016✔
1358
                // Wait for upload completion only
918✔
1359
                self->m_upload_completion_handlers.push_back(std::move(handler)); // Throws
2,016✔
1360
            }
2,016✔
1361
        }
2,294✔
1362
        else {
2,026✔
1363
            // Wait for download completion only
1,014✔
1364
            self->m_download_completion_handlers.push_back(std::move(handler)); // Throws
2,026✔
1365
        }
2,026✔
1366
        SessionImpl& sess = *self->m_sess;
4,320✔
1367
        if (upload_completion)
4,320✔
1368
            sess.request_upload_completion_notification(); // Throws
2,294✔
1369
        if (download_completion)
4,320✔
1370
            sess.request_download_completion_notification(); // Throws
2,304✔
1371
    });                                                      // Throws
4,320✔
1372
}
4,390✔
1373

1374

1375
bool SessionWrapper::wait_for_upload_complete_or_client_stopped()
1376
{
12,996✔
1377
    // Thread safety required
6,498✔
1378
    REALM_ASSERT(m_initiated);
12,996✔
1379
    REALM_ASSERT(!m_finalized);
12,996✔
1380

6,498✔
1381
    std::int_fast64_t target_mark;
12,996✔
1382
    {
12,996✔
1383
        std::lock_guard lock{m_client.m_mutex};
12,996✔
1384
        target_mark = ++m_target_upload_mark;
12,996✔
1385
    }
12,996✔
1386

6,498✔
1387
    util::bind_ptr<SessionWrapper> self{this};
12,996✔
1388
    m_client.post([self = std::move(self), target_mark](Status status) {
12,996✔
1389
        if (status == ErrorCodes::OperationAborted)
12,996✔
1390
            return;
×
1391
        else if (!status.is_ok())
12,996✔
1392
            throw Exception(status);
×
1393

6,498✔
1394
        REALM_ASSERT(self->m_actualized);
12,996✔
1395
        // The session wrapper may already have been finalized. This can only
6,498✔
1396
        // happen if it was abandoned, but in that case, the call of
6,498✔
1397
        // wait_for_upload_complete_or_client_stopped() must have returned
6,498✔
1398
        // already.
6,498✔
1399
        if (REALM_UNLIKELY(!self->m_sess))
12,996✔
1400
            return;
6,508✔
1401
        if (target_mark > self->m_staged_upload_mark) {
12,982✔
1402
            self->m_staged_upload_mark = target_mark;
12,982✔
1403
            SessionImpl& sess = *self->m_sess;
12,982✔
1404
            sess.request_upload_completion_notification(); // Throws
12,982✔
1405
        }
12,982✔
1406
    }); // Throws
12,982✔
1407

6,498✔
1408
    bool completion_condition_was_satisfied;
12,996✔
1409
    {
12,996✔
1410
        std::unique_lock lock{m_client.m_mutex};
12,996✔
1411
        while (m_reached_upload_mark < target_mark && !m_client.m_stopped)
33,148✔
1412
            m_client.m_wait_or_client_stopped_cond.wait(lock);
20,152✔
1413
        completion_condition_was_satisfied = !m_client.m_stopped;
12,996✔
1414
    }
12,996✔
1415
    return completion_condition_was_satisfied;
12,996✔
1416
}
12,996✔
1417

1418

1419
bool SessionWrapper::wait_for_download_complete_or_client_stopped()
1420
{
10,092✔
1421
    // Thread safety required
5,048✔
1422
    REALM_ASSERT(m_initiated);
10,092✔
1423
    REALM_ASSERT(!m_finalized);
10,092✔
1424

5,048✔
1425
    std::int_fast64_t target_mark;
10,092✔
1426
    {
10,092✔
1427
        std::lock_guard lock{m_client.m_mutex};
10,092✔
1428
        target_mark = ++m_target_download_mark;
10,092✔
1429
    }
10,092✔
1430

5,048✔
1431
    util::bind_ptr<SessionWrapper> self{this};
10,092✔
1432
    m_client.post([self = std::move(self), target_mark](Status status) {
10,092✔
1433
        if (status == ErrorCodes::OperationAborted)
10,092✔
1434
            return;
×
1435
        else if (!status.is_ok())
10,092✔
1436
            throw Exception(status);
×
1437

5,048✔
1438
        REALM_ASSERT(self->m_actualized);
10,092✔
1439
        // The session wrapper may already have been finalized. This can only
5,048✔
1440
        // happen if it was abandoned, but in that case, the call of
5,048✔
1441
        // wait_for_download_complete_or_client_stopped() must have returned
5,048✔
1442
        // already.
5,048✔
1443
        if (REALM_UNLIKELY(!self->m_sess))
10,092✔
1444
            return;
5,078✔
1445
        if (target_mark > self->m_staged_download_mark) {
10,032✔
1446
            self->m_staged_download_mark = target_mark;
10,032✔
1447
            SessionImpl& sess = *self->m_sess;
10,032✔
1448
            sess.request_download_completion_notification(); // Throws
10,032✔
1449
        }
10,032✔
1450
    }); // Throws
10,032✔
1451

5,048✔
1452
    bool completion_condition_was_satisfied;
10,092✔
1453
    {
10,092✔
1454
        std::unique_lock lock{m_client.m_mutex};
10,092✔
1455
        while (m_reached_download_mark < target_mark && !m_client.m_stopped)
20,558✔
1456
            m_client.m_wait_or_client_stopped_cond.wait(lock);
10,466✔
1457
        completion_condition_was_satisfied = !m_client.m_stopped;
10,092✔
1458
    }
10,092✔
1459
    return completion_condition_was_satisfied;
10,092✔
1460
}
10,092✔
1461

1462

1463
void SessionWrapper::refresh(std::string signed_access_token)
1464
{
208✔
1465
    // Thread safety required
104✔
1466
    REALM_ASSERT(m_initiated);
208✔
1467
    REALM_ASSERT(!m_finalized);
208✔
1468

104✔
1469
    m_client.post([self = util::bind_ptr(this), token = std::move(signed_access_token)](Status status) {
208✔
1470
        if (status == ErrorCodes::OperationAborted)
208✔
1471
            return;
×
1472
        else if (!status.is_ok())
208✔
1473
            throw Exception(status);
×
1474

104✔
1475
        REALM_ASSERT(self->m_actualized);
208✔
1476
        if (REALM_UNLIKELY(!self->m_sess))
208✔
1477
            return; // Already finalized
104✔
1478
        self->m_signed_access_token = std::move(token);
208✔
1479
        SessionImpl& sess = *self->m_sess;
208✔
1480
        ClientImpl::Connection& conn = sess.get_connection();
208✔
1481
        // FIXME: This only makes sense when each session uses a separate connection.
104✔
1482
        conn.update_connect_info(self->m_http_request_path_prefix, self->m_signed_access_token); // Throws
208✔
1483
        sess.cancel_resumption_delay();                                                          // Throws
208✔
1484
        conn.cancel_reconnect_delay();                                                           // Throws
208✔
1485
    });
208✔
1486
}
208✔
1487

1488

1489
inline void SessionWrapper::abandon(util::bind_ptr<SessionWrapper> wrapper) noexcept
1490
{
11,028✔
1491
    if (wrapper->m_initiated) {
11,028✔
1492
        ClientImpl& client = wrapper->m_client;
9,876✔
1493
        client.register_abandoned_session_wrapper(std::move(wrapper));
9,876✔
1494
    }
9,876✔
1495
}
11,028✔
1496

1497

1498
// Must be called from event loop thread
1499
void SessionWrapper::actualize(ServerEndpoint endpoint)
1500
{
9,704✔
1501
    REALM_ASSERT(!m_actualized);
9,704✔
1502
    REALM_ASSERT(!m_sess);
9,704✔
1503
    // Cannot be actualized if it's already been finalized or force closed
4,678✔
1504
    REALM_ASSERT(!m_finalized);
9,704✔
1505
    REALM_ASSERT(!m_force_closed);
9,704✔
1506
    try {
9,704✔
1507
        m_db->claim_sync_agent();
9,704✔
1508
    }
9,704✔
1509
    catch (const MultipleSyncAgents&) {
4,680✔
1510
        finalize_before_actualization();
4✔
1511
        throw;
4✔
1512
    }
4✔
1513
    auto sync_mode = endpoint.server_mode;
9,700✔
1514

4,676✔
1515
    bool was_created = false;
9,700✔
1516
    ClientImpl::Connection& conn = m_client.get_connection(
9,700✔
1517
        std::move(endpoint), m_authorization_header_name, m_custom_http_headers, m_verify_servers_ssl_certificate,
9,700✔
1518
        m_ssl_trust_certificate_path, m_ssl_verify_callback, m_proxy_config,
9,700✔
1519
        was_created); // Throws
9,700✔
1520
    try {
9,700✔
1521
        // FIXME: This only makes sense when each session uses a separate connection.
4,676✔
1522
        conn.update_connect_info(m_http_request_path_prefix, m_signed_access_token);    // Throws
9,700✔
1523
        std::unique_ptr<SessionImpl> sess = std::make_unique<SessionImpl>(*this, conn); // Throws
9,700✔
1524
        if (sync_mode == SyncServerMode::FLX) {
9,700✔
1525
            m_flx_pending_bootstrap_store = std::make_unique<PendingBootstrapStore>(m_db, sess->logger);
1,038✔
1526
        }
1,038✔
1527

4,676✔
1528
        sess->logger.info("Binding '%1' to '%2'", m_db->get_path(), m_virt_path); // Throws
9,700✔
1529
        m_sess = sess.get();
9,700✔
1530
        conn.activate_session(std::move(sess)); // Throws
9,700✔
1531
    }
9,700✔
1532
    catch (...) {
4,676✔
1533
        if (was_created)
×
1534
            m_client.remove_connection(conn);
×
1535

1536
        finalize_before_actualization();
×
1537
        throw;
×
1538
    }
×
1539

4,676✔
1540
    m_actualized = true;
9,698✔
1541
    if (was_created)
9,698✔
1542
        conn.activate(); // Throws
2,516✔
1543

4,676✔
1544
    if (m_connection_state_change_listener) {
9,698✔
1545
        ConnectionState state = conn.get_state();
9,680✔
1546
        if (state != ConnectionState::disconnected) {
9,680✔
1547
            m_connection_state_change_listener(ConnectionState::connecting, util::none); // Throws
7,012✔
1548
            if (state == ConnectionState::connected)
7,012✔
1549
                m_connection_state_change_listener(ConnectionState::connected, util::none); // Throws
6,762✔
1550
        }
7,012✔
1551
    }
9,680✔
1552

4,676✔
1553
    if (!m_client_reset_config)
9,698✔
1554
        report_progress(); // Throws
9,350✔
1555
}
9,698✔
1556

1557
void SessionWrapper::force_close()
1558
{
152✔
1559
    if (m_force_closed || m_finalized) {
152✔
1560
        return;
×
1561
    }
×
1562
    REALM_ASSERT(m_actualized);
152✔
1563
    REALM_ASSERT(m_sess);
152✔
1564
    m_force_closed = true;
152✔
1565

78✔
1566
    ClientImpl::Connection& conn = m_sess->get_connection();
152✔
1567
    conn.initiate_session_deactivation(m_sess); // Throws
152✔
1568

78✔
1569
    // Delete the pending bootstrap store since it uses a reference to the logger in m_sess
78✔
1570
    m_flx_pending_bootstrap_store.reset();
152✔
1571
    // Clear the subscription and migration store refs since they are owned by SyncSession
78✔
1572
    m_flx_subscription_store.reset();
152✔
1573
    m_migration_store.reset();
152✔
1574
    m_sess = nullptr;
152✔
1575
    // Everything is being torn down, no need to report connection state anymore
78✔
1576
    m_connection_state_change_listener = {};
152✔
1577
}
152✔
1578

1579
// Must be called from event loop thread
1580
void SessionWrapper::finalize()
1581
{
9,708✔
1582
    REALM_ASSERT(m_actualized);
9,708✔
1583

4,680✔
1584
    // Already finalized?
4,680✔
1585
    if (m_finalized) {
9,708✔
1586
        return;
×
1587
    }
×
1588

4,680✔
1589
    // Must be before marking as finalized as we expect m_finalized == false in on_change()
4,680✔
1590
    m_db->remove_commit_listener(this);
9,708✔
1591

4,680✔
1592
    m_finalized = true;
9,708✔
1593

4,680✔
1594
    if (!m_force_closed) {
9,708✔
1595
        REALM_ASSERT(m_sess);
9,546✔
1596
        ClientImpl::Connection& conn = m_sess->get_connection();
9,546✔
1597
        conn.initiate_session_deactivation(m_sess); // Throws
9,546✔
1598

4,598✔
1599
        // Delete the pending bootstrap store since it uses a reference to the logger in m_sess
4,598✔
1600
        m_flx_pending_bootstrap_store.reset();
9,546✔
1601
        // Clear the subscription and migration store refs since they are owned by SyncSession
4,598✔
1602
        m_flx_subscription_store.reset();
9,546✔
1603
        m_migration_store.reset();
9,546✔
1604
        m_sess = nullptr;
9,546✔
1605
    }
9,546✔
1606

4,680✔
1607
    // The Realm file can be closed now, as no access to the Realm file is
4,680✔
1608
    // supposed to happen on behalf of a session after initiation of
4,680✔
1609
    // deactivation.
4,680✔
1610
    m_db->release_sync_agent();
9,708✔
1611
    m_db = nullptr;
9,708✔
1612

4,680✔
1613
    // All outstanding wait operations must be canceled
4,680✔
1614
    while (!m_upload_completion_handlers.empty()) {
10,090✔
1615
        auto handler = std::move(m_upload_completion_handlers.back());
382✔
1616
        m_upload_completion_handlers.pop_back();
382✔
1617
        handler(
382✔
1618
            {ErrorCodes::OperationAborted, "Sync session is being finalized before upload was complete"}); // Throws
382✔
1619
    }
382✔
1620
    while (!m_download_completion_handlers.empty()) {
9,866✔
1621
        auto handler = std::move(m_download_completion_handlers.back());
158✔
1622
        m_download_completion_handlers.pop_back();
158✔
1623
        handler(
158✔
1624
            {ErrorCodes::OperationAborted, "Sync session is being finalized before download was complete"}); // Throws
158✔
1625
    }
158✔
1626
    while (!m_sync_completion_handlers.empty()) {
9,722✔
1627
        auto handler = std::move(m_sync_completion_handlers.back());
14✔
1628
        m_sync_completion_handlers.pop_back();
14✔
1629
        handler({ErrorCodes::OperationAborted, "Sync session is being finalized before sync was complete"}); // Throws
14✔
1630
    }
14✔
1631
}
9,708✔
1632

1633

1634
// Must be called only when an unactualized session wrapper becomes abandoned.
1635
//
1636
// Called with a lock on `m_client.m_mutex`.
1637
inline void SessionWrapper::finalize_before_actualization() noexcept
1638
{
176✔
1639
    REALM_ASSERT(!m_sess);
176✔
1640
    m_actualized = true;
176✔
1641
    m_force_closed = true;
176✔
1642
}
176✔
1643

1644

1645
inline void SessionWrapper::on_sync_progress()
1646
{
45,204✔
1647
    REALM_ASSERT(!m_finalized);
45,204✔
1648
    m_reliable_download_progress = true;
45,204✔
1649
    report_progress(); // Throws
45,204✔
1650
}
45,204✔
1651

1652

1653
void SessionWrapper::on_upload_completion()
1654
{
14,670✔
1655
    REALM_ASSERT(!m_finalized);
14,670✔
1656
    while (!m_upload_completion_handlers.empty()) {
16,384✔
1657
        auto handler = std::move(m_upload_completion_handlers.back());
1,714✔
1658
        m_upload_completion_handlers.pop_back();
1,714✔
1659
        handler(Status::OK()); // Throws
1,714✔
1660
    }
1,714✔
1661
    while (!m_sync_completion_handlers.empty()) {
14,854✔
1662
        auto handler = std::move(m_sync_completion_handlers.back());
184✔
1663
        m_download_completion_handlers.push_back(std::move(handler)); // Throws
184✔
1664
        m_sync_completion_handlers.pop_back();
184✔
1665
    }
184✔
1666
    std::lock_guard lock{m_client.m_mutex};
14,670✔
1667
    if (m_staged_upload_mark > m_reached_upload_mark) {
14,670✔
1668
        m_reached_upload_mark = m_staged_upload_mark;
12,958✔
1669
        m_client.m_wait_or_client_stopped_cond.notify_all();
12,958✔
1670
    }
12,958✔
1671
}
14,670✔
1672

1673

1674
void SessionWrapper::on_download_completion()
1675
{
17,032✔
1676
    while (!m_download_completion_handlers.empty()) {
19,084✔
1677
        auto handler = std::move(m_download_completion_handlers.back());
2,052✔
1678
        m_download_completion_handlers.pop_back();
2,052✔
1679
        handler(Status::OK()); // Throws
2,052✔
1680
    }
2,052✔
1681
    while (!m_sync_completion_handlers.empty()) {
17,112✔
1682
        auto handler = std::move(m_sync_completion_handlers.back());
80✔
1683
        m_upload_completion_handlers.push_back(std::move(handler)); // Throws
80✔
1684
        m_sync_completion_handlers.pop_back();
80✔
1685
    }
80✔
1686

7,644✔
1687
    if (m_flx_subscription_store && m_flx_pending_mark_version != SubscriptionSet::EmptyVersion) {
17,032✔
1688
        m_sess->logger.debug("Marking query version %1 as complete after receiving MARK message",
682✔
1689
                             m_flx_pending_mark_version);
682✔
1690
        auto mutable_subs = m_flx_subscription_store->get_mutable_by_version(m_flx_pending_mark_version);
682✔
1691
        mutable_subs.update_state(SubscriptionSet::State::Complete);
682✔
1692
        mutable_subs.commit();
682✔
1693
        m_flx_pending_mark_version = SubscriptionSet::EmptyVersion;
682✔
1694
    }
682✔
1695

7,644✔
1696
    std::lock_guard lock{m_client.m_mutex};
17,032✔
1697
    if (m_staged_download_mark > m_reached_download_mark) {
17,032✔
1698
        m_reached_download_mark = m_staged_download_mark;
9,962✔
1699
        m_client.m_wait_or_client_stopped_cond.notify_all();
9,962✔
1700
    }
9,962✔
1701
}
17,032✔
1702

1703

1704
void SessionWrapper::on_suspended(const SessionErrorInfo& error_info)
1705
{
924✔
1706
    REALM_ASSERT(!m_finalized);
924✔
1707
    m_suspended = true;
924✔
1708
    if (m_connection_state_change_listener) {
924✔
1709
        m_connection_state_change_listener(ConnectionState::disconnected, error_info); // Throws
924✔
1710
    }
924✔
1711
}
924✔
1712

1713

1714
void SessionWrapper::on_resumed()
1715
{
382✔
1716
    REALM_ASSERT(!m_finalized);
382✔
1717
    m_suspended = false;
382✔
1718
    if (m_connection_state_change_listener) {
382✔
1719
        ClientImpl::Connection& conn = m_sess->get_connection();
382✔
1720
        if (conn.get_state() != ConnectionState::disconnected) {
382✔
1721
            m_connection_state_change_listener(ConnectionState::connecting, util::none); // Throws
382✔
1722
            if (conn.get_state() == ConnectionState::connected)
382✔
1723
                m_connection_state_change_listener(ConnectionState::connected, util::none); // Throws
380✔
1724
        }
382✔
1725
    }
382✔
1726
}
382✔
1727

1728

1729
void SessionWrapper::on_connection_state_changed(ConnectionState state,
1730
                                                 const util::Optional<SessionErrorInfo>& error_info)
1731
{
10,114✔
1732
    if (m_connection_state_change_listener) {
10,114✔
1733
        if (!m_suspended)
10,096✔
1734
            m_connection_state_change_listener(state, error_info); // Throws
10,098✔
1735
    }
10,096✔
1736
}
10,114✔
1737

1738

1739
void SessionWrapper::report_progress(bool only_if_new_uploadable_data)
1740
{
161,034✔
1741
    REALM_ASSERT(!m_finalized);
161,034✔
1742
    REALM_ASSERT(m_sess);
161,034✔
1743

81,292✔
1744
    if (!m_progress_handler)
161,034✔
1745
        return;
115,396✔
1746

20,940✔
1747
    std::uint_fast64_t downloaded_bytes = 0;
45,638✔
1748
    std::uint_fast64_t downloadable_bytes = 0;
45,638✔
1749
    std::uint_fast64_t uploaded_bytes = 0;
45,638✔
1750
    std::uint_fast64_t uploadable_bytes = 0;
45,638✔
1751
    std::uint_fast64_t snapshot_version = 0;
45,638✔
1752
    ClientHistory::get_upload_download_bytes(m_db.get(), downloaded_bytes, downloadable_bytes, uploaded_bytes,
45,638✔
1753
                                             uploadable_bytes, snapshot_version);
45,638✔
1754

20,940✔
1755
    // If this progress notification was triggered by a commit being made we
20,940✔
1756
    // only want to send it if the uploadable bytes has actually increased,
20,940✔
1757
    // and not if it was an empty commit.
20,940✔
1758
    if (only_if_new_uploadable_data && m_last_reported_uploadable_bytes == uploadable_bytes)
45,638✔
1759
        return;
31,952✔
1760
    m_last_reported_uploadable_bytes = uploadable_bytes;
13,686✔
1761

5,780✔
1762
    // uploadable_bytes is uploaded + remaining to upload, while downloadable_bytes
5,780✔
1763
    // is only the remaining to download. This is confusing, so make them use
5,780✔
1764
    // the same units.
5,780✔
1765
    std::uint_fast64_t total_bytes = downloaded_bytes + downloadable_bytes;
13,686✔
1766

5,780✔
1767
    m_sess->logger.debug("Progress handler called, downloaded = %1, "
13,686✔
1768
                         "downloadable(total) = %2, uploaded = %3, "
13,686✔
1769
                         "uploadable = %4, reliable_download_progress = %5, "
13,686✔
1770
                         "snapshot version = %6",
13,686✔
1771
                         downloaded_bytes, total_bytes, uploaded_bytes, uploadable_bytes,
13,686✔
1772
                         m_reliable_download_progress, snapshot_version);
13,686✔
1773

5,780✔
1774
    // FIXME: Why is this boolean status communicated to the application as
5,780✔
1775
    // a 64-bit integer? Also, the name `progress_version` is confusing.
5,780✔
1776
    std::uint_fast64_t progress_version = (m_reliable_download_progress ? 1 : 0);
10,128✔
1777
    m_progress_handler(downloaded_bytes, total_bytes, uploaded_bytes, uploadable_bytes, progress_version,
13,686✔
1778
                       snapshot_version);
13,686✔
1779
}
13,686✔
1780

1781
util::Future<std::string> SessionWrapper::send_test_command(std::string body)
1782
{
52✔
1783
    if (!m_sess) {
52✔
1784
        return Status{ErrorCodes::RuntimeError, "session must be activated to send a test command"};
×
1785
    }
×
1786

26✔
1787
    return m_sess->send_test_command(std::move(body));
52✔
1788
}
52✔
1789

1790
void SessionWrapper::handle_pending_client_reset_acknowledgement()
1791
{
292✔
1792
    REALM_ASSERT(!m_finalized);
292✔
1793

146✔
1794
    auto pending_reset = _impl::client_reset::has_pending_reset(*m_db->start_frozen());
292✔
1795
    REALM_ASSERT(pending_reset);
292✔
1796
    m_sess->logger.info("Tracking pending client reset of type \"%1\" from %2", pending_reset->type,
292✔
1797
                        pending_reset->time);
292✔
1798
    async_wait_for(true, true, [self = util::bind_ptr(this), pending_reset = *pending_reset](Status status) {
292✔
1799
        if (status == ErrorCodes::OperationAborted) {
292✔
1800
            return;
148✔
1801
        }
148✔
1802
        auto& logger = self->m_sess->logger;
144✔
1803
        if (!status.is_ok()) {
144✔
1804
            logger.error("Error while tracking client reset acknowledgement: %1", status);
×
1805
            return;
×
1806
        }
×
1807

72✔
1808
        auto wt = self->m_db->start_write();
144✔
1809
        auto cur_pending_reset = _impl::client_reset::has_pending_reset(*wt);
144✔
1810
        if (!cur_pending_reset) {
144✔
1811
            logger.debug(
×
1812
                "Was going to remove client reset tracker for type \"%1\" from %2, but it was already removed",
×
1813
                pending_reset.type, pending_reset.time);
×
1814
            return;
×
1815
        }
×
1816
        else if (cur_pending_reset->type != pending_reset.type || cur_pending_reset->time != pending_reset.time) {
144✔
1817
            logger.debug(
×
1818
                "Was going to remove client reset tracker for type \"%1\" from %2, but found type \"%3\" from %4.",
×
1819
                pending_reset.type, pending_reset.time, cur_pending_reset->type, cur_pending_reset->time);
×
1820
        }
×
1821
        else {
144✔
1822
            logger.debug("Client reset of type \"%1\" from %2 has been acknowledged by the server. "
144✔
1823
                         "Removing cycle detection tracker.",
144✔
1824
                         pending_reset.type, pending_reset.time);
144✔
1825
        }
144✔
1826
        _impl::client_reset::remove_pending_client_resets(*wt);
144✔
1827
        wt->commit();
144✔
1828
    });
144✔
1829
}
292✔
1830

1831
void SessionWrapper::update_subscription_version_info()
1832
{
11,300✔
1833
    if (!m_flx_subscription_store)
11,300✔
1834
        return;
10,162✔
1835
    auto versions_info = m_flx_subscription_store->get_version_info();
1,138✔
1836
    m_flx_active_version = versions_info.active;
1,138✔
1837
    m_flx_pending_mark_version = versions_info.pending_mark;
1,138✔
1838
}
1,138✔
1839

1840
std::string SessionWrapper::get_appservices_connection_id()
1841
{
72✔
1842
    auto pf = util::make_promise_future<std::string>();
72✔
1843
    REALM_ASSERT(m_initiated);
72✔
1844

36✔
1845
    util::bind_ptr<SessionWrapper> self(this);
72✔
1846
    get_client().post([self, promise = std::move(pf.promise)](Status status) mutable {
72✔
1847
        if (!status.is_ok()) {
72✔
1848
            promise.set_error(status);
×
1849
            return;
×
1850
        }
×
1851

36✔
1852
        if (!self->m_sess) {
72✔
1853
            promise.set_error({ErrorCodes::RuntimeError, "session already finalized"});
×
1854
            return;
×
1855
        }
×
1856

36✔
1857
        promise.emplace_value(self->m_sess->get_connection().get_active_appservices_connection_id());
72✔
1858
    });
72✔
1859

36✔
1860
    return pf.future.get();
72✔
1861
}
72✔
1862

1863
// ################ ClientImpl::Connection ################
1864

1865
ClientImpl::Connection::Connection(ClientImpl& client, connection_ident_type ident, ServerEndpoint endpoint,
1866
                                   const std::string& authorization_header_name,
1867
                                   const std::map<std::string, std::string>& custom_http_headers,
1868
                                   bool verify_servers_ssl_certificate,
1869
                                   Optional<std::string> ssl_trust_certificate_path,
1870
                                   std::function<SSLVerifyCallback> ssl_verify_callback,
1871
                                   Optional<ProxyConfig> proxy_config, ReconnectInfo reconnect_info)
1872
    : logger_ptr{std::make_shared<util::PrefixLogger>(make_logger_prefix(ident), client.logger_ptr)} // Throws
1873
    , logger{*logger_ptr}
1874
    , m_client{client}
1875
    , m_verify_servers_ssl_certificate{verify_servers_ssl_certificate}    // DEPRECATED
1876
    , m_ssl_trust_certificate_path{std::move(ssl_trust_certificate_path)} // DEPRECATED
1877
    , m_ssl_verify_callback{std::move(ssl_verify_callback)}               // DEPRECATED
1878
    , m_proxy_config{std::move(proxy_config)}                             // DEPRECATED
1879
    , m_reconnect_info{reconnect_info}
1880
    , m_session_history{}
1881
    , m_ident{ident}
1882
    , m_server_endpoint{std::move(endpoint)}
1883
    , m_authorization_header_name{authorization_header_name} // DEPRECATED
1884
    , m_custom_http_headers{custom_http_headers}             // DEPRECATED
1885
{
2,518✔
1886
    m_on_idle = m_client.create_trigger([this](Status status) {
2,518✔
1887
        if (status == ErrorCodes::OperationAborted)
2,518✔
1888
            return;
×
1889
        else if (!status.is_ok())
2,518✔
1890
            throw Exception(status);
×
1891

1,190✔
1892
        REALM_ASSERT(m_activated);
2,518✔
1893
        if (m_state == ConnectionState::disconnected && m_num_active_sessions == 0) {
2,518✔
1894
            on_idle(); // Throws
2,516✔
1895
            // Connection object may be destroyed now.
1,188✔
1896
        }
2,516✔
1897
    });
2,518✔
1898
}
2,518✔
1899

1900
inline connection_ident_type ClientImpl::Connection::get_ident() const noexcept
1901
{
12✔
1902
    return m_ident;
12✔
1903
}
12✔
1904

1905

1906
inline const ServerEndpoint& ClientImpl::Connection::get_server_endpoint() const noexcept
1907
{
2,516✔
1908
    return m_server_endpoint;
2,516✔
1909
}
2,516✔
1910

1911
inline void ClientImpl::Connection::update_connect_info(const std::string& http_request_path_prefix,
1912
                                                        const std::string& signed_access_token)
1913
{
9,908✔
1914
    m_http_request_path_prefix = http_request_path_prefix; // Throws (copy)
9,908✔
1915
    m_signed_access_token = signed_access_token;           // Throws (copy)
9,908✔
1916
}
9,908✔
1917

1918

1919
void ClientImpl::Connection::resume_active_sessions()
1920
{
1,532✔
1921
    auto handler = [=](ClientImpl::Session& sess) {
3,060✔
1922
        sess.cancel_resumption_delay(); // Throws
3,060✔
1923
    };
3,060✔
1924
    for_each_active_session(std::move(handler)); // Throws
1,532✔
1925
}
1,532✔
1926

1927
void ClientImpl::Connection::on_idle()
1928
{
2,512✔
1929
    logger.debug("Destroying connection object");
2,512✔
1930
    ClientImpl& client = get_client();
2,512✔
1931
    client.remove_connection(*this);
2,512✔
1932
    // NOTE: This connection object is now destroyed!
1,188✔
1933
}
2,512✔
1934

1935

1936
std::string ClientImpl::Connection::get_http_request_path() const
1937
{
3,262✔
1938
    using namespace std::string_view_literals;
3,262✔
1939
    const auto param = m_http_request_path_prefix.find('?') == std::string::npos ? "?baas_at="sv : "&baas_at="sv;
3,260✔
1940

1,606✔
1941
    std::string path;
3,262✔
1942
    path.reserve(m_http_request_path_prefix.size() + param.size() + m_signed_access_token.size());
3,262✔
1943
    path += m_http_request_path_prefix;
3,262✔
1944
    path += param;
3,262✔
1945
    path += m_signed_access_token;
3,262✔
1946

1,606✔
1947
    return path;
3,262✔
1948
}
3,262✔
1949

1950

1951
std::string ClientImpl::Connection::make_logger_prefix(connection_ident_type ident)
1952
{
2,518✔
1953
    std::ostringstream out;
2,518✔
1954
    out.imbue(std::locale::classic());
2,518✔
1955
    out << "Connection[" << ident << "]: "; // Throws
2,518✔
1956
    return out.str();                       // Throws
2,518✔
1957
}
2,518✔
1958

1959

1960
void ClientImpl::Connection::report_connection_state_change(ConnectionState state,
1961
                                                            util::Optional<SessionErrorInfo> error_info)
1962
{
9,688✔
1963
    if (m_force_closed) {
9,688✔
1964
        return;
2,220✔
1965
    }
2,220✔
1966
    auto handler = [=](ClientImpl::Session& sess) {
10,034✔
1967
        SessionImpl& sess_2 = static_cast<SessionImpl&>(sess);
10,034✔
1968
        sess_2.on_connection_state_changed(state, error_info); // Throws
10,034✔
1969
    };
10,034✔
1970
    for_each_active_session(std::move(handler)); // Throws
7,468✔
1971
}
7,468✔
1972

1973

1974
Client::Client(Config config)
1975
    : m_impl{new ClientImpl{std::move(config)}} // Throws
1976
{
9,482✔
1977
}
9,482✔
1978

1979

1980
Client::Client(Client&& client) noexcept
1981
    : m_impl{std::move(client.m_impl)}
1982
{
×
1983
}
×
1984

1985

1986
Client::~Client() noexcept {}
9,482✔
1987

1988

1989
void Client::shutdown() noexcept
1990
{
9,560✔
1991
    m_impl->shutdown();
9,560✔
1992
}
9,560✔
1993

1994
void Client::shutdown_and_wait()
1995
{
760✔
1996
    m_impl->shutdown_and_wait();
760✔
1997
}
760✔
1998

1999
void Client::cancel_reconnect_delay()
2000
{
1,536✔
2001
    m_impl->cancel_reconnect_delay();
1,536✔
2002
}
1,536✔
2003

2004
void Client::voluntary_disconnect_all_connections()
2005
{
12✔
2006
    m_impl->voluntary_disconnect_all_connections();
12✔
2007
}
12✔
2008

2009
bool Client::wait_for_session_terminations_or_client_stopped()
2010
{
372✔
2011
    return m_impl.get()->wait_for_session_terminations_or_client_stopped();
372✔
2012
}
372✔
2013

2014

2015
bool Client::decompose_server_url(const std::string& url, ProtocolEnvelope& protocol, std::string& address,
2016
                                  port_type& port, std::string& path) const
2017
{
3,420✔
2018
    return m_impl->decompose_server_url(url, protocol, address, port, path); // Throws
3,420✔
2019
}
3,420✔
2020

2021

2022
Session::Session(Client& client, DBRef db, std::shared_ptr<SubscriptionStore> flx_sub_store,
2023
                 std::shared_ptr<MigrationStore> migration_store, Config&& config)
2024
{
11,028✔
2025
    util::bind_ptr<SessionWrapper> sess;
11,028✔
2026
    sess.reset(new SessionWrapper{*client.m_impl, std::move(db), std::move(flx_sub_store), std::move(migration_store),
11,028✔
2027
                                  std::move(config)}); // Throws
11,028✔
2028
    // The reference count passed back to the application is implicitly
5,342✔
2029
    // owned by a naked pointer. This is done to avoid exposing
5,342✔
2030
    // implementation details through the header file (that is, through the
5,342✔
2031
    // Session object).
5,342✔
2032
    m_impl = sess.release();
11,028✔
2033
}
11,028✔
2034

2035

2036
void Session::set_progress_handler(util::UniqueFunction<ProgressHandler> handler)
2037
{
3,496✔
2038
    m_impl->set_progress_handler(std::move(handler)); // Throws
3,496✔
2039
}
3,496✔
2040

2041

2042
void Session::set_connection_state_change_listener(util::UniqueFunction<ConnectionStateChangeListener> listener)
2043
{
11,124✔
2044
    m_impl->set_connection_state_change_listener(std::move(listener)); // Throws
11,124✔
2045
}
11,124✔
2046

2047

2048
void Session::bind()
2049
{
9,876✔
2050
    m_impl->initiate(); // Throws
9,876✔
2051
}
9,876✔
2052

2053

2054
void Session::nonsync_transact_notify(version_type new_version)
2055
{
15,270✔
2056
    m_impl->on_commit(new_version); // Throws
15,270✔
2057
}
15,270✔
2058

2059

2060
void Session::cancel_reconnect_delay()
2061
{
12✔
2062
    m_impl->cancel_reconnect_delay(); // Throws
12✔
2063
}
12✔
2064

2065

2066
void Session::async_wait_for(bool upload_completion, bool download_completion, WaitOperCompletionHandler handler)
2067
{
4,098✔
2068
    m_impl->async_wait_for(upload_completion, download_completion, std::move(handler)); // Throws
4,098✔
2069
}
4,098✔
2070

2071

2072
bool Session::wait_for_upload_complete_or_client_stopped()
2073
{
12,996✔
2074
    return m_impl->wait_for_upload_complete_or_client_stopped(); // Throws
12,996✔
2075
}
12,996✔
2076

2077

2078
bool Session::wait_for_download_complete_or_client_stopped()
2079
{
10,092✔
2080
    return m_impl->wait_for_download_complete_or_client_stopped(); // Throws
10,092✔
2081
}
10,092✔
2082

2083

2084
void Session::refresh(const std::string& signed_access_token)
2085
{
208✔
2086
    m_impl->refresh(signed_access_token); // Throws
208✔
2087
}
208✔
2088

2089

2090
void Session::abandon() noexcept
2091
{
11,028✔
2092
    REALM_ASSERT(m_impl);
11,028✔
2093
    // Reabsorb the ownership assigned to the applications naked pointer by
5,342✔
2094
    // Session constructor
5,342✔
2095
    util::bind_ptr<SessionWrapper> wrapper{m_impl, util::bind_ptr_base::adopt_tag{}};
11,028✔
2096
    SessionWrapper::abandon(std::move(wrapper));
11,028✔
2097
}
11,028✔
2098

2099
util::Future<std::string> Session::send_test_command(std::string body)
2100
{
52✔
2101
    return m_impl->send_test_command(std::move(body));
52✔
2102
}
52✔
2103

2104
std::string Session::get_appservices_connection_id()
2105
{
72✔
2106
    return m_impl->get_appservices_connection_id();
72✔
2107
}
72✔
2108

2109
std::ostream& operator<<(std::ostream& os, ProxyConfig::Type proxyType)
2110
{
×
2111
    switch (proxyType) {
×
2112
        case ProxyConfig::Type::HTTP:
×
2113
            return os << "HTTP";
×
2114
        case ProxyConfig::Type::HTTPS:
×
2115
            return os << "HTTPS";
×
2116
    }
×
2117
    REALM_TERMINATE("Invalid Proxy Type object.");
2118
}
×
2119

2120
} // namespace sync
2121
} // namespace realm
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc