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

realm / realm-core / 1859

23 Nov 2023 09:42AM UTC coverage: 91.66% (-0.01%) from 91.671%
1859

push

Evergreen

web-flow
Rework KeyPathArray filters for notifications in the C-API. (#7087)

* Add support for keypaths crossing backlins

---------

Co-authored-by: Jørgen Edelbo <jorgen.edelbo@mongodb.com>

92322 of 169178 branches covered (0.0%)

151 of 155 new or added lines in 4 files covered. (97.42%)

77 existing lines in 13 files now uncovered.

231381 of 252435 relevant lines covered (91.66%)

6404136.77 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,684✔
270
    REALM_ASSERT(!w->m_next);
9,684✔
271
    w->m_next = m_back;
9,684✔
272
    m_back = w.release();
9,684✔
273
}
9,684✔
274

275

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

286

287
inline void SessionWrapperStack::clear() noexcept
288
{
23,092✔
289
    while (m_back) {
23,092✔
290
        util::bind_ptr<SessionWrapper> w{m_back, util::bind_ptr_base::adopt_tag{}};
×
291
        m_back = w->m_next;
×
292
    }
×
293
}
23,092✔
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,092✔
305
    clear();
23,092✔
306
}
23,092✔
307

308

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

311

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

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

324

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

1,046✔
334
        for (auto& p : m_server_slots) {
1,916✔
335
            ServerSlot& slot = p.second;
1,916✔
336
            if (m_one_connection_per_session) {
1,916✔
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,916✔
345
                REALM_ASSERT(slot.alt_connections.empty());
1,916✔
346
                if (slot.connection) {
1,916✔
347
                    ClientImpl::Connection& conn = *slot.connection;
1,912✔
348
                    conn.resume_active_sessions(); // Throws
1,912✔
349
                    conn.cancel_reconnect_delay(); // Throws
1,912✔
350
                }
1,912✔
351
                else {
4✔
352
                    slot.reconnect_info.reset();
4✔
353
                }
4✔
354
            }
1,916✔
355
        }
1,916✔
356
    }); // Throws
1,916✔
357
}
1,916✔
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
{
370✔
408
    // Thread safety required
34✔
409

34✔
410
    {
370✔
411
        std::lock_guard lock{m_mutex};
370✔
412
        m_sessions_terminated = false;
370✔
413
    }
370✔
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 {
370✔
433
        if (status == ErrorCodes::OperationAborted)
370✔
434
            return;
×
435
        else if (!status.is_ok())
370✔
436
            throw Exception(status);
×
437

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

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

453

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

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

4,424✔
470
    logger.debug("Waiting for %1 connections to drain", m_num_connections);
8,986✔
471
    m_drain_cv.wait(lock, [&] {
19,856✔
472
        return m_num_connections == 0 && m_outstanding_posts == 0;
19,856✔
473
    });
19,856✔
474

4,424✔
475
    m_drained = true;
8,986✔
476
}
8,986✔
477

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

4,422✔
488
    drain_connections_on_loop();
8,982✔
489
}
8,982✔
490

491

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

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

514

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

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

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

542

543
// Must be called from the event loop thread.
544
void ClientImpl::actualize_and_finalize_session_wrappers()
545
{
14,108✔
546
    std::map<SessionWrapper*, ServerEndpoint> unactualized_session_wrappers;
14,108✔
547
    SessionWrapperStack abandoned_session_wrappers;
14,108✔
548
    bool stopped;
14,108✔
549
    {
14,108✔
550
        std::lock_guard lock{m_mutex};
14,108✔
551
        m_actualize_and_finalize_needed = false;
14,108✔
552
        swap(m_unactualized_session_wrappers, unactualized_session_wrappers);
14,108✔
553
        swap(m_abandoned_session_wrappers, abandoned_session_wrappers);
14,108✔
554
        stopped = m_stopped;
14,108✔
555
    }
14,108✔
556
    // Note, we need to finalize old session wrappers before we actualize new
6,744✔
557
    // ones. This ensures that deactivation of old sessions is initiated before
6,744✔
558
    // new session are activated. This, in turn, ensures that the server does
6,744✔
559
    // not see two overlapping sessions for the same local Realm file.
6,744✔
560
    while (util::bind_ptr<SessionWrapper> wrapper = abandoned_session_wrappers.pop())
23,790✔
561
        wrapper->finalize(); // Throws
9,682✔
562
    if (stopped) {
14,108✔
563
        for (auto& p : unactualized_session_wrappers) {
404✔
564
            SessionWrapper& wrapper = *p.first;
4✔
565
            wrapper.finalize_before_actualization();
4✔
566
        }
4✔
567
        return;
758✔
568
    }
758✔
569
    for (auto& p : unactualized_session_wrappers) {
13,350✔
570
        SessionWrapper& wrapper = *p.first;
9,680✔
571
        ServerEndpoint server_endpoint = std::move(p.second);
9,680✔
572
        wrapper.actualize(std::move(server_endpoint)); // Throws
9,680✔
573
    }
9,680✔
574
}
13,350✔
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,676✔
585
    auto&& [server_slot_it, inserted] =
9,676✔
586
        m_server_slots.try_emplace(endpoint, ReconnectInfo(m_reconnect_mode, m_reconnect_backoff_info, get_random()));
9,676✔
587
    ServerSlot& server_slot = server_slot_it->second; // Throws
9,676✔
588

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

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

619

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

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

649

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

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

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

669

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

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

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

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

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

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

710
SessionReason SessionImpl::get_session_reason() noexcept
711
{
1,404✔
712
    // Can only be called if the session is active or being activated
720✔
713
    REALM_ASSERT_EX(m_state == State::Active || m_state == State::Unactivated, m_state);
1,404!
714
    return m_wrapper.m_session_reason;
1,404✔
715
}
1,404✔
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,018✔
720
    // Ignore the call if the session is not active
23,030✔
721
    if (m_state != State::Active) {
43,018✔
722
        return;
×
723
    }
×
724

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

748

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

757

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

766

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

775

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

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

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

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

23,850✔
808
    if (is_steady_state_download_message(batch_state, query_version)) {
44,658✔
809
        return false;
43,018✔
810
    }
43,018✔
811

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

820✔
818
    bool new_batch = false;
1,640✔
819
    try {
1,640✔
820
        bootstrap_store->add_batch(query_version, std::move(maybe_progress), received_changesets, &new_batch);
1,640✔
821
    }
1,640✔
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,640✔
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,640✔
840
                                       batch_state, received_changesets.size());
1,640✔
841
    if (hook_action == SyncClientHookAction::EarlyReturn) {
1,640✔
842
        return true;
12✔
843
    }
12✔
844
    REALM_ASSERT_EX(hook_action == SyncClientHookAction::NoAction, hook_action);
1,628✔
845

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

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

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

860

861
void SessionImpl::process_pending_flx_bootstrap()
862
{
11,146✔
863
    // Ignore the call if not a flx session or session is not active
5,396✔
864
    if (!m_is_flx_sync_session || m_state != State::Active) {
11,146✔
865
        return;
8,638✔
866
    }
8,638✔
867
    // Should never be called if session is not active
1,254✔
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,020✔
872
    }
1,020✔
873

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

744✔
885
    // Used to commit each batch after it was transformed.
744✔
886
    TransactionRef transact = get_db()->start_write();
1,488✔
887
    while (bootstrap_store->has_pending()) {
3,092✔
888
        auto start_time = std::chrono::steady_clock::now();
1,620✔
889
        auto pending_batch = bootstrap_store->peek_pending(m_wrapper.m_flx_bootstrap_batch_size_bytes);
1,620✔
890
        if (!pending_batch.progress) {
1,620✔
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,612✔
900
            pending_batch.remaining_changesets > 0 ? DownloadBatchState::MoreToCome : DownloadBatchState::LastInBatch;
1,544✔
901
        uint64_t downloadable_bytes = 0;
1,612✔
902
        query_version = pending_batch.query_version;
1,612✔
903
        bool simulate_integration_error =
1,612✔
904
            (m_wrapper.m_simulate_integration_error && !pending_batch.changesets.empty());
1,612✔
905
        if (simulate_integration_error) {
1,612✔
906
            throw IntegrationException(ErrorCodes::BadChangeset, "simulated failure", ProtocolError::bad_changeset);
8✔
907
        }
8✔
908

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

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

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

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

736✔
935
    auto action = call_debug_hook(SyncClientHookEvent::BootstrapProcessed, progress, query_version,
1,472✔
936
                                  DownloadBatchState::LastInBatch, changesets_processed);
1,472✔
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,472✔
939
}
1,472✔
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,508✔
951
    // Ignore the call if the session is not active
754✔
952
    if (m_state == State::Active) {
1,508✔
953
        m_wrapper.on_flx_sync_progress(version, batch_state);
1,508✔
954
    }
1,508✔
955
}
1,508✔
956

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

964
MigrationStore* SessionImpl::get_migration_store()
965
{
59,710✔
966
    // Should never be called if session is not active
31,618✔
967
    REALM_ASSERT_EX(m_state == State::Active, m_state);
59,710✔
968
    return m_wrapper.get_migration_store();
59,710✔
969
}
59,710✔
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
{
840✔
981
    // Should never be called if session is not active
424✔
982
    REALM_ASSERT_EX(m_state == State::Active, m_state);
840✔
983

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

424✔
993
    auto action = m_wrapper.m_debug_hook(data);
840✔
994
    switch (action) {
840✔
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:
816✔
1008
            return action;
816✔
1009
    }
840✔
1010
}
840✔
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
{
92,398✔
1016
    if (REALM_LIKELY(!m_wrapper.m_debug_hook)) {
92,398✔
1017
        return SyncClientHookAction::NoAction;
91,636✔
1018
    }
91,636✔
1019
    if (REALM_UNLIKELY(m_state != State::Active)) {
762✔
1020
        return SyncClientHookAction::NoAction;
×
1021
    }
×
1022

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

378✔
1030
    return call_debug_hook(data);
762✔
1031
}
762✔
1032

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

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

44✔
1050
    return call_debug_hook(data);
80✔
1051
}
80✔
1052

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

24,676✔
1061
    if (!m_is_flx_sync_session) {
46,310✔
1062
        return true;
42,132✔
1063
    }
42,132✔
1064

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

1,644✔
1070
    return false;
3,288✔
1071
}
3,288✔
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,006✔
1140
    REALM_ASSERT(m_db);
11,006✔
1141
    REALM_ASSERT(m_db->get_replication());
11,006✔
1142
    REALM_ASSERT(dynamic_cast<ClientReplication*>(m_db->get_replication()));
11,006✔
1143
    if (m_client_reset_config) {
11,006✔
1144
        m_session_reason = SessionReason::ClientReset;
344✔
1145
    }
344✔
1146

5,330✔
1147
    update_subscription_version_info();
11,006✔
1148
}
11,006✔
1149

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

1158

1159
inline ClientReplication& SessionWrapper::get_replication() noexcept
1160
{
111,870✔
1161
    REALM_ASSERT(m_db);
111,870✔
1162
    return static_cast<ClientReplication&>(*m_replication);
111,870✔
1163
}
111,870✔
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,508✔
1173
    return static_cast<bool>(m_flx_subscription_store);
1,508✔
1174
}
1,508✔
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,700✔
1186
    REALM_ASSERT(!m_finalized);
1,700✔
1187
    m_flx_last_seen_version = version;
1,700✔
1188
    m_flx_active_version = version;
1,700✔
1189
}
1,700✔
1190

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

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

754✔
1203
    switch (batch_state) {
1,508✔
1204
        case DownloadBatchState::SteadyState:
✔
1205
            // Cannot be called with this value.
1206
            REALM_UNREACHABLE();
1207
        case DownloadBatchState::LastInBatch:
1,468✔
1208
            if (m_flx_active_version == new_version) {
1,468✔
1209
                return;
×
1210
            }
×
1211
            on_flx_sync_version_complete(new_version);
1,468✔
1212
            if (new_version == 0) {
1,468✔
1213
                new_state = SubscriptionSet::State::Complete;
672✔
1214
            }
672✔
1215
            else {
796✔
1216
                new_state = SubscriptionSet::State::AwaitingMark;
796✔
1217
                m_flx_pending_mark_version = new_version;
796✔
1218
            }
796✔
1219
            break;
1,468✔
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,508✔
1229

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

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

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

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

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

1259

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

1267

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

1277

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

53,898✔
1283
    if (REALM_UNLIKELY(m_finalized || m_force_closed)) {
106,506✔
1284
        return;
4✔
1285
    }
4✔
1286

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

53,894✔
1294
        REALM_ASSERT(self->m_actualized);
106,500✔
1295
        if (REALM_UNLIKELY(!self->m_sess))
106,500✔
1296
            return; // Already finalized
54,376✔
1297
        SessionImpl& sess = *self->m_sess;
105,628✔
1298
        sess.recognize_sync_version(new_version); // Throws
105,628✔
1299
        bool only_if_new_uploadable_data = true;
105,628✔
1300
        self->report_progress(only_if_new_uploadable_data); // Throws
105,628✔
1301
    });
105,628✔
1302
}
106,502✔
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,342✔
1334
    REALM_ASSERT(upload_completion || download_completion);
4,342✔
1335
    REALM_ASSERT(m_initiated);
4,342✔
1336
    REALM_ASSERT(!m_finalized);
4,342✔
1337

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

2,084✔
1346
        REALM_ASSERT(self->m_actualized);
4,342✔
1347
        if (REALM_UNLIKELY(!self->m_sess)) {
4,342✔
1348
            // Already finalized
42✔
1349
            handler({ErrorCodes::OperationAborted, "Session finalized before callback could run"}); // Throws
74✔
1350
            return;
74✔
1351
        }
74✔
1352
        if (upload_completion) {
4,268✔
1353
            if (download_completion) {
2,270✔
1354
                // Wait for upload and download completion
136✔
1355
                self->m_sync_completion_handlers.push_back(std::move(handler)); // Throws
272✔
1356
            }
272✔
1357
            else {
1,998✔
1358
                // Wait for upload completion only
908✔
1359
                self->m_upload_completion_handlers.push_back(std::move(handler)); // Throws
1,998✔
1360
            }
1,998✔
1361
        }
2,270✔
1362
        else {
1,998✔
1363
            // Wait for download completion only
998✔
1364
            self->m_download_completion_handlers.push_back(std::move(handler)); // Throws
1,998✔
1365
        }
1,998✔
1366
        SessionImpl& sess = *self->m_sess;
4,268✔
1367
        if (upload_completion)
4,268✔
1368
            sess.request_upload_completion_notification(); // Throws
2,270✔
1369
        if (download_completion)
4,268✔
1370
            sess.request_download_completion_notification(); // Throws
2,270✔
1371
    });                                                      // Throws
4,268✔
1372
}
4,342✔
1373

1374

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

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

6,498✔
1387
    util::bind_ptr<SessionWrapper> self{this};
12,994✔
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,510✔
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,994✔
1409
    {
12,994✔
1410
        std::unique_lock lock{m_client.m_mutex};
12,994✔
1411
        while (m_reached_upload_mark < target_mark && !m_client.m_stopped)
32,784✔
1412
            m_client.m_wait_or_client_stopped_cond.wait(lock);
19,790✔
1413
        completion_condition_was_satisfied = !m_client.m_stopped;
12,994✔
1414
    }
12,994✔
1415
    return completion_condition_was_satisfied;
12,994✔
1416
}
12,994✔
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,496✔
1456
            m_client.m_wait_or_client_stopped_cond.wait(lock);
10,404✔
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,004✔
1491
    if (wrapper->m_initiated) {
11,004✔
1492
        ClientImpl& client = wrapper->m_client;
9,854✔
1493
        client.register_abandoned_session_wrapper(std::move(wrapper));
9,854✔
1494
    }
9,854✔
1495
}
11,004✔
1496

1497

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

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

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

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

4,660✔
1540
    m_actualized = true;
9,674✔
1541
    if (was_created)
9,674✔
1542
        conn.activate(); // Throws
2,504✔
1543

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

4,660✔
1553
    if (!m_client_reset_config)
9,674✔
1554
        report_progress(); // Throws
9,334✔
1555
}
9,674✔
1556

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

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

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

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

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

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

4,664✔
1592
    m_finalized = true;
9,682✔
1593

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

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

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

4,664✔
1613
    // All outstanding wait operations must be canceled
4,664✔
1614
    while (!m_upload_completion_handlers.empty()) {
10,060✔
1615
        auto handler = std::move(m_upload_completion_handlers.back());
378✔
1616
        m_upload_completion_handlers.pop_back();
378✔
1617
        handler(
378✔
1618
            {ErrorCodes::OperationAborted, "Sync session is being finalized before upload was complete"}); // Throws
378✔
1619
    }
378✔
1620
    while (!m_download_completion_handlers.empty()) {
9,836✔
1621
        auto handler = std::move(m_download_completion_handlers.back());
154✔
1622
        m_download_completion_handlers.pop_back();
154✔
1623
        handler(
154✔
1624
            {ErrorCodes::OperationAborted, "Sync session is being finalized before download was complete"}); // Throws
154✔
1625
    }
154✔
1626
    while (!m_sync_completion_handlers.empty()) {
9,694✔
1627
        auto handler = std::move(m_sync_completion_handlers.back());
12✔
1628
        m_sync_completion_handlers.pop_back();
12✔
1629
        handler({ErrorCodes::OperationAborted, "Sync session is being finalized before sync was complete"}); // Throws
12✔
1630
    }
12✔
1631
}
9,682✔
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
{
178✔
1639
    REALM_ASSERT(!m_sess);
178✔
1640
    m_actualized = true;
178✔
1641
    m_force_closed = true;
178✔
1642
}
178✔
1643

1644

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

1652

1653
void SessionWrapper::on_upload_completion()
1654
{
14,652✔
1655
    REALM_ASSERT(!m_finalized);
14,652✔
1656
    while (!m_upload_completion_handlers.empty()) {
16,348✔
1657
        auto handler = std::move(m_upload_completion_handlers.back());
1,696✔
1658
        m_upload_completion_handlers.pop_back();
1,696✔
1659
        handler(Status::OK()); // Throws
1,696✔
1660
    }
1,696✔
1661
    while (!m_sync_completion_handlers.empty()) {
14,836✔
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,652✔
1667
    if (m_staged_upload_mark > m_reached_upload_mark) {
14,652✔
1668
        m_reached_upload_mark = m_staged_upload_mark;
12,956✔
1669
        m_client.m_wait_or_client_stopped_cond.notify_all();
12,956✔
1670
    }
12,956✔
1671
}
14,652✔
1672

1673

1674
void SessionWrapper::on_download_completion()
1675
{
15,458✔
1676
    while (!m_download_completion_handlers.empty()) {
17,486✔
1677
        auto handler = std::move(m_download_completion_handlers.back());
2,028✔
1678
        m_download_completion_handlers.pop_back();
2,028✔
1679
        handler(Status::OK()); // Throws
2,028✔
1680
    }
2,028✔
1681
    while (!m_sync_completion_handlers.empty()) {
15,534✔
1682
        auto handler = std::move(m_sync_completion_handlers.back());
76✔
1683
        m_upload_completion_handlers.push_back(std::move(handler)); // Throws
76✔
1684
        m_sync_completion_handlers.pop_back();
76✔
1685
    }
76✔
1686

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

7,622✔
1696
    std::lock_guard lock{m_client.m_mutex};
15,458✔
1697
    if (m_staged_download_mark > m_reached_download_mark) {
15,458✔
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
}
15,458✔
1702

1703

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

1713

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

1728

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

1738

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

81,762✔
1744
    if (!m_progress_handler)
159,452✔
1745
        return;
114,124✔
1746

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

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

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

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

5,716✔
1774
    // FIXME: Why is this boolean status communicated to the application as
5,716✔
1775
    // a 64-bit integer? Also, the name `progress_version` is confusing.
5,716✔
1776
    std::uint_fast64_t progress_version = (m_reliable_download_progress ? 1 : 0);
10,006✔
1777
    m_progress_handler(downloaded_bytes, total_bytes, uploaded_bytes, uploadable_bytes, progress_version,
13,558✔
1778
                       snapshot_version);
13,558✔
1779
}
13,558✔
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
{
288✔
1792
    REALM_ASSERT(!m_finalized);
288✔
1793

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

70✔
1808
        auto wt = self->m_db->start_write();
140✔
1809
        auto cur_pending_reset = _impl::client_reset::has_pending_reset(*wt);
140✔
1810
        if (!cur_pending_reset) {
140✔
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) {
140✔
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 {
140✔
1822
            logger.debug("Client reset of type \"%1\" from %2 has been acknowledged by the server. "
140✔
1823
                         "Removing cycle detection tracker.",
140✔
1824
                         pending_reset.type, pending_reset.time);
140✔
1825
        }
140✔
1826
        _impl::client_reset::remove_pending_client_resets(*wt);
140✔
1827
        wt->commit();
140✔
1828
    });
140✔
1829
}
288✔
1830

1831
void SessionWrapper::update_subscription_version_info()
1832
{
11,274✔
1833
    if (!m_flx_subscription_store)
11,274✔
1834
        return;
10,138✔
1835
    auto versions_info = m_flx_subscription_store->get_version_info();
1,136✔
1836
    m_flx_active_version = versions_info.active;
1,136✔
1837
    m_flx_pending_mark_version = versions_info.pending_mark;
1,136✔
1838
}
1,136✔
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,506✔
1886
    m_on_idle = m_client.create_trigger([this](Status status) {
2,510✔
1887
        if (status == ErrorCodes::OperationAborted)
2,510✔
1888
            return;
×
1889
        else if (!status.is_ok())
2,510✔
1890
            throw Exception(status);
×
1891

1,184✔
1892
        REALM_ASSERT(m_activated);
2,510✔
1893
        if (m_state == ConnectionState::disconnected && m_num_active_sessions == 0) {
2,510✔
1894
            on_idle(); // Throws
2,506✔
1895
            // Connection object may be destroyed now.
1,184✔
1896
        }
2,506✔
1897
    });
2,510✔
1898
}
2,506✔
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,506✔
1908
    return m_server_endpoint;
2,506✔
1909
}
2,506✔
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,884✔
1914
    m_http_request_path_prefix = http_request_path_prefix; // Throws (copy)
9,884✔
1915
    m_signed_access_token = signed_access_token;           // Throws (copy)
9,884✔
1916
}
9,884✔
1917

1918

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

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

1935

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

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

1,694✔
1947
    return path;
3,432✔
1948
}
3,432✔
1949

1950

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

1959

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

1973

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

1979

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

1985

1986
Client::~Client() noexcept {}
8,982✔
1987

1988

1989
void Client::shutdown() noexcept
1990
{
9,060✔
1991
    m_impl->shutdown();
9,060✔
1992
}
9,060✔
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,916✔
2001
    m_impl->cancel_reconnect_delay();
1,916✔
2002
}
1,916✔
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
{
370✔
2011
    return m_impl.get()->wait_for_session_terminations_or_client_stopped();
370✔
2012
}
370✔
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,398✔
2018
    return m_impl->decompose_server_url(url, protocol, address, port, path); // Throws
3,398✔
2019
}
3,398✔
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,006✔
2025
    util::bind_ptr<SessionWrapper> sess;
11,006✔
2026
    sess.reset(new SessionWrapper{*client.m_impl, std::move(db), std::move(flx_sub_store), std::move(migration_store),
11,006✔
2027
                                  std::move(config)}); // Throws
11,006✔
2028
    // The reference count passed back to the application is implicitly
5,330✔
2029
    // owned by a naked pointer. This is done to avoid exposing
5,330✔
2030
    // implementation details through the header file (that is, through the
5,330✔
2031
    // Session object).
5,330✔
2032
    m_impl = sess.release();
11,006✔
2033
}
11,006✔
2034

2035

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

2041

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

2047

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

2053

2054
void Session::nonsync_transact_notify(version_type new_version)
2055
{
15,198✔
2056
    m_impl->on_commit(new_version); // Throws
15,198✔
2057
}
15,198✔
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,054✔
2068
    m_impl->async_wait_for(upload_completion, download_completion, std::move(handler)); // Throws
4,054✔
2069
}
4,054✔
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,006✔
2092
    REALM_ASSERT(m_impl);
11,006✔
2093
    // Reabsorb the ownership assigned to the applications naked pointer by
5,330✔
2094
    // Session constructor
5,330✔
2095
    util::bind_ptr<SessionWrapper> wrapper{m_impl, util::bind_ptr_base::adopt_tag{}};
11,006✔
2096
    SessionWrapper::abandon(std::move(wrapper));
11,006✔
2097
}
11,006✔
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