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

realm / realm-core / 1652

08 Sep 2023 09:35AM UTC coverage: 91.205% (-0.04%) from 91.24%
1652

push

Evergreen

GitHub
Fix tools build (#6475)

95914 of 175834 branches covered (0.0%)

233332 of 255832 relevant lines covered (91.21%)

7627125.31 hits per line

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

85.32
/src/realm/sync/noinst/client_impl_base.cpp
1
#include <system_error>
2
#include <sstream>
3

4
#include <realm/util/assert.hpp>
5
#include <realm/util/safe_int_ops.hpp>
6
#include <realm/util/random.hpp>
7
#include <realm/util/memory_stream.hpp>
8
#include <realm/util/basic_system_errors.hpp>
9
#include <realm/util/scope_exit.hpp>
10
#include <realm/util/to_string.hpp>
11
#include <realm/util/uri.hpp>
12
#include <realm/util/platform_info.hpp>
13
#include <realm/sync/impl/clock.hpp>
14
#include <realm/impl/simulated_failure.hpp>
15
#include <realm/sync/network/http.hpp>
16
#include <realm/sync/network/websocket.hpp>
17
#include <realm/sync/noinst/client_history_impl.hpp>
18
#include <realm/sync/noinst/client_impl_base.hpp>
19
#include <realm/sync/noinst/compact_changesets.hpp>
20
#include <realm/sync/protocol.hpp>
21
#include <realm/version.hpp>
22
#include <realm/sync/changeset_parser.hpp>
23

24
#include <realm/sync/network/websocket.hpp> // Only for websocket::Error TODO remove
25

26
// NOTE: The protocol specification is in `/doc/protocol.md`
27

28
using namespace realm;
29
using namespace _impl;
30
using namespace realm::util;
31
using namespace realm::sync;
32
using namespace realm::sync::websocket;
33

34
// clang-format off
35
using Connection      = ClientImpl::Connection;
36
using Session         = ClientImpl::Session;
37
using UploadChangeset = ClientHistory::UploadChangeset;
38

39
// These are a work-around for a bug in MSVC. It cannot find in-class types
40
// mentioned in signature of out-of-line member function definitions.
41
using ConnectionTerminationReason = ClientImpl::ConnectionTerminationReason;
42
using OutputBuffer                = ClientImpl::OutputBuffer;
43
using ReceivedChangesets          = ClientProtocol::ReceivedChangesets;
44
// clang-format on
45

46
void ClientImpl::ReconnectInfo::reset() noexcept
47
{
1,780✔
48
    m_backoff_state.reset();
1,780✔
49
    scheduled_reset = false;
1,780✔
50
}
1,780✔
51

52

53
void ClientImpl::ReconnectInfo::update(ConnectionTerminationReason new_reason,
54
                                       std::optional<ResumptionDelayInfo> new_delay_info)
55
{
3,280✔
56
    m_backoff_state.update(new_reason, new_delay_info);
3,280✔
57
}
3,280✔
58

59

60
std::chrono::milliseconds ClientImpl::ReconnectInfo::delay_interval()
61
{
5,332✔
62
    if (scheduled_reset) {
5,332✔
63
        reset();
4✔
64
    }
4✔
65

2,798✔
66
    if (!m_backoff_state.triggering_error) {
5,332✔
67
        return std::chrono::milliseconds::zero();
4,110✔
68
    }
4,110✔
69

676✔
70
    switch (*m_backoff_state.triggering_error) {
1,222✔
71
        case ConnectionTerminationReason::closed_voluntarily:
66✔
72
            return std::chrono::milliseconds::zero();
66✔
73
        case ConnectionTerminationReason::server_said_do_not_reconnect:
18✔
74
            return std::chrono::milliseconds::max();
18✔
75
        default:
1,138✔
76
            if (m_reconnect_mode == ReconnectMode::testing) {
1,138✔
77
                return std::chrono::milliseconds::max();
920✔
78
            }
920✔
79

108✔
80
            REALM_ASSERT(m_reconnect_mode == ReconnectMode::normal);
218✔
81
            return m_backoff_state.delay_interval();
218✔
82
    }
1,222✔
83
}
1,222✔
84

85

86
bool ClientImpl::decompose_server_url(const std::string& url, ProtocolEnvelope& protocol, std::string& address,
87
                                      port_type& port, std::string& path) const
88
{
3,274✔
89
    util::Uri uri(url); // Throws
3,274✔
90
    uri.canonicalize(); // Throws
3,274✔
91
    std::string userinfo, address_2, port_2;
3,274✔
92
    bool realm_scheme = (uri.get_scheme() == "realm:" || uri.get_scheme() == "realms:");
3,274✔
93
    bool ws_scheme = (uri.get_scheme() == "ws:" || uri.get_scheme() == "wss:");
3,274!
94
    bool good = ((realm_scheme || ws_scheme) && uri.get_auth(userinfo, address_2, port_2) && userinfo.empty() &&
3,274✔
95
                 !address_2.empty() && uri.get_query().empty() && uri.get_frag().empty()); // Throws
3,274✔
96
    if (REALM_UNLIKELY(!good))
3,274✔
97
        return false;
1,462✔
98
    ProtocolEnvelope protocol_2;
3,274✔
99
    port_type port_3;
3,274✔
100
    if (realm_scheme) {
3,274✔
101
        if (uri.get_scheme() == "realm:") {
×
102
            protocol_2 = ProtocolEnvelope::realm;
×
103
            port_3 = (m_enable_default_port_hack ? 80 : 7800);
×
104
        }
×
105
        else {
×
106
            protocol_2 = ProtocolEnvelope::realms;
×
107
            port_3 = (m_enable_default_port_hack ? 443 : 7801);
×
108
        }
×
109
    }
×
110
    else {
3,274✔
111
        REALM_ASSERT(ws_scheme);
3,274✔
112
        if (uri.get_scheme() == "ws:") {
3,274✔
113
            protocol_2 = ProtocolEnvelope::ws;
3,274✔
114
            port_3 = 80;
3,274✔
115
        }
3,274✔
116
        else {
×
117
            protocol_2 = ProtocolEnvelope::wss;
×
118
            port_3 = 443;
×
119
        }
×
120
    }
3,274✔
121
    if (!port_2.empty()) {
3,274✔
122
        std::istringstream in(port_2);    // Throws
3,274✔
123
        in.imbue(std::locale::classic()); // Throws
3,274✔
124
        in >> port_3;
3,274✔
125
        if (REALM_UNLIKELY(!in || !in.eof() || port_3 < 1))
3,274✔
126
            return false;
1,462✔
127
    }
3,274✔
128
    std::string path_2 = uri.get_path(); // Throws (copy)
3,274✔
129

1,462✔
130
    protocol = protocol_2;
3,274✔
131
    address = std::move(address_2);
3,274✔
132
    port = port_3;
3,274✔
133
    path = std::move(path_2);
3,274✔
134
    return true;
3,274✔
135
}
3,274✔
136

137

138
ClientImpl::ClientImpl(ClientConfig config)
139
    : logger_ptr{config.logger ? std::move(config.logger) : std::make_shared<util::StderrLogger>()}
140
    , logger{*logger_ptr}
141
    , m_reconnect_mode{config.reconnect_mode}
142
    , m_connect_timeout{config.connect_timeout}
143
    , m_connection_linger_time{config.one_connection_per_session ? 0 : config.connection_linger_time}
144
    , m_ping_keepalive_period{config.ping_keepalive_period}
145
    , m_pong_keepalive_timeout{config.pong_keepalive_timeout}
146
    , m_fast_reconnect_limit{config.fast_reconnect_limit}
147
    , m_reconnect_backoff_info{config.reconnect_backoff_info}
148
    , m_disable_upload_activation_delay{config.disable_upload_activation_delay}
149
    , m_dry_run{config.dry_run}
150
    , m_enable_default_port_hack{config.enable_default_port_hack}
151
    , m_disable_upload_compaction{config.disable_upload_compaction}
152
    , m_fix_up_object_ids{config.fix_up_object_ids}
153
    , m_roundtrip_time_handler{std::move(config.roundtrip_time_handler)}
154
    , m_socket_provider{std::move(config.socket_provider)}
155
    , m_client_protocol{} // Throws
156
    , m_one_connection_per_session{config.one_connection_per_session}
157
    , m_random{}
158
{
8,906✔
159
    // FIXME: Would be better if seeding was up to the application.
4,384✔
160
    util::seed_prng_nondeterministically(m_random); // Throws
8,906✔
161

4,384✔
162
    logger.info("Realm sync client (%1)", REALM_VER_CHUNK); // Throws
8,906✔
163
    logger.debug("Supported protocol versions: %1-%2", get_oldest_supported_protocol_version(),
8,906✔
164
                 get_current_protocol_version()); // Throws
8,906✔
165
    logger.info("Platform: %1", util::get_platform_info());
8,906✔
166
    const char* build_mode;
8,906✔
167
#if REALM_DEBUG
8,906✔
168
    build_mode = "Debug";
8,906✔
169
#else
170
    build_mode = "Release";
171
#endif
172
    logger.debug("Build mode: %1", build_mode);
8,906✔
173
    logger.debug("Config param: one_connection_per_session = %1",
8,906✔
174
                 config.one_connection_per_session); // Throws
8,906✔
175
    logger.debug("Config param: connect_timeout = %1 ms",
8,906✔
176
                 config.connect_timeout); // Throws
8,906✔
177
    logger.debug("Config param: connection_linger_time = %1 ms",
8,906✔
178
                 config.connection_linger_time); // Throws
8,906✔
179
    logger.debug("Config param: ping_keepalive_period = %1 ms",
8,906✔
180
                 config.ping_keepalive_period); // Throws
8,906✔
181
    logger.debug("Config param: pong_keepalive_timeout = %1 ms",
8,906✔
182
                 config.pong_keepalive_timeout); // Throws
8,906✔
183
    logger.debug("Config param: fast_reconnect_limit = %1 ms",
8,906✔
184
                 config.fast_reconnect_limit); // Throws
8,906✔
185
    logger.debug("Config param: disable_upload_compaction = %1",
8,906✔
186
                 config.disable_upload_compaction); // Throws
8,906✔
187
    logger.debug("Config param: disable_sync_to_disk = %1",
8,906✔
188
                 config.disable_sync_to_disk); // Throws
8,906✔
189
    logger.debug("Config param: reconnect backoff info: max_delay: %1 ms, initial_delay: %2 ms, multiplier: %3",
8,906✔
190
                 m_reconnect_backoff_info.max_resumption_delay_interval.count(),
8,906✔
191
                 m_reconnect_backoff_info.resumption_delay_interval.count(),
8,906✔
192
                 m_reconnect_backoff_info.resumption_delay_backoff_multiplier);
8,906✔
193

4,384✔
194
    if (config.reconnect_mode != ReconnectMode::normal) {
8,906✔
195
        logger.warn("Testing/debugging feature 'nonnormal reconnect mode' enabled - "
768✔
196
                    "never do this in production!");
768✔
197
    }
768✔
198

4,384✔
199
    if (config.dry_run) {
8,906✔
200
        logger.warn("Testing/debugging feature 'dry run' enabled - "
×
201
                    "never do this in production!");
×
202
    }
×
203

4,384✔
204
    REALM_ASSERT_EX(m_socket_provider, "Must provide socket provider in sync Client config");
8,906✔
205

4,384✔
206
    if (m_one_connection_per_session) {
8,906✔
207
        // FIXME: Re-enable this warning when the load balancer is able to handle
2✔
208
        // multiplexing.
2✔
209
        //        logger.warn("Testing/debugging feature 'one connection per session' enabled - "
2✔
210
        //            "never do this in production");
2✔
211
    }
4✔
212

4,384✔
213
    if (config.disable_upload_activation_delay) {
8,906✔
214
        logger.warn("Testing/debugging feature 'disable_upload_activation_delay' enabled - "
×
215
                    "never do this in production");
×
216
    }
×
217

4,384✔
218
    if (config.disable_sync_to_disk) {
8,906✔
219
        logger.warn("Testing/debugging feature 'disable_sync_to_disk' enabled - "
×
220
                    "never do this in production");
×
221
    }
×
222

4,384✔
223
    m_actualize_and_finalize = create_trigger([this](Status status) {
14,718✔
224
        if (status == ErrorCodes::OperationAborted)
14,718✔
225
            return;
×
226
        else if (!status.is_ok())
14,718✔
227
            throw Exception(status);
×
228
        actualize_and_finalize_session_wrappers(); // Throws
14,718✔
229
    });
14,718✔
230
}
8,906✔
231

232

233
void ClientImpl::post(SyncSocketProvider::FunctionHandler&& handler)
234
{
98,552✔
235
    REALM_ASSERT(m_socket_provider);
98,552✔
236
    {
98,552✔
237
        std::lock_guard lock(m_drain_mutex);
98,552✔
238
        ++m_outstanding_posts;
98,552✔
239
        m_drained = false;
98,552✔
240
    }
98,552✔
241
    m_socket_provider->post([handler = std::move(handler), this](Status status) {
98,552✔
242
        auto decr_guard = util::make_scope_exit([&]() noexcept {
98,552✔
243
            std::lock_guard lock(m_drain_mutex);
98,552✔
244
            REALM_ASSERT(m_outstanding_posts);
98,552✔
245
            --m_outstanding_posts;
98,552✔
246
            m_drain_cv.notify_all();
98,552✔
247
        });
98,552✔
248
        handler(status);
98,548✔
249
    });
98,548✔
250
}
98,552✔
251

252

253
void ClientImpl::drain_connections()
254
{
8,906✔
255
    logger.debug("Draining connections during sync client shutdown");
8,906✔
256
    for (auto& server_slot_pair : m_server_slots) {
5,602✔
257
        auto& server_slot = server_slot_pair.second;
2,302✔
258

1,084✔
259
        if (server_slot.connection) {
2,302✔
260
            auto& conn = server_slot.connection;
2,210✔
261
            conn->force_close();
2,210✔
262
        }
2,210✔
263
        else {
92✔
264
            for (auto& conn_pair : server_slot.alt_connections) {
46✔
265
                conn_pair.second->force_close();
6✔
266
            }
6✔
267
        }
92✔
268
    }
2,302✔
269
}
8,906✔
270

271

272
SyncSocketProvider::SyncTimer ClientImpl::create_timer(std::chrono::milliseconds delay,
273
                                                       SyncSocketProvider::FunctionHandler&& handler)
274
{
15,966✔
275
    REALM_ASSERT(m_socket_provider);
15,966✔
276
    {
15,966✔
277
        std::lock_guard lock(m_drain_mutex);
15,966✔
278
        ++m_outstanding_posts;
15,966✔
279
        m_drained = false;
15,966✔
280
    }
15,966✔
281
    return m_socket_provider->create_timer(delay, [handler = std::move(handler), this](Status status) {
15,968✔
282
        handler(status);
15,968✔
283

7,850✔
284
        std::lock_guard lock(m_drain_mutex);
15,968✔
285
        REALM_ASSERT(m_outstanding_posts);
15,968✔
286
        --m_outstanding_posts;
15,968✔
287
        m_drain_cv.notify_all();
15,968✔
288
    });
15,968✔
289
}
15,966✔
290

291

292
ClientImpl::SyncTrigger ClientImpl::create_trigger(SyncSocketProvider::FunctionHandler&& handler)
293
{
11,324✔
294
    REALM_ASSERT(m_socket_provider);
11,324✔
295
    return std::make_unique<Trigger<ClientImpl>>(this, std::move(handler));
11,324✔
296
}
11,324✔
297

298
Connection::~Connection()
299
{
2,416✔
300
    if (m_websocket_sentinel) {
2,416✔
301
        m_websocket_sentinel->destroyed = true;
×
302
        m_websocket_sentinel.reset();
×
303
    }
×
304
}
2,416✔
305

306
void Connection::activate()
307
{
2,418✔
308
    REALM_ASSERT(m_on_idle);
2,418✔
309
    m_activated = true;
2,418✔
310
    if (m_num_active_sessions == 0)
2,418✔
311
        m_on_idle->trigger();
×
312
    // We cannot in general connect immediately, because a prior failure to
1,138✔
313
    // connect may require a delay before reconnecting (see `m_reconnect_info`).
1,138✔
314
    initiate_reconnect_wait(); // Throws
2,418✔
315
}
2,418✔
316

317

318
void Connection::activate_session(std::unique_ptr<Session> sess)
319
{
9,550✔
320
    REALM_ASSERT(sess);
9,550✔
321
    REALM_ASSERT(&sess->m_conn == this);
9,550✔
322
    REALM_ASSERT(!m_force_closed);
9,550✔
323
    Session& sess_2 = *sess;
9,550✔
324
    session_ident_type ident = sess->m_ident;
9,550✔
325
    auto p = m_sessions.emplace(ident, std::move(sess)); // Throws
9,550✔
326
    bool was_inserted = p.second;
9,550✔
327
    REALM_ASSERT(was_inserted);
9,550✔
328
    // Save the session ident to the historical list of session idents
4,600✔
329
    m_session_history.insert(ident);
9,550✔
330
    sess_2.activate(); // Throws
9,550✔
331
    if (m_state == ConnectionState::connected) {
9,550✔
332
        bool fast_reconnect = false;
6,708✔
333
        sess_2.connection_established(fast_reconnect); // Throws
6,708✔
334
    }
6,708✔
335
    ++m_num_active_sessions;
9,550✔
336
}
9,550✔
337

338

339
void Connection::initiate_session_deactivation(Session* sess)
340
{
9,550✔
341
    REALM_ASSERT(sess);
9,550✔
342
    REALM_ASSERT(&sess->m_conn == this);
9,550✔
343
    REALM_ASSERT(m_num_active_sessions);
9,550✔
344
    // Since the client may be waiting for m_num_active_sessions to reach 0
4,600✔
345
    // in stop_and_wait() (on a separate thread), deactivate Session before
4,600✔
346
    // decrementing the num active sessions value.
4,600✔
347
    sess->initiate_deactivation(); // Throws
9,550✔
348
    if (sess->m_state == Session::Deactivated) {
9,550✔
349
        finish_session_deactivation(sess);
944✔
350
    }
944✔
351
    if (REALM_UNLIKELY(--m_num_active_sessions == 0)) {
9,550✔
352
        if (m_activated && m_state == ConnectionState::disconnected)
3,900✔
353
            m_on_idle->trigger();
320✔
354
    }
3,900✔
355
}
9,550✔
356

357

358
void Connection::cancel_reconnect_delay()
359
{
1,984✔
360
    REALM_ASSERT(m_activated);
1,984✔
361

1,130✔
362
    if (m_reconnect_delay_in_progress) {
1,984✔
363
        if (m_nonzero_reconnect_delay)
1,772✔
364
            logger.detail("Canceling reconnect delay"); // Throws
890✔
365

1,024✔
366
        // Cancel the in-progress wait operation by destroying the timer
1,024✔
367
        // object. Destruction is needed in this case, because a new wait
1,024✔
368
        // operation might have to be initiated before the previous one
1,024✔
369
        // completes (its completion handler starts to execute), so the new wait
1,024✔
370
        // operation must be done on a new timer object.
1,024✔
371
        m_reconnect_disconnect_timer.reset();
1,772✔
372
        m_reconnect_delay_in_progress = false;
1,772✔
373
        m_reconnect_info.reset();
1,772✔
374
        initiate_reconnect_wait(); // Throws
1,772✔
375
        return;
1,772✔
376
    }
1,772✔
377

106✔
378
    // If we are not disconnected, then we need to make sure the next time we get disconnected
106✔
379
    // that we are allowed to re-connect as quickly as possible.
106✔
380
    //
106✔
381
    // Setting m_reconnect_info.scheduled_reset will cause initiate_reconnect_wait to reset the
106✔
382
    // backoff/delay state before calculating the next delay, unless a PONG message is received
106✔
383
    // for the urgent PING message we send below.
106✔
384
    //
106✔
385
    // If we get a PONG message for the urgent PING message sent below, then the connection is
106✔
386
    // healthy and we can calculate the next delay normally.
106✔
387
    if (m_state != ConnectionState::disconnected) {
212✔
388
        m_reconnect_info.scheduled_reset = true;
212✔
389
        m_ping_after_scheduled_reset_of_reconnect_info = false;
212✔
390

106✔
391
        schedule_urgent_ping(); // Throws
212✔
392
        return;
212✔
393
    }
212✔
394
    // Nothing to do in this case. The next reconnect attemp will be made as
106✔
395
    // soon as there are any sessions that are both active and unsuspended.
106✔
396
}
212✔
397

398
void ClientImpl::Connection::finish_session_deactivation(Session* sess)
399
{
7,702✔
400
    REALM_ASSERT(sess->m_state == Session::Deactivated);
7,702✔
401
    auto ident = sess->m_ident;
7,702✔
402
    m_sessions.erase(ident);
7,702✔
403
    m_session_history.erase(ident);
7,702✔
404
}
7,702✔
405

406
void Connection::force_close()
407
{
2,218✔
408
    if (m_force_closed) {
2,218✔
409
        return;
×
410
    }
×
411

1,044✔
412
    m_force_closed = true;
2,218✔
413

1,044✔
414
    if (m_state != ConnectionState::disconnected) {
2,218✔
415
        voluntary_disconnect();
2,138✔
416
    }
2,138✔
417

1,044✔
418
    REALM_ASSERT_EX(m_state == ConnectionState::disconnected, m_state);
2,218✔
419
    if (m_reconnect_delay_in_progress || m_disconnect_delay_in_progress) {
2,218✔
420
        m_reconnect_disconnect_timer.reset();
78✔
421
        m_reconnect_delay_in_progress = false;
78✔
422
        m_disconnect_delay_in_progress = false;
78✔
423
    }
78✔
424

1,044✔
425
    // We must copy any session pointers we want to close to a vector because force_closing
1,044✔
426
    // the session may remove it from m_sessions and invalidate the iterator uses to loop
1,044✔
427
    // through the map. By copying to a separate vector we ensure our iterators remain valid.
1,044✔
428
    std::vector<Session*> to_close;
2,218✔
429
    for (auto& session_pair : m_sessions) {
1,118✔
430
        if (session_pair.second->m_state == Session::State::Active) {
144✔
431
            to_close.push_back(session_pair.second.get());
144✔
432
        }
144✔
433
    }
144✔
434

1,044✔
435
    for (auto& sess : to_close) {
1,118✔
436
        sess->force_close();
144✔
437
    }
144✔
438

1,044✔
439
    logger.debug("Force closed idle connection");
2,218✔
440
}
2,218✔
441

442

443
void Connection::websocket_connected_handler(const std::string& protocol)
444
{
3,170✔
445
    if (!protocol.empty()) {
3,170✔
446
        std::string_view expected_prefix =
3,170✔
447
            is_flx_sync_connection() ? get_flx_websocket_protocol_prefix() : get_pbs_websocket_protocol_prefix();
2,916✔
448
        // FIXME: Use std::string_view::begins_with() in C++20.
1,584✔
449
        auto prefix_matches = [&](std::string_view other) {
3,170✔
450
            return protocol.size() >= other.size() && (protocol.substr(0, other.size()) == other);
3,170✔
451
        };
3,170✔
452
        if (prefix_matches(expected_prefix)) {
3,170✔
453
            util::MemoryInputStream in;
3,170✔
454
            in.set_buffer(protocol.data() + expected_prefix.size(), protocol.data() + protocol.size());
3,170✔
455
            in.imbue(std::locale::classic());
3,170✔
456
            in.unsetf(std::ios_base::skipws);
3,170✔
457
            int value_2 = 0;
3,170✔
458
            in >> value_2;
3,170✔
459
            if (in && in.eof() && value_2 >= 0) {
3,170✔
460
                bool good_version =
3,170✔
461
                    (value_2 >= get_oldest_supported_protocol_version() && value_2 <= get_current_protocol_version());
3,170✔
462
                if (good_version) {
3,170✔
463
                    logger.detail("Negotiated protocol version: %1", value_2);
3,170✔
464
                    // For now, grab the connection ID from the websocket if it supports it. In the future, the server
1,584✔
465
                    // will provide the appservices connection ID via a log message.
1,584✔
466
                    // TODO: Remove once the server starts sending the connection ID
1,584✔
467
                    receive_appservices_request_id(m_websocket->get_appservices_request_id());
3,170✔
468
                    m_negotiated_protocol_version = value_2;
3,170✔
469
                    handle_connection_established(); // Throws
3,170✔
470
                    return;
3,170✔
471
                }
3,170✔
472
            }
×
473
        }
3,170✔
474
        close_due_to_client_side_error({ErrorCodes::SyncProtocolNegotiationFailed,
×
475
                                        util::format("Bad protocol info from server: '%1'", protocol)},
×
476
                                       IsFatal{true}, ConnectionTerminationReason::bad_headers_in_http_response);
×
477
    }
×
478
    else {
×
479
        close_due_to_client_side_error(
×
480
            {ErrorCodes::SyncProtocolNegotiationFailed, "Missing protocol info from server"}, IsFatal{true},
×
481
            ConnectionTerminationReason::bad_headers_in_http_response);
×
482
    }
×
483
}
3,170✔
484

485

486
bool Connection::websocket_binary_message_received(util::Span<const char> data)
487
{
70,962✔
488
    if (m_force_closed) {
70,962✔
489
        logger.debug("Received binary message after connection was force closed");
×
490
        return false;
×
491
    }
×
492

36,556✔
493
    using sf = SimulatedFailure;
70,962✔
494
    if (sf::check_trigger(sf::sync_client__read_head)) {
70,962✔
495
        close_due_to_client_side_error(
364✔
496
            {ErrorCodes::RuntimeError, "Simulated failure during sync client websocket read"}, IsFatal{false},
364✔
497
            ConnectionTerminationReason::read_or_write_error);
364✔
498
        return bool(m_websocket);
364✔
499
    }
364✔
500

36,346✔
501
    handle_message_received(data);
70,598✔
502
    return bool(m_websocket);
70,598✔
503
}
70,598✔
504

505

506
void Connection::websocket_error_handler()
507
{
606✔
508
    m_websocket_error_received = true;
606✔
509
}
606✔
510

511
bool Connection::websocket_closed_handler(bool was_clean, WebSocketError error_code, std::string_view msg)
512
{
734✔
513
    if (m_force_closed) {
734✔
514
        logger.debug("Received websocket close message after connection was force closed");
×
515
        return false;
×
516
    }
×
517
    logger.info("Closing the websocket with error code=%1, message='%2', was_clean=%3", error_code, msg, was_clean);
734✔
518

408✔
519
    switch (error_code) {
734✔
520
        case WebSocketError::websocket_ok:
76✔
521
            break;
76✔
522
        case WebSocketError::websocket_resolve_failed:
✔
523
            [[fallthrough]];
×
524
        case WebSocketError::websocket_connection_failed: {
2✔
525
            SessionErrorInfo error_info(
2✔
526
                {ErrorCodes::SyncConnectFailed, util::format("Failed to connect to sync: %1", msg)}, IsFatal{false});
2✔
527
            involuntary_disconnect(std::move(error_info), ConnectionTerminationReason::connect_operation_failed);
2✔
528
            break;
2✔
529
        }
×
530
        case WebSocketError::websocket_read_error:
598✔
531
            [[fallthrough]];
598✔
532
        case WebSocketError::websocket_write_error: {
598✔
533
            close_due_to_transient_error({ErrorCodes::ConnectionClosed, msg},
598✔
534
                                         ConnectionTerminationReason::read_or_write_error);
598✔
535
            break;
598✔
536
        }
598✔
537
        case WebSocketError::websocket_going_away:
340✔
538
            [[fallthrough]];
×
539
        case WebSocketError::websocket_protocol_error:
✔
540
            [[fallthrough]];
×
541
        case WebSocketError::websocket_unsupported_data:
✔
542
            [[fallthrough]];
×
543
        case WebSocketError::websocket_invalid_payload_data:
✔
544
            [[fallthrough]];
×
545
        case WebSocketError::websocket_policy_violation:
✔
546
            [[fallthrough]];
×
547
        case WebSocketError::websocket_reserved:
✔
548
            [[fallthrough]];
×
549
        case WebSocketError::websocket_no_status_received:
✔
550
            [[fallthrough]];
×
551
        case WebSocketError::websocket_invalid_extension: {
✔
552
            close_due_to_client_side_error({ErrorCodes::SyncProtocolInvariantFailed, msg}, IsFatal{false},
×
553
                                           ConnectionTerminationReason::websocket_protocol_violation); // Throws
×
554
            break;
×
555
        }
×
556
        case WebSocketError::websocket_message_too_big: {
4✔
557
            auto message = util::format(
4✔
558
                "Sync websocket closed because the server received a message that was too large: %1", msg);
4✔
559
            SessionErrorInfo error_info(Status(ErrorCodes::LimitExceeded, std::move(message)), IsFatal{false});
4✔
560
            error_info.server_requests_action = ProtocolErrorInfo::Action::ClientReset;
4✔
561
            involuntary_disconnect(std::move(error_info),
4✔
562
                                   ConnectionTerminationReason::websocket_protocol_violation); // Throws
4✔
563
            break;
4✔
564
        }
×
565
        case WebSocketError::websocket_tls_handshake_failed: {
6✔
566
            close_due_to_client_side_error(Status(ErrorCodes::TlsHandshakeFailed, msg), IsFatal{false},
6✔
567
                                           ConnectionTerminationReason::ssl_certificate_rejected); // Throws
6✔
568
            break;
6✔
569
        }
×
570
        case WebSocketError::websocket_client_too_old:
✔
571
            [[fallthrough]];
×
572
        case WebSocketError::websocket_client_too_new:
✔
573
            [[fallthrough]];
×
574
        case WebSocketError::websocket_protocol_mismatch: {
✔
575
            close_due_to_client_side_error({ErrorCodes::SyncProtocolNegotiationFailed, msg}, IsFatal{true},
×
576
                                           ConnectionTerminationReason::http_response_says_fatal_error); // Throws
×
577
            break;
×
578
        }
×
579
        case WebSocketError::websocket_fatal_error: {
✔
580
            involuntary_disconnect(SessionErrorInfo({ErrorCodes::ConnectionClosed, msg}, IsFatal{true}),
×
581
                                   ConnectionTerminationReason::http_response_says_fatal_error);
×
582
            break;
×
583
        }
×
584
        case WebSocketError::websocket_forbidden: {
✔
585
            SessionErrorInfo error_info({ErrorCodes::AuthError, msg}, IsFatal{true});
×
586
            error_info.server_requests_action = ProtocolErrorInfo::Action::LogOutUser;
×
587
            involuntary_disconnect(std::move(error_info),
×
588
                                   ConnectionTerminationReason::http_response_says_fatal_error);
×
589
            break;
×
590
        }
×
591
        case WebSocketError::websocket_unauthorized: {
36✔
592
            SessionErrorInfo error_info(
36✔
593
                {ErrorCodes::AuthError,
36✔
594
                 util::format("Websocket was closed because of an authentication issue: %1", msg)},
36✔
595
                IsFatal{false});
36✔
596
            error_info.server_requests_action = ProtocolErrorInfo::Action::RefreshUser;
36✔
597
            involuntary_disconnect(std::move(error_info),
36✔
598
                                   ConnectionTerminationReason::http_response_says_nonfatal_error);
36✔
599
            break;
36✔
600
        }
×
601
        case WebSocketError::websocket_moved_permanently: {
12✔
602
            SessionErrorInfo error_info({ErrorCodes::ConnectionClosed, msg}, IsFatal{false});
12✔
603
            error_info.server_requests_action = ProtocolErrorInfo::Action::RefreshLocation;
12✔
604
            involuntary_disconnect(std::move(error_info),
12✔
605
                                   ConnectionTerminationReason::http_response_says_nonfatal_error);
12✔
606
            break;
12✔
607
        }
×
608
        case WebSocketError::websocket_abnormal_closure: {
✔
609
            SessionErrorInfo error_info({ErrorCodes::ConnectionClosed, msg}, IsFatal{false});
×
610
            error_info.server_requests_action = ProtocolErrorInfo::Action::RefreshUser;
×
611
            involuntary_disconnect(std::move(error_info),
×
612
                                   ConnectionTerminationReason::http_response_says_nonfatal_error);
×
613
            break;
×
614
        }
×
615
        case WebSocketError::websocket_internal_server_error:
✔
616
            [[fallthrough]];
×
617
        case WebSocketError::websocket_retry_error: {
✔
618
            involuntary_disconnect(SessionErrorInfo({ErrorCodes::ConnectionClosed, msg}, IsFatal{false}),
×
619
                                   ConnectionTerminationReason::http_response_says_nonfatal_error);
×
620
            break;
×
621
        }
734✔
622
    }
734✔
623

408✔
624
    return bool(m_websocket);
734✔
625
}
734✔
626

627
// Guarantees that handle_reconnect_wait() is never called from within the
628
// execution of initiate_reconnect_wait() (no callback reentrance).
629
void Connection::initiate_reconnect_wait()
630
{
7,472✔
631
    REALM_ASSERT(m_activated);
7,472✔
632
    REALM_ASSERT(!m_reconnect_delay_in_progress);
7,472✔
633
    REALM_ASSERT(!m_disconnect_delay_in_progress);
7,472✔
634

3,800✔
635
    // If we've been force closed then we don't need/want to reconnect. Just return early here.
3,800✔
636
    if (m_force_closed) {
7,472✔
637
        return;
2,140✔
638
    }
2,140✔
639

2,798✔
640
    m_reconnect_delay_in_progress = true;
5,332✔
641
    auto delay = m_reconnect_info.delay_interval();
5,332✔
642
    if (delay == std::chrono::milliseconds::max()) {
5,332✔
643
        logger.detail("Reconnection delayed indefinitely"); // Throws
938✔
644
        // Not actually starting a timer corresponds to an infinite wait
538✔
645
        m_nonzero_reconnect_delay = true;
938✔
646
        return;
938✔
647
    }
938✔
648

2,260✔
649
    if (delay == std::chrono::milliseconds::zero()) {
4,394✔
650
        m_nonzero_reconnect_delay = false;
4,176✔
651
    }
4,176✔
652
    else {
218✔
653
        logger.detail("Allowing reconnection in %1 milliseconds", delay.count()); // Throws
218✔
654
        m_nonzero_reconnect_delay = true;
218✔
655
    }
218✔
656

2,260✔
657
    // We create a timer for the reconnect_disconnect timer even if the delay is zero because
2,260✔
658
    // we need it to be cancelable in case the connection is terminated before the timer
2,260✔
659
    // callback is run.
2,260✔
660
    m_reconnect_disconnect_timer = m_client.create_timer(delay, [this](Status status) {
4,394✔
661
        // If the operation is aborted, the connection object may have been
2,260✔
662
        // destroyed.
2,260✔
663
        if (status != ErrorCodes::OperationAborted)
4,394✔
664
            handle_reconnect_wait(status); // Throws
3,276✔
665
    });                                    // Throws
4,394✔
666
}
4,394✔
667

668

669
void Connection::handle_reconnect_wait(Status status)
670
{
3,280✔
671
    if (!status.is_ok()) {
3,280✔
672
        REALM_ASSERT(status != ErrorCodes::OperationAborted);
×
673
        throw Exception(status);
×
674
    }
×
675

1,636✔
676
    REALM_ASSERT(m_reconnect_delay_in_progress);
3,280✔
677
    m_reconnect_delay_in_progress = false;
3,280✔
678

1,636✔
679
    if (m_num_active_unsuspended_sessions > 0)
3,280✔
680
        initiate_reconnect(); // Throws
3,282✔
681
}
3,280✔
682

683
struct Connection::WebSocketObserverShim : public sync::WebSocketObserver {
684
    explicit WebSocketObserverShim(Connection* conn)
685
        : conn(conn)
686
        , sentinel(conn->m_websocket_sentinel)
687
    {
3,280✔
688
    }
3,280✔
689

690
    Connection* conn;
691
    util::bind_ptr<LifecycleSentinel> sentinel;
692

693
    void websocket_connected_handler(const std::string& protocol) override
694
    {
3,170✔
695
        if (sentinel->destroyed) {
3,170✔
696
            return;
×
697
        }
×
698

1,584✔
699
        return conn->websocket_connected_handler(protocol);
3,170✔
700
    }
3,170✔
701

702
    void websocket_error_handler() override
703
    {
606✔
704
        if (sentinel->destroyed) {
606✔
705
            return;
×
706
        }
×
707

344✔
708
        conn->websocket_error_handler();
606✔
709
    }
606✔
710

711
    bool websocket_binary_message_received(util::Span<const char> data) override
712
    {
70,962✔
713
        if (sentinel->destroyed) {
70,962✔
714
            return false;
×
715
        }
×
716

36,556✔
717
        return conn->websocket_binary_message_received(data);
70,962✔
718
    }
70,962✔
719

720
    bool websocket_closed_handler(bool was_clean, WebSocketError error_code, std::string_view msg) override
721
    {
734✔
722
        if (sentinel->destroyed) {
734✔
723
            return true;
×
724
        }
×
725

408✔
726
        return conn->websocket_closed_handler(was_clean, error_code, msg);
734✔
727
    }
734✔
728
};
729

730
void Connection::initiate_reconnect()
731
{
3,282✔
732
    REALM_ASSERT(m_activated);
3,282✔
733

1,638✔
734
    m_state = ConnectionState::connecting;
3,282✔
735
    report_connection_state_change(ConnectionState::connecting); // Throws
3,282✔
736
    if (m_websocket_sentinel) {
3,282✔
737
        m_websocket_sentinel->destroyed = true;
×
738
    }
×
739
    m_websocket_sentinel = util::make_bind<LifecycleSentinel>();
3,282✔
740
    m_websocket.reset();
3,282✔
741

1,638✔
742
    // Watchdog
1,638✔
743
    initiate_connect_wait(); // Throws
3,282✔
744

1,638✔
745
    std::vector<std::string> sec_websocket_protocol;
3,282✔
746
    {
3,282✔
747
        auto protocol_prefix =
3,282✔
748
            is_flx_sync_connection() ? get_flx_websocket_protocol_prefix() : get_pbs_websocket_protocol_prefix();
3,024✔
749
        int min = get_oldest_supported_protocol_version();
3,282✔
750
        int max = get_current_protocol_version();
3,282✔
751
        REALM_ASSERT_3(min, <=, max);
3,282✔
752
        // List protocol version in descending order to ensure that the server
1,638✔
753
        // selects the highest possible version.
1,638✔
754
        for (int version = max; version >= min; --version) {
32,806✔
755
            sec_websocket_protocol.push_back(util::format("%1%2", protocol_prefix, version)); // Throws
29,524✔
756
        }
29,524✔
757
    }
3,282✔
758

1,638✔
759
    logger.info("Connecting to '%1%2:%3%4'", to_string(m_server_endpoint.envelope), m_server_endpoint.address,
3,282✔
760
                m_server_endpoint.port, m_http_request_path_prefix);
3,282✔
761

1,638✔
762
    m_websocket_error_received = false;
3,282✔
763
    m_websocket =
3,282✔
764
        m_client.m_socket_provider->connect(std::make_unique<WebSocketObserverShim>(this),
3,282✔
765
                                            WebSocketEndpoint{
3,282✔
766
                                                m_server_endpoint.address,
3,282✔
767
                                                m_server_endpoint.port,
3,282✔
768
                                                get_http_request_path(),
3,282✔
769
                                                std::move(sec_websocket_protocol),
3,282✔
770
                                                is_ssl(m_server_endpoint.envelope),
3,282✔
771
                                                /// DEPRECATED - The following will be removed in a future release
1,638✔
772
                                                {m_custom_http_headers.begin(), m_custom_http_headers.end()},
3,282✔
773
                                                m_verify_servers_ssl_certificate,
3,282✔
774
                                                m_ssl_trust_certificate_path,
3,282✔
775
                                                m_ssl_verify_callback,
3,282✔
776
                                                m_proxy_config,
3,282✔
777
                                            });
3,282✔
778
}
3,282✔
779

780

781
void Connection::initiate_connect_wait()
782
{
3,278✔
783
    // Deploy a watchdog to enforce an upper bound on the time it can take to
1,636✔
784
    // fully establish the connection (including SSL and WebSocket
1,636✔
785
    // handshakes). Without such a watchdog, connect operations could take very
1,636✔
786
    // long, or even indefinite time.
1,636✔
787
    milliseconds_type time = m_client.m_connect_timeout;
3,278✔
788

1,636✔
789
    m_connect_timer = m_client.create_timer(std::chrono::milliseconds(time), [this](Status status) {
3,282✔
790
        // If the operation is aborted, the connection object may have been
1,638✔
791
        // destroyed.
1,638✔
792
        if (status != ErrorCodes::OperationAborted)
3,282✔
793
            handle_connect_wait(status); // Throws
×
794
    });                                  // Throws
3,282✔
795
}
3,278✔
796

797

798
void Connection::handle_connect_wait(Status status)
799
{
×
800
    if (!status.is_ok()) {
×
801
        REALM_ASSERT(status != ErrorCodes::OperationAborted);
×
802
        throw Exception(status);
×
803
    }
×
804

805
    REALM_ASSERT_EX(m_state == ConnectionState::connecting, m_state);
×
806
    logger.info("Connect timeout"); // Throws
×
807
    involuntary_disconnect(
×
808
        SessionErrorInfo{Status{ErrorCodes::SyncConnectTimeout, "Sync connection was not fully established in time"},
×
809
                         IsFatal{false}},
×
810
        ConnectionTerminationReason::sync_connect_timeout); // Throws
×
811
}
×
812

813

814
void Connection::handle_connection_established()
815
{
3,170✔
816
    // Cancel connect timeout watchdog
1,584✔
817
    m_connect_timer.reset();
3,170✔
818

1,584✔
819
    m_state = ConnectionState::connected;
3,170✔
820

1,584✔
821
    milliseconds_type now = monotonic_clock_now();
3,170✔
822
    m_pong_wait_started_at = now; // Initially, no time was spent waiting for a PONG message
3,170✔
823
    initiate_ping_delay(now);     // Throws
3,170✔
824

1,584✔
825
    bool fast_reconnect = false;
3,170✔
826
    if (m_disconnect_has_occurred) {
3,170✔
827
        milliseconds_type time = now - m_disconnect_time;
946✔
828
        if (time <= m_client.m_fast_reconnect_limit)
946✔
829
            fast_reconnect = true;
946✔
830
    }
946✔
831

1,584✔
832
    for (auto& p : m_sessions) {
4,218✔
833
        Session& sess = *p.second;
4,218✔
834
        sess.connection_established(fast_reconnect); // Throws
4,218✔
835
    }
4,218✔
836

1,584✔
837
    report_connection_state_change(ConnectionState::connected); // Throws
3,170✔
838
}
3,170✔
839

840

841
void Connection::schedule_urgent_ping()
842
{
212✔
843
    REALM_ASSERT_EX(m_state != ConnectionState::disconnected, m_state);
212✔
844
    if (m_ping_delay_in_progress) {
212✔
845
        m_heartbeat_timer.reset();
156✔
846
        m_ping_delay_in_progress = false;
156✔
847
        m_minimize_next_ping_delay = true;
156✔
848
        milliseconds_type now = monotonic_clock_now();
156✔
849
        initiate_ping_delay(now); // Throws
156✔
850
        return;
156✔
851
    }
156✔
852
    REALM_ASSERT_EX(m_state == ConnectionState::connecting || m_waiting_for_pong, m_state);
56✔
853
    if (!m_send_ping)
56✔
854
        m_minimize_next_ping_delay = true;
56✔
855
}
56✔
856

857

858
void Connection::initiate_ping_delay(milliseconds_type now)
859
{
3,506✔
860
    REALM_ASSERT(!m_ping_delay_in_progress);
3,506✔
861
    REALM_ASSERT(!m_waiting_for_pong);
3,506✔
862
    REALM_ASSERT(!m_send_ping);
3,506✔
863

1,696✔
864
    milliseconds_type delay = 0;
3,506✔
865
    if (!m_minimize_next_ping_delay) {
3,506✔
866
        delay = m_client.m_ping_keepalive_period;
3,342✔
867
        // Make a randomized deduction of up to 10%, or up to 100% if this is
1,638✔
868
        // the first PING message to be sent since the connection was
1,638✔
869
        // established. The purpose of this randomized deduction is to reduce
1,638✔
870
        // the risk of many connections sending PING messages simultaneously to
1,638✔
871
        // the server.
1,638✔
872
        milliseconds_type max_deduction = (m_ping_sent ? delay / 10 : delay);
3,224✔
873
        auto distr = std::uniform_int_distribution<milliseconds_type>(0, max_deduction);
3,342✔
874
        milliseconds_type randomized_deduction = distr(m_client.get_random());
3,342✔
875
        delay -= randomized_deduction;
3,342✔
876
        // Deduct the time spent waiting for PONG
1,638✔
877
        REALM_ASSERT_3(now, >=, m_pong_wait_started_at);
3,342✔
878
        milliseconds_type spent_time = now - m_pong_wait_started_at;
3,342✔
879
        if (spent_time < delay) {
3,342✔
880
            delay -= spent_time;
3,332✔
881
        }
3,332✔
882
        else {
10✔
883
            delay = 0;
10✔
884
        }
10✔
885
    }
3,342✔
886
    else {
164✔
887
        m_minimize_next_ping_delay = false;
164✔
888
    }
164✔
889

1,696✔
890

1,696✔
891
    m_ping_delay_in_progress = true;
3,506✔
892

1,696✔
893
    m_heartbeat_timer = m_client.create_timer(std::chrono::milliseconds(delay), [this](Status status) {
3,504✔
894
        if (status == ErrorCodes::OperationAborted)
3,504✔
895
            return;
3,304✔
896
        else if (!status.is_ok())
200✔
897
            throw Exception(status);
×
898

68✔
899
        handle_ping_delay();                                    // Throws
200✔
900
    });                                                         // Throws
200✔
901
    logger.debug("Will emit a ping in %1 milliseconds", delay); // Throws
3,506✔
902
}
3,506✔
903

904

905
void Connection::handle_ping_delay()
906
{
200✔
907
    REALM_ASSERT(m_ping_delay_in_progress);
200✔
908
    m_ping_delay_in_progress = false;
200✔
909
    m_send_ping = true;
200✔
910

68✔
911
    initiate_pong_timeout(); // Throws
200✔
912

68✔
913
    if (m_state == ConnectionState::connected && !m_sending)
200✔
914
        send_next_message(); // Throws
168✔
915
}
200✔
916

917

918
void Connection::initiate_pong_timeout()
919
{
200✔
920
    REALM_ASSERT(!m_ping_delay_in_progress);
200✔
921
    REALM_ASSERT(!m_waiting_for_pong);
200✔
922
    REALM_ASSERT(m_send_ping);
200✔
923

68✔
924
    m_waiting_for_pong = true;
200✔
925
    m_pong_wait_started_at = monotonic_clock_now();
200✔
926

68✔
927
    milliseconds_type time = m_client.m_pong_keepalive_timeout;
200✔
928
    m_heartbeat_timer = m_client.create_timer(std::chrono::milliseconds(time), [this](Status status) {
200✔
929
        if (status == ErrorCodes::OperationAborted)
200✔
930
            return;
188✔
931
        else if (!status.is_ok())
12✔
932
            throw Exception(status);
×
933

6✔
934
        handle_pong_timeout(); // Throws
12✔
935
    });                        // Throws
12✔
936
}
200✔
937

938

939
void Connection::handle_pong_timeout()
940
{
12✔
941
    REALM_ASSERT(m_waiting_for_pong);
12✔
942
    logger.debug("Timeout on reception of PONG message"); // Throws
12✔
943
    close_due_to_transient_error({ErrorCodes::ConnectionClosed, "Timed out waiting for PONG response from server"},
12✔
944
                                 ConnectionTerminationReason::pong_timeout);
12✔
945
}
12✔
946

947

948
void Connection::initiate_write_message(const OutputBuffer& out, Session* sess)
949
{
92,624✔
950
    // Stop sending messages if an websocket error was received.
46,968✔
951
    if (m_websocket_error_received)
92,624✔
952
        return;
×
953

46,968✔
954
    m_websocket->async_write_binary(out.as_span(), [this, sentinel = m_websocket_sentinel](Status status) {
92,624✔
955
        if (sentinel->destroyed) {
91,152✔
956
            return;
×
957
        }
×
958
        if (status == ErrorCodes::OperationAborted)
91,152✔
959
            return;
×
960
        else if (!status.is_ok())
91,152✔
961
            throw Exception(status);
×
962

46,070✔
963
        handle_write_message(); // Throws
91,152✔
964
    });                         // Throws
91,152✔
965
    m_sending_session = sess;
92,624✔
966
    m_sending = true;
92,624✔
967
}
92,624✔
968

969

970
void Connection::handle_write_message()
971
{
91,150✔
972
    m_sending_session->message_sent(); // Throws
91,150✔
973
    if (m_sending_session->m_state == Session::Deactivated) {
91,150✔
974
        finish_session_deactivation(m_sending_session);
100✔
975
    }
100✔
976
    m_sending_session = nullptr;
91,150✔
977
    m_sending = false;
91,150✔
978
    send_next_message(); // Throws
91,150✔
979
}
91,150✔
980

981

982
void Connection::send_next_message()
983
{
152,188✔
984
    REALM_ASSERT_EX(m_state == ConnectionState::connected, m_state);
152,188✔
985
    REALM_ASSERT(!m_sending_session);
152,188✔
986
    REALM_ASSERT(!m_sending);
152,188✔
987
    if (m_send_ping) {
152,188✔
988
        send_ping(); // Throws
188✔
989
        return;
188✔
990
    }
188✔
991
    while (!m_sessions_enlisted_to_send.empty()) {
217,326✔
992
        // The state of being connected is not supposed to be able to change
80,732✔
993
        // across this loop thanks to the "no callback reentrance" guarantee
80,732✔
994
        // provided by Websocket::async_write_text(), and friends.
80,732✔
995
        REALM_ASSERT_EX(m_state == ConnectionState::connected, m_state);
157,954✔
996

80,732✔
997
        Session& sess = *m_sessions_enlisted_to_send.front();
157,954✔
998
        m_sessions_enlisted_to_send.pop_front();
157,954✔
999
        sess.send_message(); // Throws
157,954✔
1000

80,732✔
1001
        if (sess.m_state == Session::Deactivated) {
157,954✔
1002
            finish_session_deactivation(&sess);
3,518✔
1003
        }
3,518✔
1004

80,732✔
1005
        // An enlisted session may choose to not send a message. In that case,
80,732✔
1006
        // we should pass the opportunity to the next enlisted session.
80,732✔
1007
        if (m_sending)
157,954✔
1008
            break;
92,628✔
1009
    }
157,954✔
1010
}
152,000✔
1011

1012

1013
void Connection::send_ping()
1014
{
188✔
1015
    REALM_ASSERT(!m_ping_delay_in_progress);
188✔
1016
    REALM_ASSERT(m_waiting_for_pong);
188✔
1017
    REALM_ASSERT(m_send_ping);
188✔
1018

62✔
1019
    m_send_ping = false;
188✔
1020
    if (m_reconnect_info.scheduled_reset)
188✔
1021
        m_ping_after_scheduled_reset_of_reconnect_info = true;
140✔
1022

62✔
1023
    m_last_ping_sent_at = monotonic_clock_now();
188✔
1024
    logger.debug("Sending: PING(timestamp=%1, rtt=%2)", m_last_ping_sent_at,
188✔
1025
                 m_previous_ping_rtt); // Throws
188✔
1026

62✔
1027
    ClientProtocol& protocol = get_client_protocol();
188✔
1028
    OutputBuffer& out = get_output_buffer();
188✔
1029
    protocol.make_ping(out, m_last_ping_sent_at, m_previous_ping_rtt); // Throws
188✔
1030
    initiate_write_ping(out);                                          // Throws
188✔
1031
    m_ping_sent = true;
188✔
1032
}
188✔
1033

1034

1035
void Connection::initiate_write_ping(const OutputBuffer& out)
1036
{
188✔
1037
    m_websocket->async_write_binary(out.as_span(), [this, sentinel = m_websocket_sentinel](Status status) {
188✔
1038
        if (sentinel->destroyed) {
188✔
1039
            return;
×
1040
        }
×
1041
        if (status == ErrorCodes::OperationAborted)
188✔
1042
            return;
×
1043
        else if (!status.is_ok())
188✔
1044
            throw Exception(status);
×
1045

62✔
1046
        handle_write_ping(); // Throws
188✔
1047
    });                      // Throws
188✔
1048
    m_sending = true;
188✔
1049
}
188✔
1050

1051

1052
void Connection::handle_write_ping()
1053
{
188✔
1054
    REALM_ASSERT(m_sending);
188✔
1055
    REALM_ASSERT(!m_sending_session);
188✔
1056
    m_sending = false;
188✔
1057
    send_next_message(); // Throws
188✔
1058
}
188✔
1059

1060

1061
void Connection::handle_message_received(util::Span<const char> data)
1062
{
70,600✔
1063
    // parse_message_received() parses the message and calls the proper handler
36,350✔
1064
    // on the Connection object (this).
36,350✔
1065
    get_client_protocol().parse_message_received<Connection>(*this, std::string_view(data.data(), data.size()));
70,600✔
1066
}
70,600✔
1067

1068

1069
void Connection::initiate_disconnect_wait()
1070
{
4,220✔
1071
    REALM_ASSERT(!m_reconnect_delay_in_progress);
4,220✔
1072

1,992✔
1073
    if (m_disconnect_delay_in_progress) {
4,220✔
1074
        m_reconnect_disconnect_timer.reset();
2,026✔
1075
        m_disconnect_delay_in_progress = false;
2,026✔
1076
    }
2,026✔
1077

1,992✔
1078
    milliseconds_type time = m_client.m_connection_linger_time;
4,220✔
1079

1,992✔
1080
    m_reconnect_disconnect_timer = m_client.create_timer(std::chrono::milliseconds(time), [this](Status status) {
4,220✔
1081
        // If the operation is aborted, the connection object may have been
1,992✔
1082
        // destroyed.
1,992✔
1083
        if (status != ErrorCodes::OperationAborted)
4,220✔
1084
            handle_disconnect_wait(status); // Throws
8✔
1085
    });                                     // Throws
4,220✔
1086
    m_disconnect_delay_in_progress = true;
4,220✔
1087
}
4,220✔
1088

1089

1090
void Connection::handle_disconnect_wait(Status status)
1091
{
8✔
1092
    if (!status.is_ok()) {
8✔
1093
        REALM_ASSERT(status != ErrorCodes::OperationAborted);
×
1094
        throw Exception(status);
×
1095
    }
×
1096

1097
    m_disconnect_delay_in_progress = false;
8✔
1098

1099
    REALM_ASSERT_EX(m_state != ConnectionState::disconnected, m_state);
8✔
1100
    if (m_num_active_unsuspended_sessions == 0) {
8✔
1101
        if (m_client.m_connection_linger_time > 0)
6✔
1102
            logger.detail("Linger time expired"); // Throws
×
1103
        voluntary_disconnect();                   // Throws
6✔
1104
        logger.info("Disconnected");              // Throws
6✔
1105
    }
6✔
1106
}
8✔
1107

1108

1109
void Connection::close_due_to_protocol_error(Status status)
1110
{
4✔
1111
    SessionErrorInfo error_info(std::move(status), IsFatal{true});
4✔
1112
    error_info.server_requests_action = ProtocolErrorInfo::Action::ProtocolViolation;
4✔
1113
    involuntary_disconnect(std::move(error_info),
4✔
1114
                           ConnectionTerminationReason::sync_protocol_violation); // Throws
4✔
1115
}
4✔
1116

1117

1118
void Connection::close_due_to_client_side_error(Status status, IsFatal is_fatal, ConnectionTerminationReason reason)
1119
{
370✔
1120
    logger.info("Connection closed due to error: %1", status); // Throws
370✔
1121

214✔
1122
    involuntary_disconnect(SessionErrorInfo{std::move(status), is_fatal}, reason); // Throw
370✔
1123
}
370✔
1124

1125

1126
void Connection::close_due_to_transient_error(Status status, ConnectionTerminationReason reason)
1127
{
610✔
1128
    logger.info("Connection closed due to transient error: %1", status); // Throws
610✔
1129
    SessionErrorInfo error_info{std::move(status), IsFatal{false}};
610✔
1130
    error_info.server_requests_action = ProtocolErrorInfo::Action::Transient;
610✔
1131

346✔
1132
    involuntary_disconnect(std::move(error_info), reason); // Throw
610✔
1133
}
610✔
1134

1135

1136
// Close connection due to error discovered on the server-side, and then
1137
// reported to the client by way of a connection-level ERROR message.
1138
void Connection::close_due_to_server_side_error(ProtocolError error_code, const ProtocolErrorInfo& info)
1139
{
66✔
1140
    logger.info("Connection closed due to error reported by server: %1 (%2)", info.message,
66✔
1141
                int(error_code)); // Throws
66✔
1142

32✔
1143
    const auto reason = info.is_fatal ? ConnectionTerminationReason::server_said_do_not_reconnect
42✔
1144
                                      : ConnectionTerminationReason::server_said_try_again_later;
56✔
1145
    involuntary_disconnect(SessionErrorInfo{info, protocol_error_to_status(error_code, info.message)},
66✔
1146
                           reason); // Throws
66✔
1147
}
66✔
1148

1149

1150
void Connection::disconnect(const SessionErrorInfo& info)
1151
{
3,282✔
1152
    // Cancel connect timeout watchdog
1,638✔
1153
    m_connect_timer.reset();
3,282✔
1154

1,638✔
1155
    if (m_state == ConnectionState::connected) {
3,282✔
1156
        m_disconnect_time = monotonic_clock_now();
3,168✔
1157
        m_disconnect_has_occurred = true;
3,168✔
1158

1,584✔
1159
        // Sessions that are in the Deactivating state at this time can be
1,584✔
1160
        // immediately discarded, in part because they are no longer enlisted to
1,584✔
1161
        // send. Such sessions will be taken to the Deactivated state by
1,584✔
1162
        // Session::connection_lost(), and then they will be removed from
1,584✔
1163
        // `m_sessions`.
1,584✔
1164
        auto i = m_sessions.begin(), end = m_sessions.end();
3,168✔
1165
        while (i != end) {
7,018✔
1166
            // Prevent invalidation of the main iterator when erasing elements
1,918✔
1167
            auto j = i++;
3,850✔
1168
            Session& sess = *j->second;
3,850✔
1169
            sess.connection_lost(); // Throws
3,850✔
1170
            if (sess.m_state == Session::Unactivated || sess.m_state == Session::Deactivated)
3,850✔
1171
                m_sessions.erase(j);
1,848✔
1172
        }
3,850✔
1173
    }
3,168✔
1174

1,638✔
1175
    change_state_to_disconnected();
3,282✔
1176

1,638✔
1177
    m_ping_delay_in_progress = false;
3,282✔
1178
    m_waiting_for_pong = false;
3,282✔
1179
    m_send_ping = false;
3,282✔
1180
    m_minimize_next_ping_delay = false;
3,282✔
1181
    m_ping_after_scheduled_reset_of_reconnect_info = false;
3,282✔
1182
    m_ping_sent = false;
3,282✔
1183
    m_heartbeat_timer.reset();
3,282✔
1184
    m_previous_ping_rtt = 0;
3,282✔
1185

1,638✔
1186
    m_websocket_sentinel->destroyed = true;
3,282✔
1187
    m_websocket_sentinel.reset();
3,282✔
1188
    m_websocket.reset();
3,282✔
1189
    m_input_body_buffer.reset();
3,282✔
1190
    m_sending_session = nullptr;
3,282✔
1191
    m_sessions_enlisted_to_send.clear();
3,282✔
1192
    m_sending = false;
3,282✔
1193

1,638✔
1194
    report_connection_state_change(ConnectionState::disconnected, info); // Throws
3,282✔
1195
    initiate_reconnect_wait();                                           // Throws
3,282✔
1196
}
3,282✔
1197

1198
bool Connection::is_flx_sync_connection() const noexcept
1199
{
99,550✔
1200
    return m_server_endpoint.server_mode != SyncServerMode::PBS;
99,550✔
1201
}
99,550✔
1202

1203
void Connection::receive_pong(milliseconds_type timestamp)
1204
{
180✔
1205
    logger.debug("Received: PONG(timestamp=%1)", timestamp);
180✔
1206

60✔
1207
    bool legal_at_this_time = (m_waiting_for_pong && !m_send_ping);
180✔
1208
    if (REALM_UNLIKELY(!legal_at_this_time)) {
180✔
1209
        close_due_to_protocol_error(
×
1210
            {ErrorCodes::SyncProtocolInvariantFailed, "Received PONG message when it was not valid"}); // Throws
×
1211
        return;
×
1212
    }
×
1213

60✔
1214
    if (REALM_UNLIKELY(timestamp != m_last_ping_sent_at)) {
180✔
1215
        close_due_to_protocol_error(
×
1216
            {ErrorCodes::SyncProtocolInvariantFailed,
×
1217
             util::format("Received PONG message with an invalid timestamp (expected %1, received %2)",
×
1218
                          m_last_ping_sent_at, timestamp)}); // Throws
×
1219
        return;
×
1220
    }
×
1221

60✔
1222
    milliseconds_type now = monotonic_clock_now();
180✔
1223
    milliseconds_type round_trip_time = now - timestamp;
180✔
1224
    logger.debug("Round trip time was %1 milliseconds", round_trip_time);
180✔
1225
    m_previous_ping_rtt = round_trip_time;
180✔
1226

60✔
1227
    // If this PONG message is a response to a PING mesage that was sent after
60✔
1228
    // the last invocation of cancel_reconnect_delay(), then the connection is
60✔
1229
    // still good, and we do not have to skip the next reconnect delay.
60✔
1230
    if (m_ping_after_scheduled_reset_of_reconnect_info) {
180✔
1231
        REALM_ASSERT(m_reconnect_info.scheduled_reset);
130✔
1232
        m_ping_after_scheduled_reset_of_reconnect_info = false;
130✔
1233
        m_reconnect_info.scheduled_reset = false;
130✔
1234
    }
130✔
1235

60✔
1236
    m_heartbeat_timer.reset();
180✔
1237
    m_waiting_for_pong = false;
180✔
1238

60✔
1239
    initiate_ping_delay(now); // Throws
180✔
1240

60✔
1241
    if (m_client.m_roundtrip_time_handler)
180✔
1242
        m_client.m_roundtrip_time_handler(m_previous_ping_rtt); // Throws
×
1243
}
180✔
1244

1245
Session* Connection::find_and_validate_session(session_ident_type session_ident, std::string_view message) noexcept
1246
{
64,582✔
1247
    if (session_ident == 0) {
64,582✔
1248
        return nullptr;
×
1249
    }
×
1250

33,324✔
1251
    auto* sess = get_session(session_ident);
64,582✔
1252
    if (REALM_LIKELY(sess)) {
64,586✔
1253
        return sess;
64,586✔
1254
    }
64,586✔
1255
    // Check the history to see if the message received was for a previous session
2,147,483,647✔
1256
    if (auto it = m_session_history.find(session_ident); it == m_session_history.end()) {
2,147,483,647!
1257
        logger.error("Bad session identifier in %1 message, session_ident = %2", message, session_ident);
×
1258
        close_due_to_protocol_error(
×
1259
            {ErrorCodes::SyncProtocolInvariantFailed,
×
1260
             util::format("Received message %1 for session iden %2 when that session never existed", message,
×
1261
                          session_ident)});
×
1262
    }
×
1263
    else {
2,147,483,647✔
1264
        logger.error("Received %1 message for closed session, session_ident = %2", message,
2,147,483,647✔
1265
                     session_ident); // Throws
2,147,483,647✔
1266
    }
2,147,483,647✔
1267
    return nullptr;
2,147,483,647✔
1268
}
2,147,483,647✔
1269

1270
void Connection::receive_error_message(const ProtocolErrorInfo& info, session_ident_type session_ident)
1271
{
942✔
1272
    Session* sess = nullptr;
942✔
1273
    if (session_ident != 0) {
942✔
1274
        sess = find_and_validate_session(session_ident, "ERROR");
872✔
1275
        if (REALM_UNLIKELY(!sess)) {
872✔
1276
            return;
×
1277
        }
×
1278
        if (auto status = sess->receive_error_message(info); !status.is_ok()) {
872✔
1279
            close_due_to_protocol_error(std::move(status)); // Throws
×
1280
            return;
×
1281
        }
×
1282

444✔
1283
        if (sess->m_state == Session::Deactivated) {
872✔
1284
            finish_session_deactivation(sess);
×
1285
        }
×
1286
        return;
872✔
1287
    }
872✔
1288

34✔
1289
    logger.info("Received: ERROR \"%1\" (error_code=%2, is_fatal=%3, session_ident=%4, error_action=%5)",
70✔
1290
                info.message, info.raw_error_code, info.is_fatal, session_ident,
70✔
1291
                info.server_requests_action); // Throws
70✔
1292

34✔
1293
    bool known_error_code = bool(get_protocol_error_message(info.raw_error_code));
70✔
1294
    if (REALM_LIKELY(known_error_code)) {
70✔
1295
        ProtocolError error_code = ProtocolError(info.raw_error_code);
66✔
1296
        if (REALM_LIKELY(!is_session_level_error(error_code))) {
66✔
1297
            close_due_to_server_side_error(error_code, info); // Throws
66✔
1298
            return;
66✔
1299
        }
66✔
1300
        close_due_to_protocol_error(
×
1301
            {ErrorCodes::SyncProtocolInvariantFailed,
×
1302
             util::format("Received ERROR message with a non-connection-level error code %1 without a session ident",
×
1303
                          info.raw_error_code)});
×
1304
    }
×
1305
    else {
4✔
1306
        close_due_to_protocol_error(
4✔
1307
            {ErrorCodes::SyncProtocolInvariantFailed,
4✔
1308
             util::format("Received ERROR message with unknown error code %1", info.raw_error_code)});
4✔
1309
    }
4✔
1310
}
70✔
1311

1312

1313
void Connection::receive_query_error_message(int raw_error_code, std::string_view message, int64_t query_version,
1314
                                             session_ident_type session_ident)
1315
{
16✔
1316
    if (session_ident == 0) {
16✔
1317
        return close_due_to_protocol_error(
×
1318
            {ErrorCodes::SyncProtocolInvariantFailed, "Received query error message for session ident 0"});
×
1319
    }
×
1320

8✔
1321
    if (!is_flx_sync_connection()) {
16✔
1322
        return close_due_to_protocol_error({ErrorCodes::SyncProtocolInvariantFailed,
×
1323
                                            "Received a FLX query error message on a non-FLX sync connection"});
×
1324
    }
×
1325

8✔
1326
    Session* sess = find_and_validate_session(session_ident, "QUERY_ERROR");
16✔
1327
    if (REALM_UNLIKELY(!sess)) {
16✔
1328
        return;
×
1329
    }
×
1330

8✔
1331
    if (auto status = sess->receive_query_error_message(raw_error_code, message, query_version); !status.is_ok()) {
16✔
1332
        close_due_to_protocol_error(std::move(status));
×
1333
    }
×
1334
}
16✔
1335

1336

1337
void Connection::receive_ident_message(session_ident_type session_ident, SaltedFileIdent client_file_ident)
1338
{
3,176✔
1339
    Session* sess = find_and_validate_session(session_ident, "IDENT");
3,176✔
1340
    if (REALM_UNLIKELY(!sess)) {
3,176✔
1341
        return;
×
1342
    }
×
1343

1,502✔
1344
    if (auto status = sess->receive_ident_message(client_file_ident); !status.is_ok())
3,176✔
1345
        close_due_to_protocol_error(std::move(status)); // Throws
×
1346
}
3,176✔
1347

1348
void Connection::receive_download_message(session_ident_type session_ident, const SyncProgress& progress,
1349
                                          std::uint_fast64_t downloadable_bytes, int64_t query_version,
1350
                                          DownloadBatchState batch_state,
1351
                                          const ReceivedChangesets& received_changesets)
1352
{
41,770✔
1353
    Session* sess = find_and_validate_session(session_ident, "DOWNLOAD");
41,770✔
1354
    if (REALM_UNLIKELY(!sess)) {
41,770✔
1355
        return;
×
1356
    }
×
1357

22,130✔
1358
    if (auto status = sess->receive_download_message(progress, downloadable_bytes, batch_state, query_version,
41,770✔
1359
                                                     received_changesets);
41,770✔
1360
        !status.is_ok()) {
41,770✔
1361
        close_due_to_protocol_error(std::move(status));
×
1362
    }
×
1363
}
41,770✔
1364

1365
void Connection::receive_mark_message(session_ident_type session_ident, request_ident_type request_ident)
1366
{
15,564✔
1367
    Session* sess = find_and_validate_session(session_ident, "MARK");
15,564✔
1368
    if (REALM_UNLIKELY(!sess)) {
15,564✔
1369
        return;
×
1370
    }
×
1371

7,698✔
1372
    if (auto status = sess->receive_mark_message(request_ident); !status.is_ok())
15,564✔
1373
        close_due_to_protocol_error(std::move(status)); // Throws
×
1374
}
15,564✔
1375

1376

1377
void Connection::receive_unbound_message(session_ident_type session_ident)
1378
{
3,140✔
1379
    Session* sess = find_and_validate_session(session_ident, "UNBOUND");
3,140✔
1380
    if (REALM_UNLIKELY(!sess)) {
3,140✔
1381
        return;
×
1382
    }
×
1383

1,520✔
1384
    if (auto status = sess->receive_unbound_message(); !status.is_ok()) {
3,140✔
1385
        close_due_to_protocol_error(std::move(status)); // Throws
×
1386
        return;
×
1387
    }
×
1388

1,520✔
1389
    if (sess->m_state == Session::Deactivated) {
3,140✔
1390
        finish_session_deactivation(sess);
3,140✔
1391
    }
3,140✔
1392
}
3,140✔
1393

1394

1395
void Connection::receive_test_command_response(session_ident_type session_ident, request_ident_type request_ident,
1396
                                               std::string_view body)
1397
{
44✔
1398
    Session* sess = find_and_validate_session(session_ident, "TEST_COMMAND");
44✔
1399
    if (REALM_UNLIKELY(!sess)) {
44✔
1400
        return;
×
1401
    }
×
1402

22✔
1403
    if (auto status = sess->receive_test_command_response(request_ident, body); !status.is_ok()) {
44✔
1404
        close_due_to_protocol_error(std::move(status));
×
1405
    }
×
1406
}
44✔
1407

1408

1409
void Connection::receive_server_log_message(session_ident_type session_ident, util::Logger::Level level,
1410
                                            std::string_view message)
1411
{
5,766✔
1412
    std::string prefix;
5,766✔
1413
    if (REALM_LIKELY(!m_appservices_coid.empty())) {
5,766✔
1414
        prefix = util::format("Server[%1]", m_appservices_coid);
5,766✔
1415
    }
5,766✔
1416
    else {
×
1417
        prefix = "Server";
×
1418
    }
×
1419

2,928✔
1420
    if (session_ident != 0) {
5,766✔
1421
        if (auto sess = get_session(session_ident)) {
3,866✔
1422
            sess->logger.log(level, "%1 log: %2", prefix, message);
3,866✔
1423
            return;
3,866✔
1424
        }
3,866✔
1425

1426
        logger.log(level, "%1 log for unknown session %2: %3", prefix, session_ident, message);
×
1427
        return;
×
1428
    }
×
1429

958✔
1430
    logger.log(level, "%1 log: %2", prefix, message);
1,900✔
1431
}
1,900✔
1432

1433

1434
void Connection::receive_appservices_request_id(std::string_view coid)
1435
{
5,070✔
1436
    // Only set once per connection
2,542✔
1437
    if (!coid.empty() && m_appservices_coid.empty()) {
5,070✔
1438
        m_appservices_coid = coid;
2,208✔
1439
        logger.info("Connected to app services with request id: \"%1\"", m_appservices_coid);
2,208✔
1440
    }
2,208✔
1441
}
5,070✔
1442

1443

1444
void Connection::handle_protocol_error(Status status)
1445
{
×
1446
    close_due_to_protocol_error(std::move(status));
×
1447
}
×
1448

1449

1450
// Sessions are guaranteed to be granted the opportunity to send a message in
1451
// the order that they enlist. Note that this is important to ensure
1452
// nonoverlapping communication with the server for consecutive sessions
1453
// associated with the same Realm file.
1454
//
1455
// CAUTION: The specified session may get destroyed before this function
1456
// returns, but only if its Session::send_message() puts it into the Deactivated
1457
// state.
1458
void Connection::enlist_to_send(Session* sess)
1459
{
159,740✔
1460
    REALM_ASSERT_EX(m_state == ConnectionState::connected, m_state);
159,740✔
1461
    m_sessions_enlisted_to_send.push_back(sess); // Throws
159,740✔
1462
    if (!m_sending)
159,740✔
1463
        send_next_message(); // Throws
60,680✔
1464
}
159,740✔
1465

1466

1467
std::string Connection::get_active_appservices_connection_id()
1468
{
72✔
1469
    return m_appservices_coid;
72✔
1470
}
72✔
1471

1472
void Session::cancel_resumption_delay()
1473
{
4,108✔
1474
    REALM_ASSERT_EX(m_state == Active, m_state);
4,108✔
1475

2,340✔
1476
    if (!m_suspended)
4,108✔
1477
        return;
3,736✔
1478

196✔
1479
    m_suspended = false;
372✔
1480

196✔
1481
    logger.debug("Resumed"); // Throws
372✔
1482

196✔
1483
    if (unbind_process_complete())
372✔
1484
        initiate_rebind(); // Throws
366✔
1485

196✔
1486
    m_conn.one_more_active_unsuspended_session(); // Throws
372✔
1487

196✔
1488
    on_resumed(); // Throws
372✔
1489
}
372✔
1490

1491

1492
void Session::gather_pending_compensating_writes(util::Span<Changeset> changesets,
1493
                                                 std::vector<ProtocolErrorInfo>* out)
1494
{
20,234✔
1495
    if (m_pending_compensating_write_errors.empty() || changesets.empty()) {
20,234✔
1496
        return;
20,194✔
1497
    }
20,194✔
1498

20✔
1499
#ifdef REALM_DEBUG
40✔
1500
    REALM_ASSERT_DEBUG(
40✔
1501
        std::is_sorted(m_pending_compensating_write_errors.begin(), m_pending_compensating_write_errors.end(),
40✔
1502
                       [](const ProtocolErrorInfo& lhs, const ProtocolErrorInfo& rhs) {
40✔
1503
                           return lhs.compensating_write_server_version < rhs.compensating_write_server_version;
40✔
1504
                       }));
40✔
1505
#endif
40✔
1506

20✔
1507
    while (!m_pending_compensating_write_errors.empty() &&
80✔
1508
           m_pending_compensating_write_errors.front().compensating_write_server_version <=
62✔
1509
               changesets.back().version) {
40✔
1510
        auto& cur_error = m_pending_compensating_write_errors.front();
40✔
1511
        REALM_ASSERT_3(cur_error.compensating_write_server_version, >=, changesets.front().version);
40✔
1512
        out->push_back(std::move(cur_error));
40✔
1513
        m_pending_compensating_write_errors.pop_front();
40✔
1514
    }
40✔
1515
}
40✔
1516

1517

1518
void Session::integrate_changesets(ClientReplication& repl, const SyncProgress& progress,
1519
                                   std::uint_fast64_t downloadable_bytes,
1520
                                   const ReceivedChangesets& received_changesets, VersionInfo& version_info,
1521
                                   DownloadBatchState download_batch_state)
1522
{
39,804✔
1523
    auto& history = repl.get_history();
39,804✔
1524
    if (received_changesets.empty()) {
39,804✔
1525
        if (download_batch_state == DownloadBatchState::MoreToCome) {
19,548✔
1526
            throw IntegrationException(ErrorCodes::SyncProtocolInvariantFailed,
×
1527
                                       "received empty download message that was not the last in batch",
×
1528
                                       ProtocolError::bad_progress);
×
1529
        }
×
1530
        history.set_sync_progress(progress, &downloadable_bytes, version_info); // Throws
19,548✔
1531
        return;
19,548✔
1532
    }
19,548✔
1533

10,810✔
1534
    std::vector<ProtocolErrorInfo> pending_compensating_write_errors;
20,256✔
1535
    auto transact = get_db()->start_read();
20,256✔
1536
    history.integrate_server_changesets(
20,256✔
1537
        progress, &downloadable_bytes, received_changesets, version_info, download_batch_state, logger, transact,
20,256✔
1538
        [&](const TransactionRef&, util::Span<Changeset> changesets) {
20,244✔
1539
            gather_pending_compensating_writes(changesets, &pending_compensating_write_errors);
20,234✔
1540
        },
20,234✔
1541
        get_transact_reporter()); // Throws
20,256✔
1542
    if (received_changesets.size() == 1) {
20,256✔
1543
        logger.debug("1 remote changeset integrated, producing client version %1",
14,502✔
1544
                     version_info.sync_version.version); // Throws
14,502✔
1545
    }
14,502✔
1546
    else {
5,754✔
1547
        logger.debug("%2 remote changesets integrated, producing client version %1",
5,754✔
1548
                     version_info.sync_version.version, received_changesets.size()); // Throws
5,754✔
1549
    }
5,754✔
1550

10,810✔
1551
    for (const auto& pending_error : pending_compensating_write_errors) {
10,830✔
1552
        logger.info("Reporting compensating write for client version %1 in server version %2: %3",
40✔
1553
                    pending_error.compensating_write_rejected_client_version,
40✔
1554
                    pending_error.compensating_write_server_version, pending_error.message);
40✔
1555
        try {
40✔
1556
            on_connection_state_changed(
40✔
1557
                m_conn.get_state(),
40✔
1558
                SessionErrorInfo{pending_error,
40✔
1559
                                 protocol_error_to_status(static_cast<ProtocolError>(pending_error.raw_error_code),
40✔
1560
                                                          pending_error.message)});
40✔
1561
        }
40✔
1562
        catch (...) {
20✔
1563
            logger.error("Exception thrown while reporting compensating write: %1", exception_to_status());
×
1564
        }
×
1565
    }
40✔
1566
}
20,256✔
1567

1568

1569
void Session::on_integration_failure(const IntegrationException& error)
1570
{
32✔
1571
    REALM_ASSERT_EX(m_state == Active, m_state);
32✔
1572
    REALM_ASSERT(!m_client_error && !m_error_to_send);
32✔
1573
    logger.error("Failed to integrate downloaded changesets: %1", error.to_status());
32✔
1574

16✔
1575
    m_client_error = util::make_optional<IntegrationException>(error);
32✔
1576
    m_error_to_send = true;
32✔
1577

16✔
1578
    // Surface the error to the user otherwise is lost.
16✔
1579
    on_connection_state_changed(m_conn.get_state(), SessionErrorInfo{error.to_status(), IsFatal{false}});
32✔
1580

16✔
1581
    // Since the deactivation process has not been initiated, the UNBIND
16✔
1582
    // message cannot have been sent unless an ERROR message was received.
16✔
1583
    REALM_ASSERT(m_suspended || m_error_message_received || !m_unbind_message_sent);
32✔
1584
    if (m_ident_message_sent && !m_error_message_received && !m_suspended) {
32✔
1585
        ensure_enlisted_to_send(); // Throws
32✔
1586
    }
32✔
1587
}
32✔
1588

1589
void Session::on_changesets_integrated(version_type client_version, const SyncProgress& progress)
1590
{
41,084✔
1591
    REALM_ASSERT_EX(m_state == Active, m_state);
41,084✔
1592
    REALM_ASSERT_3(progress.download.server_version, >=, m_download_progress.server_version);
41,084✔
1593
    m_download_progress = progress.download;
41,084✔
1594
    bool upload_progressed = (progress.upload.client_version > m_progress.upload.client_version);
41,084✔
1595
    m_progress = progress;
41,084✔
1596
    if (upload_progressed) {
41,084✔
1597
        if (progress.upload.client_version > m_last_version_selected_for_upload) {
31,262✔
1598
            if (progress.upload.client_version > m_upload_progress.client_version)
13,130✔
1599
                m_upload_progress = progress.upload;
926✔
1600
            m_last_version_selected_for_upload = progress.upload.client_version;
13,130✔
1601
        }
13,130✔
1602

16,384✔
1603
        check_for_upload_completion();
31,262✔
1604
    }
31,262✔
1605

21,764✔
1606
    do_recognize_sync_version(client_version); // Allows upload process to resume
41,084✔
1607
    check_for_download_completion();           // Throws
41,084✔
1608

21,764✔
1609
    // If the client migrated from PBS to FLX, create subscriptions when new tables are received from server.
21,764✔
1610
    if (auto migration_store = get_migration_store(); migration_store && m_is_flx_sync_session) {
41,084✔
1611
        auto& flx_subscription_store = *get_flx_subscription_store();
2,276✔
1612
        get_migration_store()->create_subscriptions(flx_subscription_store);
2,276✔
1613
    }
2,276✔
1614

21,764✔
1615
    // Since the deactivation process has not been initiated, the UNBIND
21,764✔
1616
    // message cannot have been sent unless an ERROR message was received.
21,764✔
1617
    REALM_ASSERT(m_suspended || m_error_message_received || !m_unbind_message_sent);
41,084✔
1618
    if (m_ident_message_sent && !m_error_message_received && !m_suspended) {
41,084✔
1619
        ensure_enlisted_to_send(); // Throws
41,082✔
1620
    }
41,082✔
1621
}
41,084✔
1622

1623

1624
Session::~Session()
1625
{
9,548✔
1626
    //    REALM_ASSERT_EX(m_state == Unactivated || m_state == Deactivated, m_state);
4,598✔
1627
}
9,548✔
1628

1629

1630
std::string Session::make_logger_prefix(session_ident_type ident)
1631
{
9,548✔
1632
    std::ostringstream out;
9,548✔
1633
    out.imbue(std::locale::classic());
9,548✔
1634
    out << "Session[" << ident << "]: "; // Throws
9,548✔
1635
    return out.str();                    // Throws
9,548✔
1636
}
9,548✔
1637

1638

1639
void Session::activate()
1640
{
9,548✔
1641
    REALM_ASSERT_EX(m_state == Unactivated, m_state);
9,548✔
1642

4,600✔
1643
    logger.debug("Activating"); // Throws
9,548✔
1644

4,600✔
1645
    bool has_pending_client_reset = false;
9,548✔
1646
    if (REALM_LIKELY(!get_client().is_dry_run())) {
9,550✔
1647
        // The reason we need a mutable reference from get_client_reset_config() is because we
4,598✔
1648
        // don't want the session to keep a strong reference to the client_reset_config->fresh_copy
4,598✔
1649
        // DB. If it did, then the fresh DB would stay alive for the duration of this sync session
4,598✔
1650
        // and we want to clean it up once the reset is finished. Additionally, the fresh copy will
4,598✔
1651
        // be set to a new copy on every reset so there is no reason to keep a reference to it.
4,598✔
1652
        // The modification to the client reset config happens via std::move(client_reset_config->fresh_copy).
4,598✔
1653
        // If the client reset config were a `const &` then this std::move would create another strong
4,598✔
1654
        // reference which we don't want to happen.
4,598✔
1655
        util::Optional<ClientReset>& client_reset_config = get_client_reset_config();
9,548✔
1656

4,598✔
1657
        bool file_exists = util::File::exists(get_realm_path());
9,548✔
1658

4,598✔
1659
        logger.info("client_reset_config = %1, Realm exists = %2, "
9,548✔
1660
                    "client reset = %3",
9,548✔
1661
                    client_reset_config ? "true" : "false", file_exists ? "true" : "false",
9,542✔
1662
                    (client_reset_config && file_exists) ? "true" : "false"); // Throws
9,548✔
1663
        if (client_reset_config && !m_client_reset_operation) {
9,548✔
1664
            m_client_reset_operation = std::make_unique<_impl::ClientResetOperation>(
336✔
1665
                logger, get_db(), std::move(client_reset_config->fresh_copy), client_reset_config->mode,
336✔
1666
                std::move(client_reset_config->notify_before_client_reset),
336✔
1667
                std::move(client_reset_config->notify_after_client_reset),
336✔
1668
                client_reset_config->recovery_is_allowed); // Throws
336✔
1669
        }
336✔
1670

4,598✔
1671
        if (!m_client_reset_operation) {
9,548✔
1672
            const ClientReplication& repl = access_realm(); // Throws
9,214✔
1673
            repl.get_history().get_status(m_last_version_available, m_client_file_ident, m_progress,
9,214✔
1674
                                          &has_pending_client_reset); // Throws
9,214✔
1675
        }
9,214✔
1676
    }
9,548✔
1677
    logger.debug("client_file_ident = %1, client_file_ident_salt = %2", m_client_file_ident.ident,
9,548✔
1678
                 m_client_file_ident.salt); // Throws
9,548✔
1679
    m_upload_target_version = m_last_version_available;
9,548✔
1680
    m_upload_progress = m_progress.upload;
9,548✔
1681
    m_last_version_selected_for_upload = m_upload_progress.client_version;
9,548✔
1682
    m_download_progress = m_progress.download;
9,548✔
1683
    REALM_ASSERT_3(m_last_version_available, >=, m_progress.upload.client_version);
9,548✔
1684

4,600✔
1685
    logger.debug("last_version_available  = %1", m_last_version_available);           // Throws
9,548✔
1686
    logger.debug("progress_server_version = %1", m_progress.download.server_version); // Throws
9,548✔
1687
    logger.debug("progress_client_version = %1",
9,548✔
1688
                 m_progress.download.last_integrated_client_version); // Throws
9,548✔
1689

4,600✔
1690
    reset_protocol_state();
9,548✔
1691
    m_state = Active;
9,548✔
1692

4,600✔
1693
    REALM_ASSERT(!m_suspended);
9,548✔
1694
    m_conn.one_more_active_unsuspended_session(); // Throws
9,548✔
1695

4,600✔
1696
    try {
9,548✔
1697
        process_pending_flx_bootstrap();
9,548✔
1698
    }
9,548✔
1699
    catch (const IntegrationException& error) {
4,602✔
1700
        logger.error("Error integrating bootstrap changesets: %1", error.what());
4✔
1701
        m_suspended = true;
4✔
1702
        m_conn.one_less_active_unsuspended_session(); // Throws
4✔
1703
        on_suspended(SessionErrorInfo{Status{error.code(), error.what()}, IsFatal{true}});
4✔
1704
    }
4✔
1705

4,600✔
1706
    if (has_pending_client_reset) {
9,550✔
1707
        handle_pending_client_reset_acknowledgement();
24✔
1708
    }
24✔
1709
}
9,548✔
1710

1711

1712
// The caller (Connection) must discard the session if the session has become
1713
// deactivated upon return.
1714
void Session::initiate_deactivation()
1715
{
9,550✔
1716
    REALM_ASSERT_EX(m_state == Active, m_state);
9,550✔
1717

4,600✔
1718
    logger.debug("Initiating deactivation"); // Throws
9,550✔
1719

4,600✔
1720
    m_state = Deactivating;
9,550✔
1721

4,600✔
1722
    if (!m_suspended)
9,550✔
1723
        m_conn.one_less_active_unsuspended_session(); // Throws
9,026✔
1724

4,600✔
1725
    if (m_enlisted_to_send) {
9,550✔
1726
        REALM_ASSERT(!unbind_process_complete());
5,828✔
1727
        return;
5,828✔
1728
    }
5,828✔
1729

1,818✔
1730
    // Deactivate immediately if the BIND message has not yet been sent and the
1,818✔
1731
    // session is not enlisted to send, or if the unbinding process has already
1,818✔
1732
    // completed.
1,818✔
1733
    if (!m_bind_message_sent || unbind_process_complete()) {
3,722✔
1734
        complete_deactivation(); // Throws
944✔
1735
        // Life cycle state is now Deactivated
438✔
1736
        return;
944✔
1737
    }
944✔
1738

1,380✔
1739
    // Ready to send the UNBIND message, if it has not already been sent
1,380✔
1740
    if (!m_unbind_message_sent) {
2,778✔
1741
        enlist_to_send(); // Throws
2,608✔
1742
        return;
2,608✔
1743
    }
2,608✔
1744
}
2,778✔
1745

1746

1747
void Session::complete_deactivation()
1748
{
9,550✔
1749
    REALM_ASSERT_EX(m_state == Deactivating, m_state);
9,550✔
1750
    m_state = Deactivated;
9,550✔
1751

4,600✔
1752
    logger.debug("Deactivation completed"); // Throws
9,550✔
1753
}
9,550✔
1754

1755

1756
// Called by the associated Connection object when this session is granted an
1757
// opportunity to send a message.
1758
//
1759
// The caller (Connection) must discard the session if the session has become
1760
// deactivated upon return.
1761
void Session::send_message()
1762
{
157,958✔
1763
    REALM_ASSERT_EX(m_state == Active || m_state == Deactivating, m_state);
157,958✔
1764
    REALM_ASSERT(m_enlisted_to_send);
157,958✔
1765
    m_enlisted_to_send = false;
157,958✔
1766
    if (m_state == Deactivating || m_error_message_received || m_suspended) {
157,958✔
1767
        // Deactivation has been initiated. If the UNBIND message has not been
4,274✔
1768
        // sent yet, there is no point in sending it. Instead, we can let the
4,274✔
1769
        // deactivation process complete.
4,274✔
1770
        if (!m_bind_message_sent) {
8,616✔
1771
            return complete_deactivation(); // Throws
3,518✔
1772
            // Life cycle state is now Deactivated
1,814✔
1773
        }
3,518✔
1774

2,460✔
1775
        // Session life cycle state is Deactivating or the unbinding process has
2,460✔
1776
        // been initiated by a session specific ERROR message
2,460✔
1777
        if (!m_unbind_message_sent)
5,098✔
1778
            send_unbind_message(); // Throws
5,098✔
1779
        return;
5,098✔
1780
    }
5,098✔
1781

76,456✔
1782
    // Session life cycle state is Active and the unbinding process has
76,456✔
1783
    // not been initiated
76,456✔
1784
    REALM_ASSERT(!m_unbind_message_sent);
149,342✔
1785

76,456✔
1786
    if (!m_bind_message_sent)
149,342✔
1787
        return send_bind_message(); // Throws
7,474✔
1788

72,644✔
1789
    if (!m_ident_message_sent) {
141,868✔
1790
        if (have_client_file_ident())
6,586✔
1791
            send_ident_message(); // Throws
6,586✔
1792
        return;
6,586✔
1793
    }
6,586✔
1794

69,310✔
1795
    const auto has_pending_test_command = std::any_of(m_pending_test_commands.begin(), m_pending_test_commands.end(),
135,282✔
1796
                                                      [](const PendingTestCommand& command) {
69,382✔
1797
                                                          return command.pending;
144✔
1798
                                                      });
144✔
1799
    if (has_pending_test_command) {
135,282✔
1800
        return send_test_command_message();
44✔
1801
    }
44✔
1802

69,288✔
1803
    if (m_error_to_send)
135,238✔
1804
        return send_json_error_message(); // Throws
30✔
1805

69,274✔
1806
    // Stop sending upload, mark and query messages when the client detects an error.
69,274✔
1807
    if (m_client_error) {
135,208✔
1808
        return;
16✔
1809
    }
16✔
1810

69,266✔
1811
    if (m_target_download_mark > m_last_download_mark_sent)
135,192✔
1812
        return send_mark_message(); // Throws
16,102✔
1813

61,290✔
1814
    auto is_upload_allowed = [&]() -> bool {
119,094✔
1815
        if (!m_is_flx_sync_session) {
119,094✔
1816
            return true;
110,190✔
1817
        }
110,190✔
1818

4,622✔
1819
        auto migration_store = get_migration_store();
8,904✔
1820
        if (!migration_store) {
8,904✔
1821
            return true;
×
1822
        }
×
1823

4,622✔
1824
        auto sentinel_query_version = migration_store->get_sentinel_subscription_set_version();
8,904✔
1825
        if (!sentinel_query_version) {
8,904✔
1826
            return true;
8,878✔
1827
        }
8,878✔
1828

12✔
1829
        // Do not allow upload if the last query sent is the sentinel one used by the migration store.
12✔
1830
        return m_last_sent_flx_query_version != *sentinel_query_version;
26✔
1831
    };
26✔
1832

61,290✔
1833
    if (!is_upload_allowed()) {
119,090✔
1834
        return;
12✔
1835
    }
12✔
1836

61,284✔
1837
    auto check_pending_flx_version = [&]() -> bool {
119,082✔
1838
        if (!m_is_flx_sync_session) {
119,082✔
1839
            return false;
110,186✔
1840
        }
110,186✔
1841

4,620✔
1842
        if (!m_allow_upload) {
8,896✔
1843
            return false;
1,514✔
1844
        }
1,514✔
1845

3,856✔
1846
        m_pending_flx_sub_set = get_flx_subscription_store()->get_next_pending_version(
7,382✔
1847
            m_last_sent_flx_query_version, m_upload_progress.client_version);
7,382✔
1848

3,856✔
1849
        if (!m_pending_flx_sub_set) {
7,382✔
1850
            return false;
5,934✔
1851
        }
5,934✔
1852

728✔
1853
        return m_upload_progress.client_version >= m_pending_flx_sub_set->snapshot_version;
1,448✔
1854
    };
1,448✔
1855

61,284✔
1856
    if (check_pending_flx_version()) {
119,078✔
1857
        return send_query_change_message(); // throws
756✔
1858
    }
756✔
1859

60,904✔
1860
    REALM_ASSERT_3(m_upload_progress.client_version, <=, m_upload_target_version);
118,322✔
1861
    REALM_ASSERT_3(m_upload_target_version, <=, m_last_version_available);
118,322✔
1862
    if (m_allow_upload && (m_upload_target_version > m_upload_progress.client_version)) {
118,322✔
1863
        return send_upload_message(); // Throws
56,540✔
1864
    }
56,540✔
1865
}
118,322✔
1866

1867

1868
void Session::send_bind_message()
1869
{
7,474✔
1870
    REALM_ASSERT_EX(m_state == Active, m_state);
7,474✔
1871

3,812✔
1872
    session_ident_type session_ident = m_ident;
7,474✔
1873
    bool need_client_file_ident = !have_client_file_ident();
7,474✔
1874
    const bool is_subserver = false;
7,474✔
1875

3,812✔
1876

3,812✔
1877
    ClientProtocol& protocol = m_conn.get_client_protocol();
7,474✔
1878
    int protocol_version = m_conn.get_negotiated_protocol_version();
7,474✔
1879
    OutputBuffer& out = m_conn.get_output_buffer();
7,474✔
1880
    // Discard the token since it's ignored by the server.
3,812✔
1881
    std::string empty_access_token;
7,474✔
1882
    if (m_is_flx_sync_session) {
7,474✔
1883
        nlohmann::json bind_json_data;
1,300✔
1884
        if (auto migrated_partition = get_migration_store()->get_migrated_partition()) {
1,300✔
1885
            bind_json_data["migratedPartition"] = *migrated_partition;
60✔
1886
        }
60✔
1887
        bind_json_data["sessionReason"] = static_cast<uint64_t>(get_session_reason());
1,300✔
1888
        if (logger.would_log(util::Logger::Level::debug)) {
1,300✔
1889
            std::string json_data_dump;
1,300✔
1890
            if (!bind_json_data.empty()) {
1,300✔
1891
                json_data_dump = bind_json_data.dump();
1,300✔
1892
            }
1,300✔
1893
            logger.debug(
1,300✔
1894
                "Sending: BIND(session_ident=%1, need_client_file_ident=%2, is_subserver=%3, json_data=\"%4\")",
1,300✔
1895
                session_ident, need_client_file_ident, is_subserver, json_data_dump);
1,300✔
1896
        }
1,300✔
1897
        protocol.make_flx_bind_message(protocol_version, out, session_ident, bind_json_data, empty_access_token,
1,300✔
1898
                                       need_client_file_ident, is_subserver); // Throws
1,300✔
1899
    }
1,300✔
1900
    else {
6,174✔
1901
        std::string server_path = get_virt_path();
6,174✔
1902
        logger.debug("Sending: BIND(session_ident=%1, need_client_file_ident=%2, is_subserver=%3, server_path=%4)",
6,174✔
1903
                     session_ident, need_client_file_ident, is_subserver, server_path);
6,174✔
1904
        protocol.make_pbs_bind_message(protocol_version, out, session_ident, server_path, empty_access_token,
6,174✔
1905
                                       need_client_file_ident, is_subserver); // Throws
6,174✔
1906
    }
6,174✔
1907
    m_conn.initiate_write_message(out, this); // Throws
7,474✔
1908

3,812✔
1909
    m_bind_message_sent = true;
7,474✔
1910

3,812✔
1911
    // Ready to send the IDENT message if the file identifier pair is already
3,812✔
1912
    // available.
3,812✔
1913
    if (!need_client_file_ident)
7,474✔
1914
        enlist_to_send(); // Throws
3,544✔
1915
}
7,474✔
1916

1917

1918
void Session::send_ident_message()
1919
{
6,586✔
1920
    REALM_ASSERT_EX(m_state == Active, m_state);
6,586✔
1921
    REALM_ASSERT(m_bind_message_sent);
6,586✔
1922
    REALM_ASSERT(!m_unbind_message_sent);
6,586✔
1923
    REALM_ASSERT(have_client_file_ident());
6,586✔
1924

3,334✔
1925

3,334✔
1926
    ClientProtocol& protocol = m_conn.get_client_protocol();
6,586✔
1927
    OutputBuffer& out = m_conn.get_output_buffer();
6,586✔
1928
    session_ident_type session_ident = m_ident;
6,586✔
1929

3,334✔
1930
    if (m_is_flx_sync_session) {
6,586✔
1931
        const auto active_query_set = get_flx_subscription_store()->get_active();
918✔
1932
        const auto active_query_body = active_query_set.to_ext_json();
918✔
1933
        logger.debug("Sending: IDENT(client_file_ident=%1, client_file_ident_salt=%2, "
918✔
1934
                     "scan_server_version=%3, scan_client_version=%4, latest_server_version=%5, "
918✔
1935
                     "latest_server_version_salt=%6, query_version=%7, query_size=%8, query=\"%9\")",
918✔
1936
                     m_client_file_ident.ident, m_client_file_ident.salt, m_progress.download.server_version,
918✔
1937
                     m_progress.download.last_integrated_client_version, m_progress.latest_server_version.version,
918✔
1938
                     m_progress.latest_server_version.salt, active_query_set.version(), active_query_body.size(),
918✔
1939
                     active_query_body); // Throws
918✔
1940
        protocol.make_flx_ident_message(out, session_ident, m_client_file_ident, m_progress,
918✔
1941
                                        active_query_set.version(), active_query_body); // Throws
918✔
1942
        m_last_sent_flx_query_version = active_query_set.version();
918✔
1943
    }
918✔
1944
    else {
5,668✔
1945
        logger.debug("Sending: IDENT(client_file_ident=%1, client_file_ident_salt=%2, "
5,668✔
1946
                     "scan_server_version=%3, scan_client_version=%4, latest_server_version=%5, "
5,668✔
1947
                     "latest_server_version_salt=%6)",
5,668✔
1948
                     m_client_file_ident.ident, m_client_file_ident.salt, m_progress.download.server_version,
5,668✔
1949
                     m_progress.download.last_integrated_client_version, m_progress.latest_server_version.version,
5,668✔
1950
                     m_progress.latest_server_version.salt);                                  // Throws
5,668✔
1951
        protocol.make_pbs_ident_message(out, session_ident, m_client_file_ident, m_progress); // Throws
5,668✔
1952
    }
5,668✔
1953
    m_conn.initiate_write_message(out, this); // Throws
6,586✔
1954

3,334✔
1955
    m_ident_message_sent = true;
6,586✔
1956

3,334✔
1957
    // Other messages may be waiting to be sent
3,334✔
1958
    enlist_to_send(); // Throws
6,586✔
1959
}
6,586✔
1960

1961
void Session::send_query_change_message()
1962
{
756✔
1963
    REALM_ASSERT_EX(m_state == Active, m_state);
756✔
1964
    REALM_ASSERT(m_ident_message_sent);
756✔
1965
    REALM_ASSERT(!m_unbind_message_sent);
756✔
1966
    REALM_ASSERT(m_pending_flx_sub_set);
756✔
1967
    REALM_ASSERT_3(m_pending_flx_sub_set->query_version, >, m_last_sent_flx_query_version);
756✔
1968

380✔
1969
    if (REALM_UNLIKELY(get_client().is_dry_run())) {
756✔
1970
        return;
×
1971
    }
×
1972

380✔
1973
    auto sub_store = get_flx_subscription_store();
756✔
1974
    auto latest_sub_set = sub_store->get_by_version(m_pending_flx_sub_set->query_version);
756✔
1975
    auto latest_queries = latest_sub_set.to_ext_json();
756✔
1976
    logger.debug("Sending: QUERY(query_version=%1, query_size=%2, query=\"%3\", snapshot_version=%4)",
756✔
1977
                 latest_sub_set.version(), latest_queries.size(), latest_queries, latest_sub_set.snapshot_version());
756✔
1978

380✔
1979
    OutputBuffer& out = m_conn.get_output_buffer();
756✔
1980
    session_ident_type session_ident = get_ident();
756✔
1981
    ClientProtocol& protocol = m_conn.get_client_protocol();
756✔
1982
    protocol.make_query_change_message(out, session_ident, latest_sub_set.version(), latest_queries);
756✔
1983
    m_conn.initiate_write_message(out, this);
756✔
1984

380✔
1985
    m_last_sent_flx_query_version = latest_sub_set.version();
756✔
1986

380✔
1987
    request_download_completion_notification();
756✔
1988
}
756✔
1989

1990
void Session::send_upload_message()
1991
{
56,538✔
1992
    REALM_ASSERT_EX(m_state == Active, m_state);
56,538✔
1993
    REALM_ASSERT(m_ident_message_sent);
56,538✔
1994
    REALM_ASSERT(!m_unbind_message_sent);
56,538✔
1995
    REALM_ASSERT_3(m_upload_target_version, >, m_upload_progress.client_version);
56,538✔
1996

28,974✔
1997
    if (REALM_UNLIKELY(get_client().is_dry_run()))
56,538✔
1998
        return;
28,974✔
1999

28,974✔
2000
    auto target_upload_version = m_upload_target_version;
56,538✔
2001
    if (m_is_flx_sync_session) {
56,538✔
2002
        if (!m_pending_flx_sub_set || m_pending_flx_sub_set->snapshot_version < m_upload_progress.client_version) {
3,718✔
2003
            m_pending_flx_sub_set = get_flx_subscription_store()->get_next_pending_version(
3,028✔
2004
                m_last_sent_flx_query_version, m_upload_progress.client_version);
3,028✔
2005
        }
3,028✔
2006
        if (m_pending_flx_sub_set && m_pending_flx_sub_set->snapshot_version < m_upload_target_version) {
3,718✔
2007
            logger.trace("Limiting UPLOAD message up to version %1 to send QUERY version %2",
690✔
2008
                         m_pending_flx_sub_set->snapshot_version, m_pending_flx_sub_set->query_version);
690✔
2009
            target_upload_version = m_pending_flx_sub_set->snapshot_version;
690✔
2010
        }
690✔
2011
    }
3,718✔
2012

28,974✔
2013
    const ClientReplication& repl = access_realm(); // Throws
56,538✔
2014

28,974✔
2015
    std::vector<UploadChangeset> uploadable_changesets;
56,538✔
2016
    version_type locked_server_version = 0;
56,538✔
2017
    repl.get_history().find_uploadable_changesets(m_upload_progress, target_upload_version, uploadable_changesets,
56,538✔
2018
                                                  locked_server_version); // Throws
56,538✔
2019

28,974✔
2020
    if (uploadable_changesets.empty()) {
56,538✔
2021
        // Nothing more to upload right now
13,654✔
2022
        check_for_upload_completion(); // Throws
27,302✔
2023
    }
27,302✔
2024
    else {
29,236✔
2025
        m_last_version_selected_for_upload = uploadable_changesets.back().progress.client_version;
29,236✔
2026
    }
29,236✔
2027

28,974✔
2028
    version_type progress_client_version = m_upload_progress.client_version;
56,538✔
2029
    version_type progress_server_version = m_upload_progress.last_integrated_server_version;
56,538✔
2030

28,974✔
2031
    logger.debug("Sending: UPLOAD(progress_client_version=%1, progress_server_version=%2, "
56,538✔
2032
                 "locked_server_version=%3, num_changesets=%4)",
56,538✔
2033
                 progress_client_version, progress_server_version, locked_server_version,
56,538✔
2034
                 uploadable_changesets.size()); // Throws
56,538✔
2035

28,974✔
2036
    ClientProtocol& protocol = m_conn.get_client_protocol();
56,538✔
2037
    ClientProtocol::UploadMessageBuilder upload_message_builder = protocol.make_upload_message_builder(); // Throws
56,538✔
2038

28,974✔
2039
    for (const UploadChangeset& uc : uploadable_changesets) {
51,106✔
2040
        logger.debug("Fetching changeset for upload (client_version=%1, server_version=%2, "
42,752✔
2041
                     "changeset_size=%3, origin_timestamp=%4, origin_file_ident=%5)",
42,752✔
2042
                     uc.progress.client_version, uc.progress.last_integrated_server_version, uc.changeset.size(),
42,752✔
2043
                     uc.origin_timestamp, uc.origin_file_ident); // Throws
42,752✔
2044
        if (logger.would_log(util::Logger::Level::trace)) {
42,752✔
2045
            BinaryData changeset_data = uc.changeset.get_first_chunk();
×
2046
            if (changeset_data.size() < 1024) {
×
2047
                logger.trace("Changeset: %1",
×
2048
                             _impl::clamped_hex_dump(changeset_data)); // Throws
×
2049
            }
×
2050
            else {
×
2051
                logger.trace("Changeset(comp): %1 %2", changeset_data.size(),
×
2052
                             protocol.compressed_hex_dump(changeset_data));
×
2053
            }
×
2054

2055
#if REALM_DEBUG
×
2056
            ChunkedBinaryInputStream in{changeset_data};
×
2057
            Changeset log;
×
2058
            try {
×
2059
                parse_changeset(in, log);
×
2060
                std::stringstream ss;
×
2061
                log.print(ss);
×
2062
                logger.trace("Changeset (parsed):\n%1", ss.str());
×
2063
            }
×
2064
            catch (const BadChangesetError& err) {
×
2065
                logger.error("Unable to parse changeset: %1", err.what());
×
2066
            }
×
2067
#endif
×
2068
        }
×
2069

20,620✔
2070
#if 0 // Upload log compaction is currently not implemented
2071
        if (!get_client().m_disable_upload_compaction) {
2072
            ChangesetEncoder::Buffer encode_buffer;
2073

2074
            {
2075
                // Upload compaction only takes place within single changesets to
2076
                // avoid another client seeing inconsistent snapshots.
2077
                ChunkedBinaryInputStream stream{uc.changeset};
2078
                Changeset changeset;
2079
                parse_changeset(stream, changeset); // Throws
2080
                // FIXME: What is the point of setting these? How can compaction care about them?
2081
                changeset.version = uc.progress.client_version;
2082
                changeset.last_integrated_remote_version = uc.progress.last_integrated_server_version;
2083
                changeset.origin_timestamp = uc.origin_timestamp;
2084
                changeset.origin_file_ident = uc.origin_file_ident;
2085

2086
                compact_changesets(&changeset, 1);
2087
                encode_changeset(changeset, encode_buffer);
2088

2089
                logger.debug("Upload compaction: original size = %1, compacted size = %2", uc.changeset.size(),
2090
                             encode_buffer.size()); // Throws
2091
            }
2092

2093
            upload_message_builder.add_changeset(
2094
                uc.progress.client_version, uc.progress.last_integrated_server_version, uc.origin_timestamp,
2095
                uc.origin_file_ident, BinaryData{encode_buffer.data(), encode_buffer.size()}); // Throws
2096
        }
2097
        else
2098
#endif
2099
        {
42,752✔
2100
            upload_message_builder.add_changeset(uc.progress.client_version,
42,752✔
2101
                                                 uc.progress.last_integrated_server_version, uc.origin_timestamp,
42,752✔
2102
                                                 uc.origin_file_ident,
42,752✔
2103
                                                 uc.changeset); // Throws
42,752✔
2104
        }
42,752✔
2105
    }
42,752✔
2106

28,974✔
2107
    int protocol_version = m_conn.get_negotiated_protocol_version();
56,538✔
2108
    OutputBuffer& out = m_conn.get_output_buffer();
56,538✔
2109
    session_ident_type session_ident = get_ident();
56,538✔
2110
    upload_message_builder.make_upload_message(protocol_version, out, session_ident, progress_client_version,
56,538✔
2111
                                               progress_server_version,
56,538✔
2112
                                               locked_server_version); // Throws
56,538✔
2113
    m_conn.initiate_write_message(out, this);                          // Throws
56,538✔
2114

28,974✔
2115
    // Other messages may be waiting to be sent
28,974✔
2116
    enlist_to_send(); // Throws
56,538✔
2117
}
56,538✔
2118

2119

2120
void Session::send_mark_message()
2121
{
16,102✔
2122
    REALM_ASSERT_EX(m_state == Active, m_state);
16,102✔
2123
    REALM_ASSERT(m_ident_message_sent);
16,102✔
2124
    REALM_ASSERT(!m_unbind_message_sent);
16,102✔
2125
    REALM_ASSERT_3(m_target_download_mark, >, m_last_download_mark_sent);
16,102✔
2126

7,976✔
2127
    request_ident_type request_ident = m_target_download_mark;
16,102✔
2128
    logger.debug("Sending: MARK(request_ident=%1)", request_ident); // Throws
16,102✔
2129

7,976✔
2130
    ClientProtocol& protocol = m_conn.get_client_protocol();
16,102✔
2131
    OutputBuffer& out = m_conn.get_output_buffer();
16,102✔
2132
    session_ident_type session_ident = get_ident();
16,102✔
2133
    protocol.make_mark_message(out, session_ident, request_ident); // Throws
16,102✔
2134
    m_conn.initiate_write_message(out, this);                      // Throws
16,102✔
2135

7,976✔
2136
    m_last_download_mark_sent = request_ident;
16,102✔
2137

7,976✔
2138
    // Other messages may be waiting to be sent
7,976✔
2139
    enlist_to_send(); // Throws
16,102✔
2140
}
16,102✔
2141

2142

2143
void Session::send_unbind_message()
2144
{
5,098✔
2145
    REALM_ASSERT_EX(m_state == Deactivating || m_error_message_received || m_suspended, m_state);
5,098✔
2146
    REALM_ASSERT(m_bind_message_sent);
5,098✔
2147
    REALM_ASSERT(!m_unbind_message_sent);
5,098✔
2148

2,460✔
2149
    logger.debug("Sending: UNBIND"); // Throws
5,098✔
2150

2,460✔
2151
    ClientProtocol& protocol = m_conn.get_client_protocol();
5,098✔
2152
    OutputBuffer& out = m_conn.get_output_buffer();
5,098✔
2153
    session_ident_type session_ident = get_ident();
5,098✔
2154
    protocol.make_unbind_message(out, session_ident); // Throws
5,098✔
2155
    m_conn.initiate_write_message(out, this);         // Throws
5,098✔
2156

2,460✔
2157
    m_unbind_message_sent = true;
5,098✔
2158
}
5,098✔
2159

2160

2161
void Session::send_json_error_message()
2162
{
30✔
2163
    REALM_ASSERT_EX(m_state == Active, m_state);
30✔
2164
    REALM_ASSERT(m_ident_message_sent);
30✔
2165
    REALM_ASSERT(!m_unbind_message_sent);
30✔
2166
    REALM_ASSERT(m_error_to_send);
30✔
2167
    REALM_ASSERT(m_client_error);
30✔
2168

14✔
2169
    ClientProtocol& protocol = m_conn.get_client_protocol();
30✔
2170
    OutputBuffer& out = m_conn.get_output_buffer();
30✔
2171
    session_ident_type session_ident = get_ident();
30✔
2172
    auto protocol_error = m_client_error->error_for_server;
30✔
2173

14✔
2174
    auto message = util::format("%1", m_client_error->to_status());
30✔
2175
    logger.info("Sending: ERROR \"%1\" (error_code=%2, session_ident=%3)", message, static_cast<int>(protocol_error),
30✔
2176
                session_ident); // Throws
30✔
2177

14✔
2178
    nlohmann::json error_body_json;
30✔
2179
    error_body_json["message"] = std::move(message);
30✔
2180
    protocol.make_json_error_message(out, session_ident, static_cast<int>(protocol_error),
30✔
2181
                                     error_body_json.dump()); // Throws
30✔
2182
    m_conn.initiate_write_message(out, this);                 // Throws
30✔
2183

14✔
2184
    m_error_to_send = false;
30✔
2185
    enlist_to_send(); // Throws
30✔
2186
}
30✔
2187

2188

2189
void Session::send_test_command_message()
2190
{
44✔
2191
    REALM_ASSERT_EX(m_state == Active, m_state);
44✔
2192

22✔
2193
    auto it = std::find_if(m_pending_test_commands.begin(), m_pending_test_commands.end(),
44✔
2194
                           [](const PendingTestCommand& command) {
44✔
2195
                               return command.pending;
44✔
2196
                           });
44✔
2197
    REALM_ASSERT(it != m_pending_test_commands.end());
44✔
2198

22✔
2199
    ClientProtocol& protocol = m_conn.get_client_protocol();
44✔
2200
    OutputBuffer& out = m_conn.get_output_buffer();
44✔
2201
    auto session_ident = get_ident();
44✔
2202

22✔
2203
    logger.info("Sending: TEST_COMMAND \"%1\" (session_ident=%2, request_ident=%3)", it->body, session_ident, it->id);
44✔
2204
    protocol.make_test_command_message(out, session_ident, it->id, it->body);
44✔
2205

22✔
2206
    m_conn.initiate_write_message(out, this); // Throws;
44✔
2207
    it->pending = false;
44✔
2208

22✔
2209
    enlist_to_send();
44✔
2210
}
44✔
2211

2212

2213
Status Session::receive_ident_message(SaltedFileIdent client_file_ident)
2214
{
3,176✔
2215
    logger.debug("Received: IDENT(client_file_ident=%1, client_file_ident_salt=%2)", client_file_ident.ident,
3,176✔
2216
                 client_file_ident.salt); // Throws
3,176✔
2217

1,502✔
2218
    // Ignore the message if the deactivation process has been initiated,
1,502✔
2219
    // because in that case, the associated Realm and SessionWrapper must
1,502✔
2220
    // not be accessed any longer.
1,502✔
2221
    if (m_state != Active)
3,176✔
2222
        return Status::OK(); // Success
38✔
2223

1,488✔
2224
    bool legal_at_this_time = (m_bind_message_sent && !have_client_file_ident() && !m_error_message_received &&
3,138✔
2225
                               !m_unbound_message_received);
3,138✔
2226
    if (REALM_UNLIKELY(!legal_at_this_time)) {
3,138✔
2227
        return {ErrorCodes::SyncProtocolInvariantFailed, "Received IDENT message when it was not legal"};
×
2228
    }
×
2229
    if (REALM_UNLIKELY(client_file_ident.ident < 1)) {
3,138✔
2230
        return {ErrorCodes::SyncProtocolInvariantFailed, "Bad client file identifier in IDENT message"};
×
2231
    }
×
2232
    if (REALM_UNLIKELY(client_file_ident.salt == 0)) {
3,138✔
2233
        return {ErrorCodes::SyncProtocolInvariantFailed, "Bad client file identifier salt in IDENT message"};
×
2234
    }
×
2235

1,488✔
2236
    m_client_file_ident = client_file_ident;
3,138✔
2237

1,488✔
2238
    if (REALM_UNLIKELY(get_client().is_dry_run())) {
3,138✔
2239
        // Ready to send the IDENT message
2240
        ensure_enlisted_to_send(); // Throws
×
2241
        return Status::OK();       // Success
×
2242
    }
×
2243

1,488✔
2244
    // access before the client reset (if applicable) because
1,488✔
2245
    // the reset can take a while and the sync session might have died
1,488✔
2246
    // by the time the reset finishes.
1,488✔
2247
    ClientReplication& repl = access_realm(); // Throws
3,138✔
2248

1,488✔
2249
    auto client_reset_if_needed = [&]() -> bool {
3,138✔
2250
        if (!m_client_reset_operation) {
3,138✔
2251
            return false;
2,802✔
2252
        }
2,802✔
2253

168✔
2254
        // ClientResetOperation::finalize() will return true only if the operation actually did
168✔
2255
        // a client reset. It may choose not to do a reset if the local Realm does not exist
168✔
2256
        // at this point (in that case there is nothing to reset). But in any case, we must
168✔
2257
        // clean up m_client_reset_operation at this point as sync should be able to continue from
168✔
2258
        // this point forward.
168✔
2259
        auto client_reset_operation = std::move(m_client_reset_operation);
336✔
2260
        util::UniqueFunction<void(int64_t)> on_flx_subscription_complete = [this](int64_t version) {
220✔
2261
            this->on_flx_sync_version_complete(version);
104✔
2262
        };
104✔
2263
        if (!client_reset_operation->finalize(client_file_ident, get_flx_subscription_store(),
336✔
2264
                                              std::move(on_flx_subscription_complete))) {
168✔
2265
            return false;
×
2266
        }
×
2267
        realm::VersionID client_reset_old_version = client_reset_operation->get_client_reset_old_version();
336✔
2268
        realm::VersionID client_reset_new_version = client_reset_operation->get_client_reset_new_version();
336✔
2269

168✔
2270
        // The fresh Realm has been used to reset the state
168✔
2271
        logger.debug("Client reset is completed, path=%1", get_realm_path()); // Throws
336✔
2272

168✔
2273
        SaltedFileIdent client_file_ident;
336✔
2274
        bool has_pending_client_reset = false;
336✔
2275
        repl.get_history().get_status(m_last_version_available, client_file_ident, m_progress,
336✔
2276
                                      &has_pending_client_reset); // Throws
336✔
2277
        REALM_ASSERT_3(m_client_file_ident.ident, ==, client_file_ident.ident);
336✔
2278
        REALM_ASSERT_3(m_client_file_ident.salt, ==, client_file_ident.salt);
336✔
2279
        REALM_ASSERT_EX(m_progress.download.last_integrated_client_version == 0,
336✔
2280
                        m_progress.download.last_integrated_client_version);
336✔
2281
        REALM_ASSERT_EX(m_progress.upload.client_version == 0, m_progress.upload.client_version);
336✔
2282
        REALM_ASSERT_EX(m_progress.upload.last_integrated_server_version == 0,
336✔
2283
                        m_progress.upload.last_integrated_server_version);
336✔
2284
        logger.trace("last_version_available  = %1", m_last_version_available); // Throws
336✔
2285

168✔
2286
        m_upload_target_version = m_last_version_available;
336✔
2287
        m_upload_progress = m_progress.upload;
336✔
2288
        m_download_progress = m_progress.download;
336✔
2289
        // In recovery mode, there may be new changesets to upload and nothing left to download.
168✔
2290
        // In FLX DiscardLocal mode, there may be new commits due to subscription handling.
168✔
2291
        // For both, we want to allow uploads again without needing external changes to download first.
168✔
2292
        m_allow_upload = true;
336✔
2293
        REALM_ASSERT_EX(m_last_version_selected_for_upload == 0, m_last_version_selected_for_upload);
336✔
2294

168✔
2295
        get_transact_reporter()->report_sync_transact(client_reset_old_version, client_reset_new_version);
336✔
2296

168✔
2297
        if (has_pending_client_reset) {
336✔
2298
            handle_pending_client_reset_acknowledgement();
268✔
2299
        }
268✔
2300

168✔
2301
        // If a migration or rollback is in progress, mark it complete when client reset is completed.
168✔
2302
        if (auto migration_store = get_migration_store()) {
336✔
2303
            migration_store->complete_migration_or_rollback();
240✔
2304
        }
240✔
2305

168✔
2306
        return true;
336✔
2307
    };
336✔
2308
    // if a client reset happens, it will take care of setting the file ident
1,488✔
2309
    // and if not, we do it here
1,488✔
2310
    bool did_client_reset = false;
3,138✔
2311
    try {
3,138✔
2312
        did_client_reset = client_reset_if_needed();
3,138✔
2313
    }
3,138✔
2314
    catch (const std::exception& e) {
1,522✔
2315
        auto err_msg = util::format("A fatal error occurred during client reset: '%1'", e.what());
68✔
2316
        logger.error(err_msg.c_str());
68✔
2317
        SessionErrorInfo err_info(Status{ErrorCodes::AutoClientResetFailed, err_msg}, IsFatal{true});
68✔
2318
        suspend(err_info);
68✔
2319
        return Status::OK();
68✔
2320
    }
68✔
2321
    if (!did_client_reset) {
3,070✔
2322
        repl.get_history().set_client_file_ident(client_file_ident,
2,802✔
2323
                                                 m_fix_up_object_ids); // Throws
2,802✔
2324
        m_progress.download.last_integrated_client_version = 0;
2,802✔
2325
        m_progress.upload.client_version = 0;
2,802✔
2326
        m_last_version_selected_for_upload = 0;
2,802✔
2327
    }
2,802✔
2328

1,454✔
2329
    // Ready to send the IDENT message
1,454✔
2330
    ensure_enlisted_to_send(); // Throws
3,070✔
2331
    return Status::OK();       // Success
3,070✔
2332
}
3,070✔
2333

2334
Status Session::receive_download_message(const SyncProgress& progress, std::uint_fast64_t downloadable_bytes,
2335
                                         DownloadBatchState batch_state, int64_t query_version,
2336
                                         const ReceivedChangesets& received_changesets)
2337
{
41,774✔
2338
    // Ignore the message if the deactivation process has been initiated,
22,134✔
2339
    // because in that case, the associated Realm and SessionWrapper must
22,134✔
2340
    // not be accessed any longer.
22,134✔
2341
    if (m_state != Active)
41,774✔
2342
        return Status::OK();
502✔
2343

21,858✔
2344
    if (is_steady_state_download_message(batch_state, query_version)) {
41,272✔
2345
        batch_state = DownloadBatchState::SteadyState;
39,812✔
2346
    }
39,812✔
2347

21,858✔
2348
    logger.debug("Received: DOWNLOAD(download_server_version=%1, download_client_version=%2, "
41,272✔
2349
                 "latest_server_version=%3, latest_server_version_salt=%4, "
41,272✔
2350
                 "upload_client_version=%5, upload_server_version=%6, downloadable_bytes=%7, "
41,272✔
2351
                 "last_in_batch=%8, query_version=%9, num_changesets=%10, ...)",
41,272✔
2352
                 progress.download.server_version, progress.download.last_integrated_client_version,
41,272✔
2353
                 progress.latest_server_version.version, progress.latest_server_version.salt,
41,272✔
2354
                 progress.upload.client_version, progress.upload.last_integrated_server_version, downloadable_bytes,
41,272✔
2355
                 batch_state != DownloadBatchState::MoreToCome, query_version, received_changesets.size()); // Throws
41,272✔
2356

21,858✔
2357
    // Ignore download messages when the client detects an error. This is to prevent transforming the same bad
21,858✔
2358
    // changeset over and over again.
21,858✔
2359
    if (m_client_error) {
41,272✔
2360
        logger.debug("Ignoring download message because the client detected an integration error");
×
2361
        return Status::OK();
×
2362
    }
×
2363

21,858✔
2364
    bool legal_at_this_time = (m_ident_message_sent && !m_error_message_received && !m_unbound_message_received);
41,272✔
2365
    if (REALM_UNLIKELY(!legal_at_this_time)) {
41,272✔
2366
        return {ErrorCodes::SyncProtocolInvariantFailed, "Received DOWNLOAD message when it was not legal"};
×
2367
    }
×
2368
    if (auto status = check_received_sync_progress(progress); REALM_UNLIKELY(!status.is_ok())) {
41,272✔
2369
        logger.error("Bad sync progress received (%1)", status);
×
2370
        return status;
×
2371
    }
×
2372

21,858✔
2373
    version_type server_version = m_progress.download.server_version;
41,272✔
2374
    version_type last_integrated_client_version = m_progress.download.last_integrated_client_version;
41,272✔
2375
    for (const Transformer::RemoteChangeset& changeset : received_changesets) {
43,036✔
2376
        // Check that per-changeset server version is strictly increasing, except in FLX sync where the server
21,856✔
2377
        // version must be increasing, but can stay the same during bootstraps.
21,856✔
2378
        bool good_server_version = m_is_flx_sync_session ? (changeset.remote_version >= server_version)
22,642✔
2379
                                                         : (changeset.remote_version > server_version);
42,248✔
2380
        // Each server version cannot be greater than the one in the header of the download message.
21,856✔
2381
        good_server_version = good_server_version && (changeset.remote_version <= progress.download.server_version);
43,034✔
2382
        if (!good_server_version) {
43,034✔
2383
            return {ErrorCodes::SyncProtocolInvariantFailed,
×
2384
                    util::format("Bad server version in changeset header (DOWNLOAD) (%1, %2, %3)",
×
2385
                                 changeset.remote_version, server_version, progress.download.server_version)};
×
2386
        }
×
2387
        server_version = changeset.remote_version;
43,034✔
2388
        // Check that per-changeset last integrated client version is "weakly"
21,856✔
2389
        // increasing.
21,856✔
2390
        bool good_client_version =
43,034✔
2391
            (changeset.last_integrated_local_version >= last_integrated_client_version &&
43,034✔
2392
             changeset.last_integrated_local_version <= progress.download.last_integrated_client_version);
43,034✔
2393
        if (!good_client_version) {
43,034✔
2394
            return {ErrorCodes::SyncProtocolInvariantFailed,
×
2395
                    util::format("Bad last integrated client version in changeset header (DOWNLOAD) "
×
2396
                                 "(%1, %2, %3)",
×
2397
                                 changeset.last_integrated_local_version, last_integrated_client_version,
×
2398
                                 progress.download.last_integrated_client_version)};
×
2399
        }
×
2400
        last_integrated_client_version = changeset.last_integrated_local_version;
43,034✔
2401
        // Server shouldn't send our own changes, and zero is not a valid client
21,856✔
2402
        // file identifier.
21,856✔
2403
        bool good_file_ident =
43,034✔
2404
            (changeset.origin_file_ident > 0 && changeset.origin_file_ident != m_client_file_ident.ident);
43,034✔
2405
        if (!good_file_ident) {
43,034✔
2406
            return {ErrorCodes::SyncProtocolInvariantFailed,
×
2407
                    util::format("Bad origin file identifier in changeset header (DOWNLOAD)",
×
2408
                                 changeset.origin_file_ident)};
×
2409
        }
×
2410
    }
43,034✔
2411

21,858✔
2412
    auto hook_action = call_debug_hook(SyncClientHookEvent::DownloadMessageReceived, progress, query_version,
41,272✔
2413
                                       batch_state, received_changesets.size());
41,272✔
2414
    if (hook_action == SyncClientHookAction::EarlyReturn) {
41,272✔
2415
        return Status::OK();
12✔
2416
    }
12✔
2417
    REALM_ASSERT_EX(hook_action == SyncClientHookAction::NoAction, hook_action);
41,260✔
2418

21,852✔
2419
    if (process_flx_bootstrap_message(progress, batch_state, query_version, received_changesets)) {
41,260✔
2420
        clear_resumption_delay_state();
1,452✔
2421
        return Status::OK();
1,452✔
2422
    }
1,452✔
2423

21,126✔
2424
    initiate_integrate_changesets(downloadable_bytes, batch_state, progress, received_changesets); // Throws
39,808✔
2425

21,126✔
2426
    hook_action = call_debug_hook(SyncClientHookEvent::DownloadMessageIntegrated, progress, query_version,
39,808✔
2427
                                  batch_state, received_changesets.size());
39,808✔
2428
    if (hook_action == SyncClientHookAction::EarlyReturn) {
39,808✔
2429
        return Status::OK();
×
2430
    }
×
2431
    REALM_ASSERT_EX(hook_action == SyncClientHookAction::NoAction, hook_action);
39,808✔
2432

21,126✔
2433
    // When we receive a DOWNLOAD message successfully, we can clear the backoff timer value used to reconnect
21,126✔
2434
    // after a retryable session error.
21,126✔
2435
    clear_resumption_delay_state();
39,808✔
2436
    return Status::OK();
39,808✔
2437
}
39,808✔
2438

2439
Status Session::receive_mark_message(request_ident_type request_ident)
2440
{
15,564✔
2441
    logger.debug("Received: MARK(request_ident=%1)", request_ident); // Throws
15,564✔
2442

7,698✔
2443
    // Ignore the message if the deactivation process has been initiated,
7,698✔
2444
    // because in that case, the associated Realm and SessionWrapper must
7,698✔
2445
    // not be accessed any longer.
7,698✔
2446
    if (m_state != Active)
15,564✔
2447
        return Status::OK(); // Success
44✔
2448

7,666✔
2449
    bool legal_at_this_time = (m_ident_message_sent && !m_error_message_received && !m_unbound_message_received);
15,520✔
2450
    if (REALM_UNLIKELY(!legal_at_this_time)) {
15,520✔
2451
        return {ErrorCodes::SyncProtocolInvariantFailed, "Received MARK message when it was not legal"};
×
2452
    }
×
2453
    bool good_request_ident =
15,520✔
2454
        (request_ident <= m_last_download_mark_sent && request_ident > m_last_download_mark_received);
15,520✔
2455
    if (REALM_UNLIKELY(!good_request_ident)) {
15,520✔
2456
        return {
×
2457
            ErrorCodes::SyncProtocolInvariantFailed,
×
2458
            util::format(
×
2459
                "Received MARK message with invalid request identifer (last mark sent: %1 last mark received: %2)",
×
2460
                m_last_download_mark_sent, m_last_download_mark_received)};
×
2461
    }
×
2462

7,666✔
2463
    m_server_version_at_last_download_mark = m_progress.download.server_version;
15,520✔
2464
    m_last_download_mark_received = request_ident;
15,520✔
2465
    check_for_download_completion(); // Throws
15,520✔
2466

7,666✔
2467
    return Status::OK(); // Success
15,520✔
2468
}
15,520✔
2469

2470

2471
// The caller (Connection) must discard the session if the session has become
2472
// deactivated upon return.
2473
Status Session::receive_unbound_message()
2474
{
3,140✔
2475
    logger.debug("Received: UNBOUND");
3,140✔
2476

1,520✔
2477
    bool legal_at_this_time = (m_unbind_message_sent && !m_error_message_received && !m_unbound_message_received);
3,140✔
2478
    if (REALM_UNLIKELY(!legal_at_this_time)) {
3,140✔
2479
        return {ErrorCodes::SyncProtocolInvariantFailed, "Received UNBOUND message when it was not legal"};
×
2480
    }
×
2481

1,520✔
2482
    // The fact that the UNBIND message has been sent, but an ERROR message has
1,520✔
2483
    // not been received, implies that the deactivation process must have been
1,520✔
2484
    // initiated, so this session must be in the Deactivating state or the session
1,520✔
2485
    // has been suspended because of a client side error.
1,520✔
2486
    REALM_ASSERT_EX(m_state == Deactivating || m_suspended, m_state);
3,140!
2487

1,520✔
2488
    m_unbound_message_received = true;
3,140✔
2489

1,520✔
2490
    // Detect completion of the unbinding process
1,520✔
2491
    if (m_unbind_message_send_complete && m_state == Deactivating) {
3,140✔
2492
        // The deactivation process completes when the unbinding process
1,520✔
2493
        // completes.
1,520✔
2494
        complete_deactivation(); // Throws
3,140✔
2495
        // Life cycle state is now Deactivated
1,520✔
2496
    }
3,140✔
2497

1,520✔
2498
    return Status::OK(); // Success
3,140✔
2499
}
3,140✔
2500

2501

2502
Status Session::receive_query_error_message(int error_code, std::string_view message, int64_t query_version)
2503
{
16✔
2504
    logger.info("Received QUERY_ERROR \"%1\" (error_code=%2, query_version=%3)", message, error_code, query_version);
16✔
2505
    // Ignore the message if the deactivation process has been initiated,
8✔
2506
    // because in that case, the associated Realm and SessionWrapper must
8✔
2507
    // not be accessed any longer.
8✔
2508
    if (m_state == Active) {
16✔
2509
        on_flx_sync_error(query_version, std::string_view(message.data(), message.size())); // throws
16✔
2510
    }
16✔
2511
    return Status::OK();
16✔
2512
}
16✔
2513

2514
// The caller (Connection) must discard the session if the session has become
2515
// deactivated upon return.
2516
Status Session::receive_error_message(const ProtocolErrorInfo& info)
2517
{
872✔
2518
    logger.info("Received: ERROR \"%1\" (error_code=%2, is_fatal=%3, error_action=%4)", info.message,
872✔
2519
                info.raw_error_code, info.is_fatal, info.server_requests_action); // Throws
872✔
2520

444✔
2521
    bool legal_at_this_time = (m_bind_message_sent && !m_error_message_received && !m_unbound_message_received);
872✔
2522
    if (REALM_UNLIKELY(!legal_at_this_time)) {
872✔
2523
        return {ErrorCodes::SyncProtocolInvariantFailed, "Received ERROR message when it was not legal"};
×
2524
    }
×
2525

444✔
2526
    auto protocol_error = static_cast<ProtocolError>(info.raw_error_code);
872✔
2527
    auto status = protocol_error_to_status(protocol_error, info.message);
872✔
2528
    if (status != ErrorCodes::UnknownError && REALM_UNLIKELY(!is_session_level_error(protocol_error))) {
872✔
2529
        return {ErrorCodes::SyncProtocolInvariantFailed,
×
2530
                util::format("Received ERROR message for session with non-session-level error code %1",
×
2531
                             info.raw_error_code)};
×
2532
    }
×
2533

444✔
2534
    // Can't process debug hook actions once the Session is undergoing deactivation, since
444✔
2535
    // the SessionWrapper may not be available
444✔
2536
    if (m_state == Active) {
872✔
2537
        auto debug_action = call_debug_hook(SyncClientHookEvent::ErrorMessageReceived, info);
868✔
2538
        if (debug_action == SyncClientHookAction::EarlyReturn) {
868✔
2539
            return Status::OK();
4✔
2540
        }
4✔
2541
    }
868✔
2542

442✔
2543
    // For compensating write errors, we need to defer raising them to the SDK until after the server version
442✔
2544
    // containing the compensating write has appeared in a download message.
442✔
2545
    if (status == ErrorCodes::SyncCompensatingWrite) {
868✔
2546
        // If the client is not active, the compensating writes will not be processed now, but will be
20✔
2547
        // sent again the next time the client connects
20✔
2548
        if (m_state == Active) {
40✔
2549
            m_pending_compensating_write_errors.push_back(info);
40✔
2550
        }
40✔
2551
        return Status::OK();
40✔
2552
    }
40✔
2553

422✔
2554
    m_error_message_received = true;
828✔
2555
    suspend(SessionErrorInfo{info, std::move(status)});
828✔
2556
    return Status::OK();
828✔
2557
}
828✔
2558

2559
void Session::suspend(const SessionErrorInfo& info)
2560
{
896✔
2561
    REALM_ASSERT(!m_suspended);
896✔
2562
    REALM_ASSERT_EX(m_state == Active || m_state == Deactivating, m_state);
896✔
2563
    logger.debug("Suspended"); // Throws
896✔
2564

456✔
2565
    m_suspended = true;
896✔
2566

456✔
2567
    // Detect completion of the unbinding process
456✔
2568
    if (m_unbind_message_send_complete && m_error_message_received) {
896!
2569
        // The fact that the UNBIND message has been sent, but we are not being suspended because
2570
        // we received an ERROR message implies that the deactivation process must
2571
        // have been initiated, so this session must be in the Deactivating state.
2572
        REALM_ASSERT_EX(m_state == Deactivating, m_state);
×
2573

2574
        // The deactivation process completes when the unbinding process
2575
        // completes.
2576
        complete_deactivation(); // Throws
×
2577
        // Life cycle state is now Deactivated
2578
    }
×
2579

456✔
2580
    // Notify the application of the suspension of the session if the session is
456✔
2581
    // still in the Active state
456✔
2582
    if (m_state == Active) {
896✔
2583
        m_conn.one_less_active_unsuspended_session(); // Throws
892✔
2584
        on_suspended(info);                           // Throws
892✔
2585
    }
892✔
2586

456✔
2587
    if (!info.is_fatal) {
896✔
2588
        begin_resumption_delay(info);
368✔
2589
    }
368✔
2590

456✔
2591
    // Ready to send the UNBIND message, if it has not been sent already
456✔
2592
    if (!m_unbind_message_sent)
896✔
2593
        ensure_enlisted_to_send(); // Throws
892✔
2594
}
896✔
2595

2596
Status Session::receive_test_command_response(request_ident_type ident, std::string_view body)
2597
{
44✔
2598
    logger.info("Received: TEST_COMMAND \"%1\" (session_ident=%2, request_ident=%3)", body, m_ident, ident);
44✔
2599
    auto it = std::find_if(m_pending_test_commands.begin(), m_pending_test_commands.end(),
44✔
2600
                           [&](const PendingTestCommand& command) {
44✔
2601
                               return command.id == ident;
44✔
2602
                           });
44✔
2603
    if (it == m_pending_test_commands.end()) {
44✔
2604
        return {ErrorCodes::SyncProtocolInvariantFailed,
×
2605
                util::format("Received test command response for a non-existent ident %1", ident)};
×
2606
    }
×
2607

22✔
2608
    it->promise.emplace_value(std::string{body});
44✔
2609
    m_pending_test_commands.erase(it);
44✔
2610

22✔
2611
    return Status::OK();
44✔
2612
}
44✔
2613

2614
void Session::begin_resumption_delay(const ProtocolErrorInfo& error_info)
2615
{
368✔
2616
    REALM_ASSERT(!m_try_again_activation_timer);
368✔
2617

194✔
2618
    m_try_again_delay_info.update(static_cast<sync::ProtocolError>(error_info.raw_error_code),
368✔
2619
                                  error_info.resumption_delay_interval);
368✔
2620
    auto try_again_interval = m_try_again_delay_info.delay_interval();
368✔
2621
    if (ProtocolError(error_info.raw_error_code) == ProtocolError::session_closed) {
368✔
2622
        // FIXME With compensating writes the server sends this error after completing a bootstrap. Doing the
10✔
2623
        // normal backoff behavior would result in waiting up to 5 minutes in between each query change which is
10✔
2624
        // not acceptable latency. So for this error code alone, we hard-code a 1 second retry interval.
10✔
2625
        try_again_interval = std::chrono::milliseconds{1000};
20✔
2626
    }
20✔
2627
    logger.debug("Will attempt to resume session after %1 milliseconds", try_again_interval.count());
368✔
2628
    m_try_again_activation_timer = get_client().create_timer(try_again_interval, [this](Status status) {
368✔
2629
        if (status == ErrorCodes::OperationAborted)
368✔
2630
            return;
4✔
2631
        else if (!status.is_ok())
364✔
2632
            throw Exception(status);
×
2633

192✔
2634
        m_try_again_activation_timer.reset();
364✔
2635
        cancel_resumption_delay();
364✔
2636
    });
364✔
2637
}
368✔
2638

2639
void Session::clear_resumption_delay_state()
2640
{
41,262✔
2641
    if (m_try_again_activation_timer) {
41,262✔
2642
        logger.debug("Clearing resumption delay state after successful download");
×
2643
        m_try_again_delay_info.reset();
×
2644
    }
×
2645
}
41,262✔
2646

2647
Status ClientImpl::Session::check_received_sync_progress(const SyncProgress& progress) noexcept
2648
{
41,272✔
2649
    const SyncProgress& a = m_progress;
41,272✔
2650
    const SyncProgress& b = progress;
41,272✔
2651
    std::string message;
41,272✔
2652
    if (b.latest_server_version.version < a.latest_server_version.version) {
41,272✔
2653
        message = util::format("Latest server version in download messages must be weakly increasing throughout a "
×
2654
                               "session (current: %1, received: %2)",
×
2655
                               a.latest_server_version.version, b.latest_server_version.version);
×
2656
    }
×
2657
    if (b.upload.client_version < a.upload.client_version) {
41,272✔
2658
        message = util::format("Last integrated client version in download messages must be weakly increasing "
×
2659
                               "throughout a session (current: %1, received: %2)",
×
2660
                               a.upload.client_version, b.upload.client_version);
×
2661
    }
×
2662
    if (b.upload.client_version > m_last_version_available) {
41,272✔
2663
        message = util::format("Last integrated client version on server cannot be greater than the latest client "
×
2664
                               "version in existence (current: %1, received: %2)",
×
2665
                               m_last_version_available, b.upload.client_version);
×
2666
    }
×
2667
    if (b.download.server_version < a.download.server_version) {
41,272✔
2668
        message =
×
2669
            util::format("Download cursor must be weakly increasing throughout a session (current: %1, received: %2)",
×
2670
                         a.download.server_version, b.download.server_version);
×
2671
    }
×
2672
    if (b.download.server_version > b.latest_server_version.version) {
41,272✔
2673
        message = util::format(
×
2674
            "Download cursor cannot be greater than the latest server version in existence (cursor: %1, latest: %2)",
×
2675
            b.download.server_version, b.latest_server_version.version);
×
2676
    }
×
2677
    if (b.download.last_integrated_client_version < a.download.last_integrated_client_version) {
41,272✔
2678
        message = util::format(
×
2679
            "Last integrated client version on the server at the position in the server's history of the download "
×
2680
            "cursor must be weakly increasing throughout a session (current: %1, received: %2)",
×
2681
            a.download.last_integrated_client_version, b.download.last_integrated_client_version);
×
2682
    }
×
2683
    if (b.download.last_integrated_client_version > b.upload.client_version) {
41,272✔
2684
        message = util::format("Last integrated client version on the server in the position at the server's history "
×
2685
                               "of the download cursor cannot be greater than the latest client version integrated "
×
2686
                               "on the server (download: %1, upload: %2)",
×
2687
                               b.download.last_integrated_client_version, b.upload.client_version);
×
2688
    }
×
2689

21,858✔
2690
    if (message.empty()) {
41,272✔
2691
        return Status::OK();
41,272✔
2692
    }
41,272✔
2693
    return {ErrorCodes::SyncProtocolInvariantFailed, std::move(message)};
×
2694
}
×
2695

2696

2697
void Session::check_for_upload_completion()
2698
{
73,780✔
2699
    REALM_ASSERT_EX(m_state == Active, m_state);
73,780✔
2700
    if (!m_upload_completion_notification_requested) {
73,780✔
2701
        return;
43,156✔
2702
    }
43,156✔
2703

14,990✔
2704
    // during an ongoing client reset operation, we never upload anything
14,990✔
2705
    if (m_client_reset_operation)
30,624✔
2706
        return;
232✔
2707

14,874✔
2708
    // Upload process must have reached end of history
14,874✔
2709
    REALM_ASSERT_3(m_upload_progress.client_version, <=, m_last_version_available);
30,392✔
2710
    bool scan_complete = (m_upload_progress.client_version == m_last_version_available);
30,392✔
2711
    if (!scan_complete)
30,392✔
2712
        return;
4,760✔
2713

12,460✔
2714
    // All uploaded changesets must have been acknowledged by the server
12,460✔
2715
    REALM_ASSERT_3(m_progress.upload.client_version, <=, m_last_version_selected_for_upload);
25,632✔
2716
    bool all_uploads_accepted = (m_progress.upload.client_version == m_last_version_selected_for_upload);
25,632✔
2717
    if (!all_uploads_accepted)
25,632✔
2718
        return;
11,000✔
2719

7,228✔
2720
    m_upload_completion_notification_requested = false;
14,632✔
2721
    on_upload_completion(); // Throws
14,632✔
2722
}
14,632✔
2723

2724

2725
void Session::check_for_download_completion()
2726
{
56,604✔
2727
    REALM_ASSERT_3(m_target_download_mark, >=, m_last_download_mark_received);
56,604✔
2728
    REALM_ASSERT_3(m_last_download_mark_received, >=, m_last_triggering_download_mark);
56,604✔
2729
    if (m_last_download_mark_received == m_last_triggering_download_mark)
56,604✔
2730
        return;
40,906✔
2731
    if (m_last_download_mark_received < m_target_download_mark)
15,698✔
2732
        return;
380✔
2733
    if (m_download_progress.server_version < m_server_version_at_last_download_mark)
15,318✔
2734
        return;
×
2735
    m_last_triggering_download_mark = m_target_download_mark;
15,318✔
2736
    if (REALM_UNLIKELY(!m_allow_upload)) {
15,318✔
2737
        // Activate the upload process now, and enable immediate reactivation
1,964✔
2738
        // after a subsequent fast reconnect.
1,964✔
2739
        m_allow_upload = true;
4,118✔
2740
        ensure_enlisted_to_send(); // Throws
4,118✔
2741
    }
4,118✔
2742
    on_download_completion(); // Throws
15,318✔
2743
}
15,318✔
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