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

realm / realm-core / michael.wilkersonbarker_1094

06 May 2024 04:51PM UTC coverage: 90.761% (+0.3%) from 90.422%
michael.wilkersonbarker_1094

Pull #7675

Evergreen

michael-wb
Added test to create 2 user and 2 rules when only 1 updated
Pull Request #7675: RCORE-1973 Add role/permissions tests for new bootstrap feature

102046 of 180410 branches covered (56.56%)

13 of 15 new or added lines in 2 files covered. (86.67%)

47 existing lines in 9 files now uncovered.

212810 of 234473 relevant lines covered (90.76%)

5670520.72 hits per line

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

82.36
/src/realm/sync/noinst/client_impl_base.cpp
1
#include <realm/sync/noinst/client_impl_base.hpp>
2

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

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

26
#include <system_error>
27
#include <sstream>
28

29
// NOTE: The protocol specification is in `/doc/protocol.md`
30

31
using namespace realm;
32
using namespace _impl;
33
using namespace realm::util;
34
using namespace realm::sync;
35
using namespace realm::sync::websocket;
36

37
// clang-format off
38
using Connection      = ClientImpl::Connection;
39
using Session         = ClientImpl::Session;
40
using UploadChangeset = ClientHistory::UploadChangeset;
41

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

49
void ClientImpl::ReconnectInfo::reset() noexcept
50
{
1,972✔
51
    m_backoff_state.reset();
1,972✔
52
    scheduled_reset = false;
1,972✔
53
}
1,972✔
54

55

56
void ClientImpl::ReconnectInfo::update(ConnectionTerminationReason new_reason,
57
                                       std::optional<ResumptionDelayInfo> new_delay_info)
58
{
3,766✔
59
    m_backoff_state.update(new_reason, new_delay_info);
3,766✔
60
}
3,766✔
61

62

63
std::chrono::milliseconds ClientImpl::ReconnectInfo::delay_interval()
64
{
6,100✔
65
    if (scheduled_reset) {
6,100✔
66
        reset();
4✔
67
    }
4✔
68

69
    if (!m_backoff_state.triggering_error) {
6,100✔
70
        return std::chrono::milliseconds::zero();
4,654✔
71
    }
4,654✔
72

73
    switch (*m_backoff_state.triggering_error) {
1,446✔
74
        case ConnectionTerminationReason::closed_voluntarily:
80✔
75
            return std::chrono::milliseconds::zero();
80✔
76
        case ConnectionTerminationReason::server_said_do_not_reconnect:
18✔
77
            return std::chrono::milliseconds::max();
18✔
78
        default:
1,346✔
79
            if (m_reconnect_mode == ReconnectMode::testing) {
1,346✔
80
                return std::chrono::milliseconds::max();
1,012✔
81
            }
1,012✔
82

83
            REALM_ASSERT(m_reconnect_mode == ReconnectMode::normal);
334✔
84
            return m_backoff_state.delay_interval();
334✔
85
    }
1,446✔
86
}
1,446✔
87

88

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

133
    protocol = protocol_2;
4,072✔
134
    address = std::move(address_2);
4,072✔
135
    port = port_3;
4,072✔
136
    path = std::move(path_2);
4,072✔
137
    return true;
4,072✔
138
}
4,072✔
139

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

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

197
    if (config.reconnect_mode != ReconnectMode::normal) {
9,726✔
198
        logger.warn("Testing/debugging feature 'nonnormal reconnect mode' enabled - "
780✔
199
                    "never do this in production!");
780✔
200
    }
780✔
201

202
    if (config.dry_run) {
9,726✔
203
        logger.warn("Testing/debugging feature 'dry run' enabled - "
×
204
                    "never do this in production!");
×
205
    }
×
206

207
    REALM_ASSERT_EX(m_socket_provider, "Must provide socket provider in sync Client config");
9,726✔
208

209
    if (m_one_connection_per_session) {
9,726✔
210
        logger.warn("Testing/debugging feature 'one connection per session' enabled - "
4✔
211
                    "never do this in production");
4✔
212
    }
4✔
213

214
    if (config.disable_upload_activation_delay) {
9,726✔
215
        logger.warn("Testing/debugging feature 'disable_upload_activation_delay' enabled - "
×
216
                    "never do this in production");
×
217
    }
×
218

219
    if (config.disable_sync_to_disk) {
9,726✔
220
        logger.warn("Testing/debugging feature 'disable_sync_to_disk' enabled - "
×
221
                    "never do this in production");
×
222
    }
×
223

224
    m_actualize_and_finalize = create_trigger([this](Status status) {
15,330✔
225
        if (status == ErrorCodes::OperationAborted)
15,330✔
226
            return;
×
227
        else if (!status.is_ok())
15,330✔
228
            throw Exception(status);
×
229
        actualize_and_finalize_session_wrappers(); // Throws
15,330✔
230
    });
15,330✔
231
}
9,726✔
232

233

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

253

254
void ClientImpl::drain_connections()
255
{
9,714✔
256
    logger.debug("Draining connections during sync client shutdown");
9,714✔
257
    for (auto& server_slot_pair : m_server_slots) {
9,714✔
258
        auto& server_slot = server_slot_pair.second;
2,650✔
259

260
        if (server_slot.connection) {
2,650✔
261
            auto& conn = server_slot.connection;
2,428✔
262
            conn->force_close();
2,428✔
263
        }
2,428✔
264
        else {
222✔
265
            for (auto& conn_pair : server_slot.alt_connections) {
222✔
266
                conn_pair.second->force_close();
×
267
            }
×
268
        }
222✔
269
    }
2,650✔
270
}
9,714✔
271

272

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

285
        std::lock_guard lock(m_drain_mutex);
17,564✔
286
        REALM_ASSERT(m_outstanding_posts);
17,564✔
287
        --m_outstanding_posts;
17,564✔
288
        m_drain_cv.notify_all();
17,564✔
289
    });
17,564✔
290
}
17,570✔
291

292

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

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

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

318

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

339

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

358

359
void Connection::cancel_reconnect_delay()
360
{
2,184✔
361
    REALM_ASSERT(m_activated);
2,184✔
362

363
    if (m_reconnect_delay_in_progress) {
2,184✔
364
        if (m_nonzero_reconnect_delay)
1,964✔
365
            logger.detail("Canceling reconnect delay"); // Throws
978✔
366

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

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

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

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

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

413
    m_force_closed = true;
2,426✔
414

415
    if (m_state != ConnectionState::disconnected) {
2,426✔
416
        voluntary_disconnect();
2,394✔
417
    }
2,394✔
418

419
    REALM_ASSERT_EX(m_state == ConnectionState::disconnected, m_state);
2,426✔
420
    if (m_reconnect_delay_in_progress || m_disconnect_delay_in_progress) {
2,426✔
421
        m_reconnect_disconnect_timer.reset();
36✔
422
        m_reconnect_delay_in_progress = false;
36✔
423
        m_disconnect_delay_in_progress = false;
36✔
424
    }
36✔
425

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

436
    for (auto& sess : to_close) {
2,426✔
437
        sess->force_close();
102✔
438
    }
102✔
439

440
    logger.debug("Force closed idle connection");
2,426✔
441
}
2,426✔
442

443

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

486

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

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

502
    handle_message_received(data);
80,436✔
503
    return bool(m_websocket);
80,436✔
504
}
80,896✔
505

506

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

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

520
    switch (error_code) {
820✔
521
        case WebSocketError::websocket_ok:
58✔
522
            break;
58✔
523
        case WebSocketError::websocket_resolve_failed:
4✔
524
            [[fallthrough]];
4✔
525
        case WebSocketError::websocket_connection_failed: {
112✔
526
            SessionErrorInfo error_info(
112✔
527
                {ErrorCodes::SyncConnectFailed, util::format("Failed to connect to sync: %1", msg)}, IsFatal{false});
112✔
528
            // If the connection fails/times out and the server has not been contacted yet, refresh the location
529
            // to make sure the websocket URL is correct
530
            if (!m_server_endpoint.is_verified) {
112✔
531
                error_info.server_requests_action = ProtocolErrorInfo::Action::RefreshLocation;
84✔
532
            }
84✔
533
            involuntary_disconnect(std::move(error_info), ConnectionTerminationReason::connect_operation_failed);
112✔
534
            break;
112✔
535
        }
4✔
536
        case WebSocketError::websocket_read_error:
580✔
537
            [[fallthrough]];
580✔
538
        case WebSocketError::websocket_write_error: {
580✔
539
            close_due_to_transient_error({ErrorCodes::ConnectionClosed, msg},
580✔
540
                                         ConnectionTerminationReason::read_or_write_error);
580✔
541
            break;
580✔
542
        }
580✔
543
        case WebSocketError::websocket_going_away:
✔
544
            [[fallthrough]];
×
545
        case WebSocketError::websocket_protocol_error:
✔
546
            [[fallthrough]];
×
547
        case WebSocketError::websocket_unsupported_data:
✔
548
            [[fallthrough]];
×
549
        case WebSocketError::websocket_invalid_payload_data:
✔
550
            [[fallthrough]];
×
551
        case WebSocketError::websocket_policy_violation:
✔
552
            [[fallthrough]];
×
553
        case WebSocketError::websocket_reserved:
✔
554
            [[fallthrough]];
×
555
        case WebSocketError::websocket_no_status_received:
✔
556
            [[fallthrough]];
×
557
        case WebSocketError::websocket_invalid_extension: {
✔
558
            close_due_to_client_side_error({ErrorCodes::SyncProtocolInvariantFailed, msg}, IsFatal{false},
×
559
                                           ConnectionTerminationReason::websocket_protocol_violation); // Throws
×
560
            break;
×
561
        }
×
562
        case WebSocketError::websocket_message_too_big: {
4✔
563
            auto message = util::format(
4✔
564
                "Sync websocket closed because the server received a message that was too large: %1", msg);
4✔
565
            SessionErrorInfo error_info(Status(ErrorCodes::LimitExceeded, std::move(message)), IsFatal{false});
4✔
566
            error_info.server_requests_action = ProtocolErrorInfo::Action::ClientReset;
4✔
567
            involuntary_disconnect(std::move(error_info),
4✔
568
                                   ConnectionTerminationReason::websocket_protocol_violation); // Throws
4✔
569
            break;
4✔
570
        }
×
571
        case WebSocketError::websocket_tls_handshake_failed: {
10✔
572
            close_due_to_client_side_error(
10✔
573
                Status(ErrorCodes::TlsHandshakeFailed, util::format("TLS handshake failed: %1", msg)), IsFatal{false},
10✔
574
                ConnectionTerminationReason::ssl_certificate_rejected); // Throws
10✔
575
            break;
10✔
576
        }
×
577
        case WebSocketError::websocket_client_too_old:
✔
578
            [[fallthrough]];
×
579
        case WebSocketError::websocket_client_too_new:
✔
580
            [[fallthrough]];
×
581
        case WebSocketError::websocket_protocol_mismatch: {
✔
582
            close_due_to_client_side_error({ErrorCodes::SyncProtocolNegotiationFailed, msg}, IsFatal{true},
×
583
                                           ConnectionTerminationReason::http_response_says_fatal_error); // Throws
×
584
            break;
×
585
        }
×
586
        case WebSocketError::websocket_fatal_error: {
✔
587
            // Error is fatal if the sync_route has already been verified - if the sync_route has not
588
            // been verified, then use a non-fatal error and try to perform a location update.
589
            SessionErrorInfo error_info(
×
590
                {ErrorCodes::SyncConnectFailed, util::format("Failed to connect to sync: %1", msg)},
×
591
                IsFatal{m_server_endpoint.is_verified});
×
592
            ConnectionTerminationReason reason = ConnectionTerminationReason::http_response_says_fatal_error;
×
593
            // If the connection fails/times out and the server has not been contacted yet, refresh the location
594
            // to make sure the websocket URL is correct
595
            if (!m_server_endpoint.is_verified) {
×
596
                error_info.server_requests_action = ProtocolErrorInfo::Action::RefreshLocation;
×
597
                reason = ConnectionTerminationReason::connect_operation_failed;
×
598
            }
×
599
            involuntary_disconnect(std::move(error_info), reason);
×
600
            break;
×
601
        }
×
602
        case WebSocketError::websocket_forbidden: {
✔
603
            SessionErrorInfo error_info({ErrorCodes::AuthError, msg}, IsFatal{true});
×
604
            error_info.server_requests_action = ProtocolErrorInfo::Action::LogOutUser;
×
605
            involuntary_disconnect(std::move(error_info),
×
606
                                   ConnectionTerminationReason::http_response_says_fatal_error);
×
607
            break;
×
608
        }
×
609
        case WebSocketError::websocket_unauthorized: {
44✔
610
            SessionErrorInfo error_info(
44✔
611
                {ErrorCodes::AuthError,
44✔
612
                 util::format("Websocket was closed because of an authentication issue: %1", msg)},
44✔
613
                IsFatal{false});
44✔
614
            error_info.server_requests_action = ProtocolErrorInfo::Action::RefreshUser;
44✔
615
            involuntary_disconnect(std::move(error_info),
44✔
616
                                   ConnectionTerminationReason::http_response_says_nonfatal_error);
44✔
617
            break;
44✔
618
        }
×
619
        case WebSocketError::websocket_moved_permanently: {
12✔
620
            SessionErrorInfo error_info({ErrorCodes::ConnectionClosed, msg}, IsFatal{false});
12✔
621
            error_info.server_requests_action = ProtocolErrorInfo::Action::RefreshLocation;
12✔
622
            involuntary_disconnect(std::move(error_info),
12✔
623
                                   ConnectionTerminationReason::http_response_says_nonfatal_error);
12✔
624
            break;
12✔
625
        }
×
626
        case WebSocketError::websocket_abnormal_closure: {
✔
627
            SessionErrorInfo error_info({ErrorCodes::ConnectionClosed, msg}, IsFatal{false});
×
628
            error_info.server_requests_action = ProtocolErrorInfo::Action::RefreshUser;
×
629
            involuntary_disconnect(std::move(error_info),
×
630
                                   ConnectionTerminationReason::http_response_says_nonfatal_error);
×
631
            break;
×
632
        }
×
633
        case WebSocketError::websocket_internal_server_error:
✔
634
            [[fallthrough]];
×
635
        case WebSocketError::websocket_retry_error: {
✔
636
            involuntary_disconnect(SessionErrorInfo({ErrorCodes::ConnectionClosed, msg}, IsFatal{false}),
×
637
                                   ConnectionTerminationReason::http_response_says_nonfatal_error);
×
638
            break;
×
639
        }
×
640
    }
820✔
641

642
    return bool(m_websocket);
808✔
643
}
820✔
644

645
// Guarantees that handle_reconnect_wait() is never called from within the
646
// execution of initiate_reconnect_wait() (no callback reentrance).
647
void Connection::initiate_reconnect_wait()
648
{
8,496✔
649
    REALM_ASSERT(m_activated);
8,496✔
650
    REALM_ASSERT(!m_reconnect_delay_in_progress);
8,496✔
651
    REALM_ASSERT(!m_disconnect_delay_in_progress);
8,496✔
652

653
    // If we've been force closed then we don't need/want to reconnect. Just return early here.
654
    if (m_force_closed) {
8,496✔
655
        return;
2,396✔
656
    }
2,396✔
657

658
    m_reconnect_delay_in_progress = true;
6,100✔
659
    auto delay = m_reconnect_info.delay_interval();
6,100✔
660
    if (delay == std::chrono::milliseconds::max()) {
6,100✔
661
        logger.detail("Reconnection delayed indefinitely"); // Throws
1,030✔
662
        // Not actually starting a timer corresponds to an infinite wait
663
        m_nonzero_reconnect_delay = true;
1,030✔
664
        return;
1,030✔
665
    }
1,030✔
666

667
    if (delay == std::chrono::milliseconds::zero()) {
5,070✔
668
        m_nonzero_reconnect_delay = false;
4,734✔
669
    }
4,734✔
670
    else {
336✔
671
        logger.detail("Allowing reconnection in %1 milliseconds", delay.count()); // Throws
336✔
672
        m_nonzero_reconnect_delay = true;
336✔
673
    }
336✔
674

675
    // We create a timer for the reconnect_disconnect timer even if the delay is zero because
676
    // we need it to be cancelable in case the connection is terminated before the timer
677
    // callback is run.
678
    m_reconnect_disconnect_timer = m_client.create_timer(delay, [this](Status status) {
5,070✔
679
        // If the operation is aborted, the connection object may have been
680
        // destroyed.
681
        if (status != ErrorCodes::OperationAborted)
5,070✔
682
            handle_reconnect_wait(status); // Throws
3,766✔
683
    });                                    // Throws
5,070✔
684
}
5,070✔
685

686

687
void Connection::handle_reconnect_wait(Status status)
688
{
3,766✔
689
    if (!status.is_ok()) {
3,766✔
690
        REALM_ASSERT(status != ErrorCodes::OperationAborted);
×
691
        throw Exception(status);
×
692
    }
×
693

694
    REALM_ASSERT(m_reconnect_delay_in_progress);
3,766✔
695
    m_reconnect_delay_in_progress = false;
3,766✔
696

697
    if (m_num_active_unsuspended_sessions > 0)
3,766✔
698
        initiate_reconnect(); // Throws
3,760✔
699
}
3,766✔
700

701
struct Connection::WebSocketObserverShim : public sync::WebSocketObserver {
702
    explicit WebSocketObserverShim(Connection* conn)
703
        : conn(conn)
1,744✔
704
        , sentinel(conn->m_websocket_sentinel)
1,744✔
705
    {
3,766✔
706
    }
3,766✔
707

708
    Connection* conn;
709
    util::bind_ptr<LifecycleSentinel> sentinel;
710

711
    void websocket_connected_handler(const std::string& protocol) override
712
    {
3,558✔
713
        if (sentinel->destroyed) {
3,558✔
714
            return;
×
715
        }
×
716

717
        return conn->websocket_connected_handler(protocol);
3,558✔
718
    }
3,558✔
719

720
    void websocket_error_handler() override
721
    {
714✔
722
        if (sentinel->destroyed) {
714✔
723
            return;
×
724
        }
×
725

726
        conn->websocket_error_handler();
714✔
727
    }
714✔
728

729
    bool websocket_binary_message_received(util::Span<const char> data) override
730
    {
80,896✔
731
        if (sentinel->destroyed) {
80,896✔
732
            return false;
×
733
        }
×
734

735
        return conn->websocket_binary_message_received(data);
80,896✔
736
    }
80,896✔
737

738
    bool websocket_closed_handler(bool was_clean, WebSocketError error_code, std::string_view msg) override
739
    {
820✔
740
        if (sentinel->destroyed) {
820✔
741
            return true;
×
742
        }
×
743

744
        return conn->websocket_closed_handler(was_clean, error_code, msg);
820✔
745
    }
820✔
746
};
747

748
void Connection::initiate_reconnect()
749
{
3,766✔
750
    REALM_ASSERT(m_activated);
3,766✔
751

752
    m_state = ConnectionState::connecting;
3,766✔
753
    report_connection_state_change(ConnectionState::connecting); // Throws
3,766✔
754
    if (m_websocket_sentinel) {
3,766✔
755
        m_websocket_sentinel->destroyed = true;
×
756
    }
×
757
    m_websocket_sentinel = util::make_bind<LifecycleSentinel>();
3,766✔
758
    m_websocket.reset();
3,766✔
759

760
    // Watchdog
761
    initiate_connect_wait(); // Throws
3,766✔
762

763
    std::vector<std::string> sec_websocket_protocol;
3,766✔
764
    {
3,766✔
765
        auto protocol_prefix =
3,766✔
766
            is_flx_sync_connection() ? get_flx_websocket_protocol_prefix() : get_pbs_websocket_protocol_prefix();
3,766✔
767
        int min = get_oldest_supported_protocol_version();
3,766✔
768
        int max = get_current_protocol_version();
3,766✔
769
        REALM_ASSERT_3(min, <=, max);
3,766✔
770
        // List protocol version in descending order to ensure that the server
771
        // selects the highest possible version.
772
        for (int version = max; version >= min; --version) {
45,170✔
773
            sec_websocket_protocol.push_back(util::format("%1%2", protocol_prefix, version)); // Throws
41,404✔
774
        }
41,404✔
775
    }
3,766✔
776

777
    logger.info("Connecting to '%1%2:%3%4'", to_string(m_server_endpoint.envelope), m_server_endpoint.address,
3,766✔
778
                m_server_endpoint.port, m_http_request_path_prefix);
3,766✔
779

780
    m_websocket_error_received = false;
3,766✔
781
    m_websocket =
3,766✔
782
        m_client.m_socket_provider->connect(std::make_unique<WebSocketObserverShim>(this),
3,766✔
783
                                            WebSocketEndpoint{
3,766✔
784
                                                m_server_endpoint.address,
3,766✔
785
                                                m_server_endpoint.port,
3,766✔
786
                                                get_http_request_path(),
3,766✔
787
                                                std::move(sec_websocket_protocol),
3,766✔
788
                                                is_ssl(m_server_endpoint.envelope),
3,766✔
789
                                                /// DEPRECATED - The following will be removed in a future release
790
                                                {m_custom_http_headers.begin(), m_custom_http_headers.end()},
3,766✔
791
                                                m_verify_servers_ssl_certificate,
3,766✔
792
                                                m_ssl_trust_certificate_path,
3,766✔
793
                                                m_ssl_verify_callback,
3,766✔
794
                                                m_proxy_config,
3,766✔
795
                                            });
3,766✔
796
}
3,766✔
797

798

799
void Connection::initiate_connect_wait()
800
{
3,764✔
801
    // Deploy a watchdog to enforce an upper bound on the time it can take to
802
    // fully establish the connection (including SSL and WebSocket
803
    // handshakes). Without such a watchdog, connect operations could take very
804
    // long, or even indefinite time.
805
    milliseconds_type time = m_client.m_connect_timeout;
3,764✔
806

807
    m_connect_timer = m_client.create_timer(std::chrono::milliseconds(time), [this](Status status) {
3,764✔
808
        // If the operation is aborted, the connection object may have been
809
        // destroyed.
810
        if (status != ErrorCodes::OperationAborted)
3,754✔
811
            handle_connect_wait(status); // Throws
×
812
    });                                  // Throws
3,754✔
813
}
3,764✔
814

815

816
void Connection::handle_connect_wait(Status status)
817
{
×
818
    if (!status.is_ok()) {
×
819
        REALM_ASSERT(status != ErrorCodes::OperationAborted);
×
820
        throw Exception(status);
×
821
    }
×
822

823
    REALM_ASSERT_EX(m_state == ConnectionState::connecting, m_state);
×
824
    logger.info("Connect timeout"); // Throws
×
825
    SessionErrorInfo error_info({ErrorCodes::SyncConnectTimeout, "Sync connection was not fully established in time"},
×
826
                                IsFatal{false});
×
827
    // If the connection fails/times out and the server has not been contacted yet, refresh the location
828
    // to make sure the websocket URL is correct
829
    if (!m_server_endpoint.is_verified) {
×
830
        error_info.server_requests_action = ProtocolErrorInfo::Action::RefreshLocation;
×
831
    }
×
832
    involuntary_disconnect(std::move(error_info), ConnectionTerminationReason::sync_connect_timeout); // Throws
×
833
}
×
834

835

836
void Connection::handle_connection_established()
837
{
3,558✔
838
    // Cancel connect timeout watchdog
839
    m_connect_timer.reset();
3,558✔
840

841
    m_state = ConnectionState::connected;
3,558✔
842
    m_server_endpoint.is_verified = true; // sync route is valid since connection is successful
3,558✔
843

844
    milliseconds_type now = monotonic_clock_now();
3,558✔
845
    m_pong_wait_started_at = now; // Initially, no time was spent waiting for a PONG message
3,558✔
846
    initiate_ping_delay(now);     // Throws
3,558✔
847

848
    bool fast_reconnect = false;
3,558✔
849
    if (m_disconnect_has_occurred) {
3,558✔
850
        milliseconds_type time = now - m_disconnect_time;
1,052✔
851
        if (time <= m_client.m_fast_reconnect_limit)
1,052✔
852
            fast_reconnect = true;
1,052✔
853
    }
1,052✔
854

855
    for (auto& p : m_sessions) {
4,662✔
856
        Session& sess = *p.second;
4,662✔
857
        sess.connection_established(fast_reconnect); // Throws
4,662✔
858
    }
4,662✔
859

860
    report_connection_state_change(ConnectionState::connected); // Throws
3,558✔
861
}
3,558✔
862

863

864
void Connection::schedule_urgent_ping()
865
{
220✔
866
    REALM_ASSERT_EX(m_state != ConnectionState::disconnected, m_state);
220✔
867
    if (m_ping_delay_in_progress) {
220✔
868
        m_heartbeat_timer.reset();
118✔
869
        m_ping_delay_in_progress = false;
118✔
870
        m_minimize_next_ping_delay = true;
118✔
871
        milliseconds_type now = monotonic_clock_now();
118✔
872
        initiate_ping_delay(now); // Throws
118✔
873
        return;
118✔
874
    }
118✔
875
    REALM_ASSERT_EX(m_state == ConnectionState::connecting || m_waiting_for_pong, m_state);
102✔
876
    if (!m_send_ping)
102✔
877
        m_minimize_next_ping_delay = true;
102✔
878
}
102✔
879

880

881
void Connection::initiate_ping_delay(milliseconds_type now)
882
{
3,826✔
883
    REALM_ASSERT(!m_ping_delay_in_progress);
3,826✔
884
    REALM_ASSERT(!m_waiting_for_pong);
3,826✔
885
    REALM_ASSERT(!m_send_ping);
3,826✔
886

887
    milliseconds_type delay = 0;
3,826✔
888
    if (!m_minimize_next_ping_delay) {
3,826✔
889
        delay = m_client.m_ping_keepalive_period;
3,696✔
890
        // Make a randomized deduction of up to 10%, or up to 100% if this is
891
        // the first PING message to be sent since the connection was
892
        // established. The purpose of this randomized deduction is to reduce
893
        // the risk of many connections sending PING messages simultaneously to
894
        // the server.
895
        milliseconds_type max_deduction = (m_ping_sent ? delay / 10 : delay);
3,696✔
896
        auto distr = std::uniform_int_distribution<milliseconds_type>(0, max_deduction);
3,696✔
897
        milliseconds_type randomized_deduction = distr(m_client.get_random());
3,696✔
898
        delay -= randomized_deduction;
3,696✔
899
        // Deduct the time spent waiting for PONG
900
        REALM_ASSERT_3(now, >=, m_pong_wait_started_at);
3,696✔
901
        milliseconds_type spent_time = now - m_pong_wait_started_at;
3,696✔
902
        if (spent_time < delay) {
3,696✔
903
            delay -= spent_time;
3,688✔
904
        }
3,688✔
905
        else {
8✔
906
            delay = 0;
8✔
907
        }
8✔
908
    }
3,696✔
909
    else {
130✔
910
        m_minimize_next_ping_delay = false;
130✔
911
    }
130✔
912

913

914
    m_ping_delay_in_progress = true;
3,826✔
915

916
    m_heartbeat_timer = m_client.create_timer(std::chrono::milliseconds(delay), [this](Status status) {
3,826✔
917
        if (status == ErrorCodes::OperationAborted)
3,826✔
918
            return;
3,662✔
919
        else if (!status.is_ok())
164✔
920
            throw Exception(status);
×
921

922
        handle_ping_delay();                                    // Throws
164✔
923
    });                                                         // Throws
164✔
924
    logger.debug("Will emit a ping in %1 milliseconds", delay); // Throws
3,826✔
925
}
3,826✔
926

927

928
void Connection::handle_ping_delay()
929
{
164✔
930
    REALM_ASSERT(m_ping_delay_in_progress);
164✔
931
    m_ping_delay_in_progress = false;
164✔
932
    m_send_ping = true;
164✔
933

934
    initiate_pong_timeout(); // Throws
164✔
935

936
    if (m_state == ConnectionState::connected && !m_sending)
164✔
937
        send_next_message(); // Throws
122✔
938
}
164✔
939

940

941
void Connection::initiate_pong_timeout()
942
{
164✔
943
    REALM_ASSERT(!m_ping_delay_in_progress);
164✔
944
    REALM_ASSERT(!m_waiting_for_pong);
164✔
945
    REALM_ASSERT(m_send_ping);
164✔
946

947
    m_waiting_for_pong = true;
164✔
948
    m_pong_wait_started_at = monotonic_clock_now();
164✔
949

950
    milliseconds_type time = m_client.m_pong_keepalive_timeout;
164✔
951
    m_heartbeat_timer = m_client.create_timer(std::chrono::milliseconds(time), [this](Status status) {
164✔
952
        if (status == ErrorCodes::OperationAborted)
164✔
953
            return;
152✔
954
        else if (!status.is_ok())
12✔
955
            throw Exception(status);
×
956

957
        handle_pong_timeout(); // Throws
12✔
958
    });                        // Throws
12✔
959
}
164✔
960

961

962
void Connection::handle_pong_timeout()
963
{
12✔
964
    REALM_ASSERT(m_waiting_for_pong);
12✔
965
    logger.debug("Timeout on reception of PONG message"); // Throws
12✔
966
    close_due_to_transient_error({ErrorCodes::ConnectionClosed, "Timed out waiting for PONG response from server"},
12✔
967
                                 ConnectionTerminationReason::pong_timeout);
12✔
968
}
12✔
969

970

971
void Connection::initiate_write_message(const OutputBuffer& out, Session* sess)
972
{
101,954✔
973
    // Stop sending messages if an websocket error was received.
974
    if (m_websocket_error_received)
101,954✔
975
        return;
×
976

977
    m_websocket->async_write_binary(out.as_span(), [this, sentinel = m_websocket_sentinel](Status status) {
101,954✔
978
        if (sentinel->destroyed) {
101,856✔
979
            return;
1,446✔
980
        }
1,446✔
981
        if (!status.is_ok()) {
100,410✔
982
            if (status != ErrorCodes::Error::OperationAborted) {
×
983
                // Write errors will be handled by the websocket_write_error_handler() callback
984
                logger.error("Connection: write failed %1: %2", status.code_string(), status.reason());
×
985
            }
×
986
            return;
×
987
        }
×
988
        handle_write_message(); // Throws
100,410✔
989
    });                         // Throws
100,410✔
990
    m_sending_session = sess;
101,954✔
991
    m_sending = true;
101,954✔
992
}
101,954✔
993

994

995
void Connection::handle_write_message()
996
{
100,404✔
997
    m_sending_session->message_sent(); // Throws
100,404✔
998
    if (m_sending_session->m_state == Session::Deactivated) {
100,404✔
999
        finish_session_deactivation(m_sending_session);
128✔
1000
    }
128✔
1001
    m_sending_session = nullptr;
100,404✔
1002
    m_sending = false;
100,404✔
1003
    send_next_message(); // Throws
100,404✔
1004
}
100,404✔
1005

1006

1007
void Connection::send_next_message()
1008
{
161,090✔
1009
    REALM_ASSERT_EX(m_state == ConnectionState::connected, m_state);
161,090✔
1010
    REALM_ASSERT(!m_sending_session);
161,090✔
1011
    REALM_ASSERT(!m_sending);
161,090✔
1012
    if (m_send_ping) {
161,090✔
1013
        send_ping(); // Throws
152✔
1014
        return;
152✔
1015
    }
152✔
1016
    while (!m_sessions_enlisted_to_send.empty()) {
224,388✔
1017
        // The state of being connected is not supposed to be able to change
1018
        // across this loop thanks to the "no callback reentrance" guarantee
1019
        // provided by Websocket::async_write_text(), and friends.
1020
        REALM_ASSERT_EX(m_state == ConnectionState::connected, m_state);
165,672✔
1021

1022
        Session& sess = *m_sessions_enlisted_to_send.front();
165,672✔
1023
        m_sessions_enlisted_to_send.pop_front();
165,672✔
1024
        sess.send_message(); // Throws
165,672✔
1025

1026
        if (sess.m_state == Session::Deactivated) {
165,672✔
1027
            finish_session_deactivation(&sess);
2,740✔
1028
        }
2,740✔
1029

1030
        // An enlisted session may choose to not send a message. In that case,
1031
        // we should pass the opportunity to the next enlisted session.
1032
        if (m_sending)
165,672✔
1033
            break;
102,222✔
1034
    }
165,672✔
1035
}
160,938✔
1036

1037

1038
void Connection::send_ping()
1039
{
152✔
1040
    REALM_ASSERT(!m_ping_delay_in_progress);
152✔
1041
    REALM_ASSERT(m_waiting_for_pong);
152✔
1042
    REALM_ASSERT(m_send_ping);
152✔
1043

1044
    m_send_ping = false;
152✔
1045
    if (m_reconnect_info.scheduled_reset)
152✔
1046
        m_ping_after_scheduled_reset_of_reconnect_info = true;
118✔
1047

1048
    m_last_ping_sent_at = monotonic_clock_now();
152✔
1049
    logger.debug("Sending: PING(timestamp=%1, rtt=%2)", m_last_ping_sent_at,
152✔
1050
                 m_previous_ping_rtt); // Throws
152✔
1051

1052
    ClientProtocol& protocol = get_client_protocol();
152✔
1053
    OutputBuffer& out = get_output_buffer();
152✔
1054
    protocol.make_ping(out, m_last_ping_sent_at, m_previous_ping_rtt); // Throws
152✔
1055
    initiate_write_ping(out);                                          // Throws
152✔
1056
    m_ping_sent = true;
152✔
1057
}
152✔
1058

1059

1060
void Connection::initiate_write_ping(const OutputBuffer& out)
1061
{
152✔
1062
    m_websocket->async_write_binary(out.as_span(), [this, sentinel = m_websocket_sentinel](Status status) {
152✔
1063
        if (sentinel->destroyed) {
152✔
1064
            return;
×
1065
        }
×
1066
        if (!status.is_ok()) {
152✔
1067
            if (status != ErrorCodes::Error::OperationAborted) {
×
1068
                // Write errors will be handled by the websocket_write_error_handler() callback
1069
                logger.error("Connection: send ping failed %1: %2", status.code_string(), status.reason());
×
1070
            }
×
1071
            return;
×
1072
        }
×
1073
        handle_write_ping(); // Throws
152✔
1074
    });                      // Throws
152✔
1075
    m_sending = true;
152✔
1076
}
152✔
1077

1078

1079
void Connection::handle_write_ping()
1080
{
152✔
1081
    REALM_ASSERT(m_sending);
152✔
1082
    REALM_ASSERT(!m_sending_session);
152✔
1083
    m_sending = false;
152✔
1084
    send_next_message(); // Throws
152✔
1085
}
152✔
1086

1087

1088
void Connection::handle_message_received(util::Span<const char> data)
1089
{
80,436✔
1090
    // parse_message_received() parses the message and calls the proper handler
1091
    // on the Connection object (this).
1092
    get_client_protocol().parse_message_received<Connection>(*this, std::string_view(data.data(), data.size()));
80,436✔
1093
}
80,436✔
1094

1095

1096
void Connection::initiate_disconnect_wait()
1097
{
4,664✔
1098
    REALM_ASSERT(!m_reconnect_delay_in_progress);
4,664✔
1099

1100
    if (m_disconnect_delay_in_progress) {
4,664✔
1101
        m_reconnect_disconnect_timer.reset();
2,180✔
1102
        m_disconnect_delay_in_progress = false;
2,180✔
1103
    }
2,180✔
1104

1105
    milliseconds_type time = m_client.m_connection_linger_time;
4,664✔
1106

1107
    m_reconnect_disconnect_timer = m_client.create_timer(std::chrono::milliseconds(time), [this](Status status) {
4,664✔
1108
        // If the operation is aborted, the connection object may have been
1109
        // destroyed.
1110
        if (status != ErrorCodes::OperationAborted)
4,664✔
1111
            handle_disconnect_wait(status); // Throws
12✔
1112
    });                                     // Throws
4,664✔
1113
    m_disconnect_delay_in_progress = true;
4,664✔
1114
}
4,664✔
1115

1116

1117
void Connection::handle_disconnect_wait(Status status)
1118
{
12✔
1119
    if (!status.is_ok()) {
12✔
1120
        REALM_ASSERT(status != ErrorCodes::OperationAborted);
×
1121
        throw Exception(status);
×
1122
    }
×
1123

1124
    m_disconnect_delay_in_progress = false;
12✔
1125

1126
    REALM_ASSERT_EX(m_state != ConnectionState::disconnected, m_state);
12✔
1127
    if (m_num_active_unsuspended_sessions == 0) {
12✔
1128
        if (m_client.m_connection_linger_time > 0)
12✔
UNCOV
1129
            logger.detail("Linger time expired"); // Throws
×
1130
        voluntary_disconnect();                   // Throws
12✔
1131
        logger.info("Disconnected");              // Throws
12✔
1132
    }
12✔
1133
}
12✔
1134

1135

1136
void Connection::close_due_to_protocol_error(Status status)
1137
{
16✔
1138
    SessionErrorInfo error_info(std::move(status), IsFatal{true});
16✔
1139
    error_info.server_requests_action = ProtocolErrorInfo::Action::ProtocolViolation;
16✔
1140
    involuntary_disconnect(std::move(error_info),
16✔
1141
                           ConnectionTerminationReason::sync_protocol_violation); // Throws
16✔
1142
}
16✔
1143

1144

1145
void Connection::close_due_to_client_side_error(Status status, IsFatal is_fatal, ConnectionTerminationReason reason)
1146
{
470✔
1147
    logger.info("Connection closed due to error: %1", status); // Throws
470✔
1148

1149
    involuntary_disconnect(SessionErrorInfo{std::move(status), is_fatal}, reason); // Throw
470✔
1150
}
470✔
1151

1152

1153
void Connection::close_due_to_transient_error(Status status, ConnectionTerminationReason reason)
1154
{
592✔
1155
    logger.info("Connection closed due to transient error: %1", status); // Throws
592✔
1156
    SessionErrorInfo error_info{std::move(status), IsFatal{false}};
592✔
1157
    error_info.server_requests_action = ProtocolErrorInfo::Action::Transient;
592✔
1158

1159
    involuntary_disconnect(std::move(error_info), reason); // Throw
592✔
1160
}
592✔
1161

1162

1163
// Close connection due to error discovered on the server-side, and then
1164
// reported to the client by way of a connection-level ERROR message.
1165
void Connection::close_due_to_server_side_error(ProtocolError error_code, const ProtocolErrorInfo& info)
1166
{
70✔
1167
    logger.info("Connection closed due to error reported by server: %1 (%2)", info.message,
70✔
1168
                int(error_code)); // Throws
70✔
1169

1170
    const auto reason = info.is_fatal ? ConnectionTerminationReason::server_said_do_not_reconnect
70✔
1171
                                      : ConnectionTerminationReason::server_said_try_again_later;
70✔
1172
    involuntary_disconnect(SessionErrorInfo{info, protocol_error_to_status(error_code, info.message)},
70✔
1173
                           reason); // Throws
70✔
1174
}
70✔
1175

1176

1177
void Connection::disconnect(const SessionErrorInfo& info)
1178
{
3,766✔
1179
    // Cancel connect timeout watchdog
1180
    m_connect_timer.reset();
3,766✔
1181

1182
    if (m_state == ConnectionState::connected) {
3,766✔
1183
        m_disconnect_time = monotonic_clock_now();
3,552✔
1184
        m_disconnect_has_occurred = true;
3,552✔
1185

1186
        // Sessions that are in the Deactivating state at this time can be
1187
        // immediately discarded, in part because they are no longer enlisted to
1188
        // send. Such sessions will be taken to the Deactivated state by
1189
        // Session::connection_lost(), and then they will be removed from
1190
        // `m_sessions`.
1191
        auto i = m_sessions.begin(), end = m_sessions.end();
3,552✔
1192
        while (i != end) {
7,554✔
1193
            // Prevent invalidation of the main iterator when erasing elements
1194
            auto j = i++;
4,002✔
1195
            Session& sess = *j->second;
4,002✔
1196
            sess.connection_lost(); // Throws
4,002✔
1197
            if (sess.m_state == Session::Unactivated || sess.m_state == Session::Deactivated)
4,004✔
1198
                m_sessions.erase(j);
1,800✔
1199
        }
4,002✔
1200
    }
3,552✔
1201

1202
    change_state_to_disconnected();
3,766✔
1203

1204
    m_ping_delay_in_progress = false;
3,766✔
1205
    m_waiting_for_pong = false;
3,766✔
1206
    m_send_ping = false;
3,766✔
1207
    m_minimize_next_ping_delay = false;
3,766✔
1208
    m_ping_after_scheduled_reset_of_reconnect_info = false;
3,766✔
1209
    m_ping_sent = false;
3,766✔
1210
    m_heartbeat_timer.reset();
3,766✔
1211
    m_previous_ping_rtt = 0;
3,766✔
1212

1213
    m_websocket_sentinel->destroyed = true;
3,766✔
1214
    m_websocket_sentinel.reset();
3,766✔
1215
    m_websocket.reset();
3,766✔
1216
    m_input_body_buffer.reset();
3,766✔
1217
    m_sending_session = nullptr;
3,766✔
1218
    m_sessions_enlisted_to_send.clear();
3,766✔
1219
    m_sending = false;
3,766✔
1220

1221
    report_connection_state_change(ConnectionState::disconnected, info); // Throws
3,766✔
1222
    initiate_reconnect_wait();                                           // Throws
3,766✔
1223
}
3,766✔
1224

1225
bool Connection::is_flx_sync_connection() const noexcept
1226
{
113,916✔
1227
    return m_server_endpoint.server_mode != SyncServerMode::PBS;
113,916✔
1228
}
113,916✔
1229

1230
void Connection::receive_pong(milliseconds_type timestamp)
1231
{
150✔
1232
    logger.debug("Received: PONG(timestamp=%1)", timestamp);
150✔
1233

1234
    bool legal_at_this_time = (m_waiting_for_pong && !m_send_ping);
150✔
1235
    if (REALM_UNLIKELY(!legal_at_this_time)) {
150✔
1236
        close_due_to_protocol_error(
×
1237
            {ErrorCodes::SyncProtocolInvariantFailed, "Received PONG message when it was not valid"}); // Throws
×
1238
        return;
×
1239
    }
×
1240

1241
    if (REALM_UNLIKELY(timestamp != m_last_ping_sent_at)) {
150✔
1242
        close_due_to_protocol_error(
×
1243
            {ErrorCodes::SyncProtocolInvariantFailed,
×
1244
             util::format("Received PONG message with an invalid timestamp (expected %1, received %2)",
×
1245
                          m_last_ping_sent_at, timestamp)}); // Throws
×
1246
        return;
×
1247
    }
×
1248

1249
    milliseconds_type now = monotonic_clock_now();
150✔
1250
    milliseconds_type round_trip_time = now - timestamp;
150✔
1251
    logger.debug("Round trip time was %1 milliseconds", round_trip_time);
150✔
1252
    m_previous_ping_rtt = round_trip_time;
150✔
1253

1254
    // If this PONG message is a response to a PING mesage that was sent after
1255
    // the last invocation of cancel_reconnect_delay(), then the connection is
1256
    // still good, and we do not have to skip the next reconnect delay.
1257
    if (m_ping_after_scheduled_reset_of_reconnect_info) {
150✔
1258
        REALM_ASSERT(m_reconnect_info.scheduled_reset);
104✔
1259
        m_ping_after_scheduled_reset_of_reconnect_info = false;
104✔
1260
        m_reconnect_info.scheduled_reset = false;
104✔
1261
    }
104✔
1262

1263
    m_heartbeat_timer.reset();
150✔
1264
    m_waiting_for_pong = false;
150✔
1265

1266
    initiate_ping_delay(now); // Throws
150✔
1267

1268
    if (m_client.m_roundtrip_time_handler)
150✔
1269
        m_client.m_roundtrip_time_handler(m_previous_ping_rtt); // Throws
×
1270
}
150✔
1271

1272
Session* Connection::find_and_validate_session(session_ident_type session_ident, std::string_view message) noexcept
1273
{
74,164✔
1274
    if (session_ident == 0) {
74,164✔
1275
        return nullptr;
×
1276
    }
×
1277

1278
    auto* sess = get_session(session_ident);
74,164✔
1279
    if (REALM_LIKELY(sess)) {
74,164✔
1280
        return sess;
74,164✔
1281
    }
74,164✔
1282
    // Check the history to see if the message received was for a previous session
UNCOV
1283
    if (auto it = m_session_history.find(session_ident); it == m_session_history.end()) {
×
1284
        logger.error("Bad session identifier in %1 message, session_ident = %2", message, session_ident);
×
1285
        close_due_to_protocol_error(
×
1286
            {ErrorCodes::SyncProtocolInvariantFailed,
×
1287
             util::format("Received message %1 for session iden %2 when that session never existed", message,
×
1288
                          session_ident)});
×
1289
    }
×
UNCOV
1290
    else {
×
UNCOV
1291
        logger.error("Received %1 message for closed session, session_ident = %2", message,
×
UNCOV
1292
                     session_ident); // Throws
×
UNCOV
1293
    }
×
UNCOV
1294
    return nullptr;
×
1295
}
74,164✔
1296

1297
void Connection::receive_error_message(const ProtocolErrorInfo& info, session_ident_type session_ident)
1298
{
794✔
1299
    Session* sess = nullptr;
794✔
1300
    if (session_ident != 0) {
794✔
1301
        sess = find_and_validate_session(session_ident, "ERROR");
720✔
1302
        if (REALM_UNLIKELY(!sess)) {
720✔
1303
            return;
×
1304
        }
×
1305
        if (auto status = sess->receive_error_message(info); !status.is_ok()) {
720✔
1306
            close_due_to_protocol_error(std::move(status)); // Throws
×
1307
            return;
×
1308
        }
×
1309

1310
        if (sess->m_state == Session::Deactivated) {
720✔
1311
            finish_session_deactivation(sess);
2✔
1312
        }
2✔
1313
        return;
720✔
1314
    }
720✔
1315

1316
    logger.info("Received: ERROR \"%1\" (error_code=%2, is_fatal=%3, session_ident=%4, error_action=%5)",
74✔
1317
                info.message, info.raw_error_code, info.is_fatal, session_ident,
74✔
1318
                info.server_requests_action); // Throws
74✔
1319

1320
    bool known_error_code = bool(get_protocol_error_message(info.raw_error_code));
74✔
1321
    if (REALM_LIKELY(known_error_code)) {
74✔
1322
        ProtocolError error_code = ProtocolError(info.raw_error_code);
70✔
1323
        if (REALM_LIKELY(!is_session_level_error(error_code))) {
70✔
1324
            close_due_to_server_side_error(error_code, info); // Throws
70✔
1325
            return;
70✔
1326
        }
70✔
1327
        close_due_to_protocol_error(
×
1328
            {ErrorCodes::SyncProtocolInvariantFailed,
×
1329
             util::format("Received ERROR message with a non-connection-level error code %1 without a session ident",
×
1330
                          info.raw_error_code)});
×
1331
    }
×
1332
    else {
4✔
1333
        close_due_to_protocol_error(
4✔
1334
            {ErrorCodes::SyncProtocolInvariantFailed,
4✔
1335
             util::format("Received ERROR message with unknown error code %1", info.raw_error_code)});
4✔
1336
    }
4✔
1337
}
74✔
1338

1339

1340
void Connection::receive_query_error_message(int raw_error_code, std::string_view message, int64_t query_version,
1341
                                             session_ident_type session_ident)
1342
{
20✔
1343
    if (session_ident == 0) {
20✔
1344
        return close_due_to_protocol_error(
×
1345
            {ErrorCodes::SyncProtocolInvariantFailed, "Received query error message for session ident 0"});
×
1346
    }
×
1347

1348
    if (!is_flx_sync_connection()) {
20✔
1349
        return close_due_to_protocol_error({ErrorCodes::SyncProtocolInvariantFailed,
×
1350
                                            "Received a FLX query error message on a non-FLX sync connection"});
×
1351
    }
×
1352

1353
    Session* sess = find_and_validate_session(session_ident, "QUERY_ERROR");
20✔
1354
    if (REALM_UNLIKELY(!sess)) {
20✔
1355
        return;
×
1356
    }
×
1357

1358
    if (auto status = sess->receive_query_error_message(raw_error_code, message, query_version); !status.is_ok()) {
20✔
1359
        close_due_to_protocol_error(std::move(status));
×
1360
    }
×
1361
}
20✔
1362

1363

1364
void Connection::receive_ident_message(session_ident_type session_ident, SaltedFileIdent client_file_ident)
1365
{
3,724✔
1366
    Session* sess = find_and_validate_session(session_ident, "IDENT");
3,724✔
1367
    if (REALM_UNLIKELY(!sess)) {
3,724✔
1368
        return;
×
1369
    }
×
1370

1371
    if (auto status = sess->receive_ident_message(client_file_ident); !status.is_ok())
3,724✔
1372
        close_due_to_protocol_error(std::move(status)); // Throws
×
1373
}
3,724✔
1374

1375
void Connection::receive_download_message(session_ident_type session_ident, const DownloadMessage& message)
1376
{
48,376✔
1377
    Session* sess = find_and_validate_session(session_ident, "DOWNLOAD");
48,376✔
1378
    if (REALM_UNLIKELY(!sess)) {
48,376✔
1379
        return;
×
1380
    }
×
1381

1382
    if (auto status = sess->receive_download_message(message); !status.is_ok()) {
48,376✔
1383
        close_due_to_protocol_error(std::move(status));
2✔
1384
    }
2✔
1385
}
48,376✔
1386

1387
void Connection::receive_mark_message(session_ident_type session_ident, request_ident_type request_ident)
1388
{
16,654✔
1389
    Session* sess = find_and_validate_session(session_ident, "MARK");
16,654✔
1390
    if (REALM_UNLIKELY(!sess)) {
16,654✔
1391
        return;
×
1392
    }
×
1393

1394
    if (auto status = sess->receive_mark_message(request_ident); !status.is_ok())
16,654✔
1395
        close_due_to_protocol_error(std::move(status)); // Throws
10✔
1396
}
16,654✔
1397

1398

1399
void Connection::receive_unbound_message(session_ident_type session_ident)
1400
{
4,618✔
1401
    Session* sess = find_and_validate_session(session_ident, "UNBOUND");
4,618✔
1402
    if (REALM_UNLIKELY(!sess)) {
4,618✔
1403
        return;
×
1404
    }
×
1405

1406
    if (auto status = sess->receive_unbound_message(); !status.is_ok()) {
4,618✔
1407
        close_due_to_protocol_error(std::move(status)); // Throws
×
1408
        return;
×
1409
    }
×
1410

1411
    if (sess->m_state == Session::Deactivated) {
4,618✔
1412
        finish_session_deactivation(sess);
4,618✔
1413
    }
4,618✔
1414
}
4,618✔
1415

1416

1417
void Connection::receive_test_command_response(session_ident_type session_ident, request_ident_type request_ident,
1418
                                               std::string_view body)
1419
{
52✔
1420
    Session* sess = find_and_validate_session(session_ident, "TEST_COMMAND");
52✔
1421
    if (REALM_UNLIKELY(!sess)) {
52✔
1422
        return;
×
1423
    }
×
1424

1425
    if (auto status = sess->receive_test_command_response(request_ident, body); !status.is_ok()) {
52✔
1426
        close_due_to_protocol_error(std::move(status));
×
1427
    }
×
1428
}
52✔
1429

1430

1431
void Connection::receive_server_log_message(session_ident_type session_ident, util::Logger::Level level,
1432
                                            std::string_view message)
1433
{
6,048✔
1434
    std::string prefix;
6,048✔
1435
    if (REALM_LIKELY(!m_appservices_coid.empty())) {
6,048✔
1436
        prefix = util::format("Server[%1]", m_appservices_coid);
6,048✔
1437
    }
6,048✔
1438
    else {
×
1439
        prefix = "Server";
×
1440
    }
×
1441

1442
    if (session_ident != 0) {
6,048✔
1443
        if (auto sess = get_session(session_ident)) {
4,036✔
1444
            sess->logger.log(LogCategory::session, level, "%1 log: %2", prefix, message);
4,022✔
1445
            return;
4,022✔
1446
        }
4,022✔
1447

1448
        logger.log(util::LogCategory::session, level, "%1 log for unknown session %2: %3", prefix, session_ident,
14✔
1449
                   message);
14✔
1450
        return;
14✔
1451
    }
4,036✔
1452

1453
    logger.log(level, "%1 log: %2", prefix, message);
2,012✔
1454
}
2,012✔
1455

1456

1457
void Connection::receive_appservices_request_id(std::string_view coid)
1458
{
5,570✔
1459
    // Only set once per connection
1460
    if (!coid.empty() && m_appservices_coid.empty()) {
5,570✔
1461
        m_appservices_coid = coid;
2,490✔
1462
        logger.log(util::LogCategory::session, util::LogCategory::Level::info,
2,490✔
1463
                   "Connected to app services with request id: \"%1\"", m_appservices_coid);
2,490✔
1464
    }
2,490✔
1465
}
5,570✔
1466

1467

1468
void Connection::handle_protocol_error(Status status)
1469
{
×
1470
    close_due_to_protocol_error(std::move(status));
×
1471
}
×
1472

1473

1474
// Sessions are guaranteed to be granted the opportunity to send a message in
1475
// the order that they enlist. Note that this is important to ensure
1476
// nonoverlapping communication with the server for consecutive sessions
1477
// associated with the same Realm file.
1478
//
1479
// CAUTION: The specified session may get destroyed before this function
1480
// returns, but only if its Session::send_message() puts it into the Deactivated
1481
// state.
1482
void Connection::enlist_to_send(Session* sess)
1483
{
167,310✔
1484
    REALM_ASSERT_EX(m_state == ConnectionState::connected, m_state);
167,310✔
1485
    m_sessions_enlisted_to_send.push_back(sess); // Throws
167,310✔
1486
    if (!m_sending)
167,310✔
1487
        send_next_message(); // Throws
60,410✔
1488
}
167,310✔
1489

1490

1491
std::string Connection::get_active_appservices_connection_id()
1492
{
72✔
1493
    return m_appservices_coid;
72✔
1494
}
72✔
1495

1496
void Session::cancel_resumption_delay()
1497
{
4,202✔
1498
    REALM_ASSERT_EX(m_state == Active, m_state);
4,202✔
1499

1500
    if (!m_suspended)
4,202✔
1501
        return;
4,112✔
1502

1503
    m_suspended = false;
90✔
1504

1505
    logger.debug("Resumed"); // Throws
90✔
1506

1507
    if (unbind_process_complete())
90✔
1508
        initiate_rebind(); // Throws
64✔
1509

1510
    m_conn.one_more_active_unsuspended_session(); // Throws
90✔
1511
    if (m_try_again_activation_timer) {
90✔
1512
        m_try_again_activation_timer.reset();
8✔
1513
    }
8✔
1514

1515
    on_resumed(); // Throws
90✔
1516
}
90✔
1517

1518

1519
void Session::gather_pending_compensating_writes(util::Span<Changeset> changesets,
1520
                                                 std::vector<ProtocolErrorInfo>* out)
1521
{
23,768✔
1522
    if (m_pending_compensating_write_errors.empty() || changesets.empty()) {
23,768✔
1523
        return;
23,722✔
1524
    }
23,722✔
1525

1526
#ifdef REALM_DEBUG
46✔
1527
    REALM_ASSERT_DEBUG(
46✔
1528
        std::is_sorted(m_pending_compensating_write_errors.begin(), m_pending_compensating_write_errors.end(),
46✔
1529
                       [](const ProtocolErrorInfo& lhs, const ProtocolErrorInfo& rhs) {
46✔
1530
                           REALM_ASSERT_DEBUG(lhs.compensating_write_server_version.has_value());
46✔
1531
                           REALM_ASSERT_DEBUG(rhs.compensating_write_server_version.has_value());
46✔
1532
                           return *lhs.compensating_write_server_version < *rhs.compensating_write_server_version;
46✔
1533
                       }));
46✔
1534
#endif
46✔
1535

1536
    while (!m_pending_compensating_write_errors.empty() &&
90✔
1537
           *m_pending_compensating_write_errors.front().compensating_write_server_version <=
90✔
1538
               changesets.back().version) {
44✔
1539
        auto& cur_error = m_pending_compensating_write_errors.front();
44✔
1540
        REALM_ASSERT_3(*cur_error.compensating_write_server_version, >=, changesets.front().version);
44✔
1541
        out->push_back(std::move(cur_error));
44✔
1542
        m_pending_compensating_write_errors.pop_front();
44✔
1543
    }
44✔
1544
}
46✔
1545

1546

1547
void Session::integrate_changesets(const SyncProgress& progress, std::uint_fast64_t downloadable_bytes,
1548
                                   const ReceivedChangesets& received_changesets, VersionInfo& version_info,
1549
                                   DownloadBatchState download_batch_state)
1550
{
45,538✔
1551
    auto& history = get_history();
45,538✔
1552
    if (received_changesets.empty()) {
45,538✔
1553
        if (download_batch_state == DownloadBatchState::MoreToCome) {
21,746✔
1554
            throw IntegrationException(ErrorCodes::SyncProtocolInvariantFailed,
×
1555
                                       "received empty download message that was not the last in batch",
×
1556
                                       ProtocolError::bad_progress);
×
1557
        }
×
1558
        history.set_sync_progress(progress, &downloadable_bytes, version_info); // Throws
21,746✔
1559
        return;
21,746✔
1560
    }
21,746✔
1561

1562
    std::vector<ProtocolErrorInfo> pending_compensating_write_errors;
23,792✔
1563
    auto transact = get_db()->start_read();
23,792✔
1564
    history.integrate_server_changesets(
23,792✔
1565
        progress, &downloadable_bytes, received_changesets, version_info, download_batch_state, logger, transact,
23,792✔
1566
        [&](const TransactionRef&, util::Span<Changeset> changesets) {
23,792✔
1567
            gather_pending_compensating_writes(changesets, &pending_compensating_write_errors);
23,768✔
1568
        }); // Throws
23,768✔
1569
    if (received_changesets.size() == 1) {
23,792✔
1570
        logger.debug("1 remote changeset integrated, producing client version %1",
15,574✔
1571
                     version_info.sync_version.version); // Throws
15,574✔
1572
    }
15,574✔
1573
    else {
8,218✔
1574
        logger.debug("%2 remote changesets integrated, producing client version %1",
8,218✔
1575
                     version_info.sync_version.version, received_changesets.size()); // Throws
8,218✔
1576
    }
8,218✔
1577

1578
    for (const auto& pending_error : pending_compensating_write_errors) {
23,792✔
1579
        logger.info("Reporting compensating write for client version %1 in server version %2: %3",
44✔
1580
                    pending_error.compensating_write_rejected_client_version,
44✔
1581
                    *pending_error.compensating_write_server_version, pending_error.message);
44✔
1582
        try {
44✔
1583
            on_connection_state_changed(
44✔
1584
                m_conn.get_state(),
44✔
1585
                SessionErrorInfo{pending_error,
44✔
1586
                                 protocol_error_to_status(static_cast<ProtocolError>(pending_error.raw_error_code),
44✔
1587
                                                          pending_error.message)});
44✔
1588
        }
44✔
1589
        catch (...) {
44✔
1590
            logger.error("Exception thrown while reporting compensating write: %1", exception_to_status());
×
1591
        }
×
1592
    }
44✔
1593
}
23,792✔
1594

1595

1596
void Session::on_integration_failure(const IntegrationException& error)
1597
{
40✔
1598
    REALM_ASSERT_EX(m_state == Active, m_state);
40✔
1599
    REALM_ASSERT(!m_client_error && !m_error_to_send);
40✔
1600
    logger.error("Failed to integrate downloaded changesets: %1", error.to_status());
40✔
1601

1602
    m_client_error = util::make_optional<IntegrationException>(error);
40✔
1603
    m_error_to_send = true;
40✔
1604
    SessionErrorInfo error_info{error.to_status(), IsFatal{false}};
40✔
1605
    error_info.server_requests_action = ProtocolErrorInfo::Action::Warning;
40✔
1606
    // Surface the error to the user otherwise is lost.
1607
    on_connection_state_changed(m_conn.get_state(), std::move(error_info));
40✔
1608

1609
    // Since the deactivation process has not been initiated, the UNBIND
1610
    // message cannot have been sent unless an ERROR message was received.
1611
    REALM_ASSERT(m_suspended || m_error_message_received || !m_unbind_message_sent);
40✔
1612
    if (m_ident_message_sent && !m_error_message_received && !m_suspended) {
40✔
1613
        ensure_enlisted_to_send(); // Throws
36✔
1614
    }
36✔
1615
}
40✔
1616

1617
void Session::on_changesets_integrated(version_type client_version, const SyncProgress& progress,
1618
                                       bool changesets_integrated)
1619
{
47,474✔
1620
    REALM_ASSERT_EX(m_state == Active, m_state);
47,474✔
1621
    REALM_ASSERT_3(progress.download.server_version, >=, m_download_progress.server_version);
47,474✔
1622
    bool upload_progressed = (progress.upload.client_version > m_progress.upload.client_version);
47,474✔
1623

1624
    m_download_progress = progress.download;
47,474✔
1625
    m_progress = progress;
47,474✔
1626

1627
    if (upload_progressed) {
47,474✔
1628
        if (progress.upload.client_version > m_last_version_selected_for_upload) {
34,524✔
1629
            if (progress.upload.client_version > m_upload_progress.client_version)
14,204✔
1630
                m_upload_progress = progress.upload;
910✔
1631
            m_last_version_selected_for_upload = progress.upload.client_version;
14,204✔
1632
        }
14,204✔
1633

1634
        notify_upload_progress();
34,524✔
1635
        check_for_upload_completion();
34,524✔
1636
    }
34,524✔
1637

1638
    bool resume_upload = do_recognize_sync_version(client_version); // Allows upload process to resume
47,474✔
1639

1640
    // notify also when final DOWNLOAD received with no changesets
1641
    bool download_progressed = changesets_integrated || (!upload_progressed && resume_upload);
47,474✔
1642
    if (download_progressed)
47,474✔
1643
        notify_download_progress();
27,656✔
1644

1645
    check_for_download_completion(); // Throws
47,474✔
1646

1647
    // If the client migrated from PBS to FLX, create subscriptions when new tables are received from server.
1648
    if (auto migration_store = get_migration_store(); migration_store && m_is_flx_sync_session) {
47,474✔
1649
        auto& flx_subscription_store = *get_flx_subscription_store();
3,370✔
1650
        get_migration_store()->create_subscriptions(flx_subscription_store);
3,370✔
1651
    }
3,370✔
1652

1653
    // Since the deactivation process has not been initiated, the UNBIND
1654
    // message cannot have been sent unless an ERROR message was received.
1655
    REALM_ASSERT(m_suspended || m_error_message_received || !m_unbind_message_sent);
47,474✔
1656
    if (m_ident_message_sent && !m_error_message_received && !m_suspended) {
47,474✔
1657
        ensure_enlisted_to_send(); // Throws
47,466✔
1658
    }
47,466✔
1659
}
47,474✔
1660

1661

1662
Session::~Session()
1663
{
10,346✔
1664
    //    REALM_ASSERT_EX(m_state == Unactivated || m_state == Deactivated, m_state);
1665
}
10,346✔
1666

1667

1668
std::string Session::make_logger_prefix(session_ident_type ident)
1669
{
10,360✔
1670
    std::ostringstream out;
10,360✔
1671
    out.imbue(std::locale::classic());
10,360✔
1672
    out << "Session[" << ident << "]: "; // Throws
10,360✔
1673
    return out.str();                    // Throws
10,360✔
1674
}
10,360✔
1675

1676

1677
void Session::activate()
1678
{
10,358✔
1679
    REALM_ASSERT_EX(m_state == Unactivated, m_state);
10,358✔
1680

1681
    logger.debug("Activating"); // Throws
10,358✔
1682

1683
    bool has_pending_client_reset = false;
10,358✔
1684
    if (REALM_LIKELY(!get_client().is_dry_run())) {
10,360✔
1685
        bool file_exists = util::File::exists(get_realm_path());
10,360✔
1686
        m_performing_client_reset = get_client_reset_config().has_value();
10,360✔
1687

1688
        logger.info("client_reset_config = %1, Realm exists = %2 ", m_performing_client_reset, file_exists);
10,360✔
1689
        if (!m_performing_client_reset) {
10,360✔
1690
            get_history().get_status(m_last_version_available, m_client_file_ident, m_progress,
9,992✔
1691
                                     &has_pending_client_reset); // Throws
9,992✔
1692
        }
9,992✔
1693
    }
10,360✔
1694
    logger.debug("client_file_ident = %1, client_file_ident_salt = %2", m_client_file_ident.ident,
10,358✔
1695
                 m_client_file_ident.salt); // Throws
10,358✔
1696
    m_upload_progress = m_progress.upload;
10,358✔
1697
    m_last_version_selected_for_upload = m_upload_progress.client_version;
10,358✔
1698
    m_download_progress = m_progress.download;
10,358✔
1699
    REALM_ASSERT_3(m_last_version_available, >=, m_progress.upload.client_version);
10,358✔
1700
    init_progress_handler();
10,358✔
1701

1702
    logger.debug("last_version_available  = %1", m_last_version_available);                    // Throws
10,358✔
1703
    logger.debug("progress_download_server_version = %1", m_progress.download.server_version); // Throws
10,358✔
1704
    logger.debug("progress_download_client_version = %1",
10,358✔
1705
                 m_progress.download.last_integrated_client_version);                                      // Throws
10,358✔
1706
    logger.debug("progress_upload_server_version = %1", m_progress.upload.last_integrated_server_version); // Throws
10,358✔
1707
    logger.debug("progress_upload_client_version = %1", m_progress.upload.client_version);                 // Throws
10,358✔
1708

1709
    reset_protocol_state();
10,358✔
1710
    m_state = Active;
10,358✔
1711

1712
    call_debug_hook(SyncClientHookEvent::SessionActivating, m_progress, m_last_sent_flx_query_version,
10,358✔
1713
                    DownloadBatchState::SteadyState, 0);
10,358✔
1714

1715
    REALM_ASSERT(!m_suspended);
10,358✔
1716
    m_conn.one_more_active_unsuspended_session(); // Throws
10,358✔
1717

1718
    try {
10,358✔
1719
        process_pending_flx_bootstrap();
10,358✔
1720
    }
10,358✔
1721
    catch (const IntegrationException& error) {
10,358✔
1722
        on_integration_failure(error);
×
1723
    }
×
1724
    catch (...) {
10,358✔
1725
        on_integration_failure(IntegrationException(exception_to_status()));
4✔
1726
    }
4✔
1727

1728
    if (has_pending_client_reset) {
10,358✔
1729
        handle_pending_client_reset_acknowledgement();
18✔
1730
    }
18✔
1731
}
10,358✔
1732

1733

1734
// The caller (Connection) must discard the session if the session has become
1735
// deactivated upon return.
1736
void Session::initiate_deactivation()
1737
{
10,348✔
1738
    REALM_ASSERT_EX(m_state == Active, m_state);
10,348✔
1739

1740
    logger.debug("Initiating deactivation"); // Throws
10,348✔
1741

1742
    m_state = Deactivating;
10,348✔
1743

1744
    if (!m_suspended)
10,348✔
1745
        m_conn.one_less_active_unsuspended_session(); // Throws
9,748✔
1746

1747
    if (m_enlisted_to_send) {
10,348✔
1748
        REALM_ASSERT(!unbind_process_complete());
5,264✔
1749
        return;
5,264✔
1750
    }
5,264✔
1751

1752
    // Deactivate immediately if the BIND message has not yet been sent and the
1753
    // session is not enlisted to send, or if the unbinding process has already
1754
    // completed.
1755
    if (!m_bind_message_sent || unbind_process_complete()) {
5,084✔
1756
        complete_deactivation(); // Throws
1,060✔
1757
        // Life cycle state is now Deactivated
1758
        return;
1,060✔
1759
    }
1,060✔
1760

1761
    // Ready to send the UNBIND message, if it has not already been sent
1762
    if (!m_unbind_message_sent) {
4,024✔
1763
        enlist_to_send(); // Throws
3,818✔
1764
        return;
3,818✔
1765
    }
3,818✔
1766
}
4,024✔
1767

1768

1769
void Session::complete_deactivation()
1770
{
10,348✔
1771
    REALM_ASSERT_EX(m_state == Deactivating, m_state);
10,348✔
1772
    m_state = Deactivated;
10,348✔
1773

1774
    logger.debug("Deactivation completed"); // Throws
10,348✔
1775
}
10,348✔
1776

1777

1778
// Called by the associated Connection object when this session is granted an
1779
// opportunity to send a message.
1780
//
1781
// The caller (Connection) must discard the session if the session has become
1782
// deactivated upon return.
1783
void Session::send_message()
1784
{
165,672✔
1785
    REALM_ASSERT_EX(m_state == Active || m_state == Deactivating, m_state);
165,672✔
1786
    REALM_ASSERT(m_enlisted_to_send);
165,672✔
1787
    m_enlisted_to_send = false;
165,672✔
1788
    if (m_state == Deactivating || m_error_message_received || m_suspended) {
165,672✔
1789
        // Deactivation has been initiated. If the UNBIND message has not been
1790
        // sent yet, there is no point in sending it. Instead, we can let the
1791
        // deactivation process complete.
1792
        if (!m_bind_message_sent) {
9,350✔
1793
            return complete_deactivation(); // Throws
2,740✔
1794
            // Life cycle state is now Deactivated
1795
        }
2,740✔
1796

1797
        // Session life cycle state is Deactivating or the unbinding process has
1798
        // been initiated by a session specific ERROR message
1799
        if (!m_unbind_message_sent)
6,610✔
1800
            send_unbind_message(); // Throws
6,610✔
1801
        return;
6,610✔
1802
    }
9,350✔
1803

1804
    // Session life cycle state is Active and the unbinding process has
1805
    // not been initiated
1806
    REALM_ASSERT(!m_unbind_message_sent);
156,322✔
1807

1808
    if (!m_bind_message_sent)
156,322✔
1809
        return send_bind_message(); // Throws
9,132✔
1810

1811
    if (!m_ident_message_sent) {
147,190✔
1812
        if (have_client_file_ident())
7,534✔
1813
            send_ident_message(); // Throws
7,534✔
1814
        return;
7,534✔
1815
    }
7,534✔
1816

1817
    const auto has_pending_test_command = std::any_of(m_pending_test_commands.begin(), m_pending_test_commands.end(),
139,656✔
1818
                                                      [](const PendingTestCommand& command) {
139,656✔
1819
                                                          return command.pending;
116✔
1820
                                                      });
116✔
1821
    if (has_pending_test_command) {
139,656✔
1822
        return send_test_command_message();
52✔
1823
    }
52✔
1824

1825
    if (m_error_to_send)
139,604✔
1826
        return send_json_error_message(); // Throws
32✔
1827

1828
    // Stop sending upload, mark and query messages when the client detects an error.
1829
    if (m_client_error) {
139,572✔
1830
        return;
12✔
1831
    }
12✔
1832

1833
    if (m_target_download_mark > m_last_download_mark_sent)
139,560✔
1834
        return send_mark_message(); // Throws
17,434✔
1835

1836
    auto is_upload_allowed = [&]() -> bool {
122,134✔
1837
        if (!m_is_flx_sync_session) {
122,134✔
1838
            return true;
106,260✔
1839
        }
106,260✔
1840

1841
        auto migration_store = get_migration_store();
15,874✔
1842
        if (!migration_store) {
15,874✔
1843
            return true;
×
1844
        }
×
1845

1846
        auto sentinel_query_version = migration_store->get_sentinel_subscription_set_version();
15,874✔
1847
        if (!sentinel_query_version) {
15,874✔
1848
            return true;
15,848✔
1849
        }
15,848✔
1850

1851
        // Do not allow upload if the last query sent is the sentinel one used by the migration store.
1852
        return m_last_sent_flx_query_version != *sentinel_query_version;
26✔
1853
    };
15,874✔
1854

1855
    if (!is_upload_allowed()) {
122,126✔
1856
        return;
16✔
1857
    }
16✔
1858

1859
    auto check_pending_flx_version = [&]() -> bool {
122,120✔
1860
        if (!m_is_flx_sync_session) {
122,120✔
1861
            return false;
106,260✔
1862
        }
106,260✔
1863

1864
        if (!m_allow_upload) {
15,860✔
1865
            return false;
3,218✔
1866
        }
3,218✔
1867

1868
        m_pending_flx_sub_set = get_flx_subscription_store()->get_next_pending_version(m_last_sent_flx_query_version);
12,642✔
1869

1870
        if (!m_pending_flx_sub_set) {
12,642✔
1871
            return false;
10,600✔
1872
        }
10,600✔
1873

1874
        return m_upload_progress.client_version >= m_pending_flx_sub_set->snapshot_version;
2,042✔
1875
    };
12,642✔
1876

1877
    if (check_pending_flx_version()) {
122,110✔
1878
        return send_query_change_message(); // throws
1,168✔
1879
    }
1,168✔
1880

1881
    if (m_allow_upload && (m_last_version_available > m_upload_progress.client_version)) {
120,942✔
1882
        return send_upload_message(); // Throws
60,260✔
1883
    }
60,260✔
1884
}
120,942✔
1885

1886

1887
void Session::send_bind_message()
1888
{
9,132✔
1889
    REALM_ASSERT_EX(m_state == Active, m_state);
9,132✔
1890

1891
    session_ident_type session_ident = m_ident;
9,132✔
1892
    bool need_client_file_ident = !have_client_file_ident();
9,132✔
1893
    const bool is_subserver = false;
9,132✔
1894
    m_last_download_batch_state = DownloadBatchState::SteadyState;
9,132✔
1895

1896
    ClientProtocol& protocol = m_conn.get_client_protocol();
9,132✔
1897
    int protocol_version = m_conn.get_negotiated_protocol_version();
9,132✔
1898
    OutputBuffer& out = m_conn.get_output_buffer();
9,132✔
1899
    // Discard the token since it's ignored by the server.
1900
    std::string empty_access_token;
9,132✔
1901
    if (m_is_flx_sync_session) {
9,132✔
1902
        nlohmann::json bind_json_data;
1,528✔
1903
        if (auto migrated_partition = get_migration_store()->get_migrated_partition()) {
1,528✔
1904
            bind_json_data["migratedPartition"] = *migrated_partition;
60✔
1905
        }
60✔
1906
        bind_json_data["sessionReason"] = static_cast<uint64_t>(get_session_reason());
1,528✔
1907
        auto schema_version = get_schema_version();
1,528✔
1908
        // Send 0 if schema is not versioned.
1909
        bind_json_data["schemaVersion"] = schema_version != uint64_t(-1) ? schema_version : 0;
1,528✔
1910
        if (logger.would_log(util::Logger::Level::debug)) {
1,528✔
1911
            std::string json_data_dump;
1,528✔
1912
            if (!bind_json_data.empty()) {
1,528✔
1913
                json_data_dump = bind_json_data.dump();
1,528✔
1914
            }
1,528✔
1915
            logger.debug(
1,528✔
1916
                "Sending: BIND(session_ident=%1, need_client_file_ident=%2, is_subserver=%3, json_data=\"%4\")",
1,528✔
1917
                session_ident, need_client_file_ident, is_subserver, json_data_dump);
1,528✔
1918
        }
1,528✔
1919
        protocol.make_flx_bind_message(protocol_version, out, session_ident, bind_json_data, empty_access_token,
1,528✔
1920
                                       need_client_file_ident, is_subserver); // Throws
1,528✔
1921
    }
1,528✔
1922
    else {
7,604✔
1923
        std::string server_path = get_virt_path();
7,604✔
1924
        logger.debug("Sending: BIND(session_ident=%1, need_client_file_ident=%2, is_subserver=%3, server_path=%4)",
7,604✔
1925
                     session_ident, need_client_file_ident, is_subserver, server_path);
7,604✔
1926
        protocol.make_pbs_bind_message(protocol_version, out, session_ident, server_path, empty_access_token,
7,604✔
1927
                                       need_client_file_ident, is_subserver); // Throws
7,604✔
1928
    }
7,604✔
1929
    m_conn.initiate_write_message(out, this); // Throws
9,132✔
1930

1931
    m_bind_message_sent = true;
9,132✔
1932
    call_debug_hook(SyncClientHookEvent::BindMessageSent, m_progress, m_last_sent_flx_query_version,
9,132✔
1933
                    DownloadBatchState::SteadyState, 0);
9,132✔
1934

1935
    // Ready to send the IDENT message if the file identifier pair is already
1936
    // available.
1937
    if (!need_client_file_ident)
9,132✔
1938
        enlist_to_send(); // Throws
3,992✔
1939
}
9,132✔
1940

1941

1942
void Session::send_ident_message()
1943
{
7,534✔
1944
    REALM_ASSERT_EX(m_state == Active, m_state);
7,534✔
1945
    REALM_ASSERT(m_bind_message_sent);
7,534✔
1946
    REALM_ASSERT(!m_unbind_message_sent);
7,534✔
1947
    REALM_ASSERT(have_client_file_ident());
7,534✔
1948

1949

1950
    ClientProtocol& protocol = m_conn.get_client_protocol();
7,534✔
1951
    OutputBuffer& out = m_conn.get_output_buffer();
7,534✔
1952
    session_ident_type session_ident = m_ident;
7,534✔
1953

1954
    if (m_is_flx_sync_session) {
7,534✔
1955
        const auto active_query_set = get_flx_subscription_store()->get_active();
1,456✔
1956
        const auto active_query_body = active_query_set.to_ext_json();
1,456✔
1957
        logger.debug("Sending: IDENT(client_file_ident=%1, client_file_ident_salt=%2, "
1,456✔
1958
                     "scan_server_version=%3, scan_client_version=%4, latest_server_version=%5, "
1,456✔
1959
                     "latest_server_version_salt=%6, query_version=%7, query_size=%8, query=\"%9\")",
1,456✔
1960
                     m_client_file_ident.ident, m_client_file_ident.salt, m_progress.download.server_version,
1,456✔
1961
                     m_progress.download.last_integrated_client_version, m_progress.latest_server_version.version,
1,456✔
1962
                     m_progress.latest_server_version.salt, active_query_set.version(), active_query_body.size(),
1,456✔
1963
                     active_query_body); // Throws
1,456✔
1964
        protocol.make_flx_ident_message(out, session_ident, m_client_file_ident, m_progress,
1,456✔
1965
                                        active_query_set.version(), active_query_body); // Throws
1,456✔
1966
        m_last_sent_flx_query_version = active_query_set.version();
1,456✔
1967
    }
1,456✔
1968
    else {
6,078✔
1969
        logger.debug("Sending: IDENT(client_file_ident=%1, client_file_ident_salt=%2, "
6,078✔
1970
                     "scan_server_version=%3, scan_client_version=%4, latest_server_version=%5, "
6,078✔
1971
                     "latest_server_version_salt=%6)",
6,078✔
1972
                     m_client_file_ident.ident, m_client_file_ident.salt, m_progress.download.server_version,
6,078✔
1973
                     m_progress.download.last_integrated_client_version, m_progress.latest_server_version.version,
6,078✔
1974
                     m_progress.latest_server_version.salt);                                  // Throws
6,078✔
1975
        protocol.make_pbs_ident_message(out, session_ident, m_client_file_ident, m_progress); // Throws
6,078✔
1976
    }
6,078✔
1977
    m_conn.initiate_write_message(out, this); // Throws
7,534✔
1978

1979
    m_ident_message_sent = true;
7,534✔
1980

1981
    // Other messages may be waiting to be sent
1982
    enlist_to_send(); // Throws
7,534✔
1983
}
7,534✔
1984

1985
void Session::send_query_change_message()
1986
{
1,168✔
1987
    REALM_ASSERT_EX(m_state == Active, m_state);
1,168✔
1988
    REALM_ASSERT(m_ident_message_sent);
1,168✔
1989
    REALM_ASSERT(!m_unbind_message_sent);
1,168✔
1990
    REALM_ASSERT(m_pending_flx_sub_set);
1,168✔
1991
    REALM_ASSERT_3(m_pending_flx_sub_set->query_version, >, m_last_sent_flx_query_version);
1,168✔
1992

1993
    if (REALM_UNLIKELY(get_client().is_dry_run())) {
1,168✔
1994
        return;
×
1995
    }
×
1996

1997
    auto sub_store = get_flx_subscription_store();
1,168✔
1998
    auto latest_sub_set = sub_store->get_by_version(m_pending_flx_sub_set->query_version);
1,168✔
1999
    auto latest_queries = latest_sub_set.to_ext_json();
1,168✔
2000
    logger.debug("Sending: QUERY(query_version=%1, query_size=%2, query=\"%3\", snapshot_version=%4)",
1,168✔
2001
                 latest_sub_set.version(), latest_queries.size(), latest_queries, latest_sub_set.snapshot_version());
1,168✔
2002

2003
    OutputBuffer& out = m_conn.get_output_buffer();
1,168✔
2004
    session_ident_type session_ident = get_ident();
1,168✔
2005
    ClientProtocol& protocol = m_conn.get_client_protocol();
1,168✔
2006
    protocol.make_query_change_message(out, session_ident, latest_sub_set.version(), latest_queries);
1,168✔
2007
    m_conn.initiate_write_message(out, this);
1,168✔
2008

2009
    m_last_sent_flx_query_version = latest_sub_set.version();
1,168✔
2010

2011
    request_download_completion_notification();
1,168✔
2012
}
1,168✔
2013

2014
void Session::send_upload_message()
2015
{
60,262✔
2016
    REALM_ASSERT_EX(m_state == Active, m_state);
60,262✔
2017
    REALM_ASSERT(m_ident_message_sent);
60,262✔
2018
    REALM_ASSERT(!m_unbind_message_sent);
60,262✔
2019

2020
    if (REALM_UNLIKELY(get_client().is_dry_run()))
60,262✔
2021
        return;
×
2022

2023
    version_type target_upload_version = m_last_version_available;
60,262✔
2024
    if (m_pending_flx_sub_set) {
60,262✔
2025
        REALM_ASSERT(m_is_flx_sync_session);
874✔
2026
        target_upload_version = m_pending_flx_sub_set->snapshot_version;
874✔
2027
    }
874✔
2028

2029
    std::vector<UploadChangeset> uploadable_changesets;
60,262✔
2030
    version_type locked_server_version = 0;
60,262✔
2031
    get_history().find_uploadable_changesets(m_upload_progress, target_upload_version, uploadable_changesets,
60,262✔
2032
                                             locked_server_version); // Throws
60,262✔
2033

2034
    if (uploadable_changesets.empty()) {
60,262✔
2035
        // Nothing more to upload right now
2036
        check_for_upload_completion(); // Throws
30,402✔
2037
        // If we need to limit upload up to some version other than the last client version available and there are no
2038
        // changes to upload, then there is no need to send an empty message.
2039
        if (m_pending_flx_sub_set) {
30,402✔
2040
            logger.debug("Empty UPLOAD was skipped (progress_client_version=%1, progress_server_version=%2)",
270✔
2041
                         m_upload_progress.client_version, m_upload_progress.last_integrated_server_version);
270✔
2042
            // Other messages may be waiting to be sent
2043
            return enlist_to_send(); // Throws
270✔
2044
        }
270✔
2045
    }
30,402✔
2046
    else {
29,860✔
2047
        m_last_version_selected_for_upload = uploadable_changesets.back().progress.client_version;
29,860✔
2048
    }
29,860✔
2049

2050
    if (m_pending_flx_sub_set && target_upload_version < m_last_version_available) {
59,992✔
2051
        logger.trace("Limiting UPLOAD message up to version %1 to send QUERY version %2",
604✔
2052
                     m_pending_flx_sub_set->snapshot_version, m_pending_flx_sub_set->query_version);
604✔
2053
    }
604✔
2054

2055
    version_type progress_client_version = m_upload_progress.client_version;
59,992✔
2056
    version_type progress_server_version = m_upload_progress.last_integrated_server_version;
59,992✔
2057

2058
    logger.debug("Sending: UPLOAD(progress_client_version=%1, progress_server_version=%2, "
59,992✔
2059
                 "locked_server_version=%3, num_changesets=%4)",
59,992✔
2060
                 progress_client_version, progress_server_version, locked_server_version,
59,992✔
2061
                 uploadable_changesets.size()); // Throws
59,992✔
2062

2063
    ClientProtocol& protocol = m_conn.get_client_protocol();
59,992✔
2064
    ClientProtocol::UploadMessageBuilder upload_message_builder = protocol.make_upload_message_builder(); // Throws
59,992✔
2065

2066
    for (const UploadChangeset& uc : uploadable_changesets) {
59,992✔
2067
        logger.debug(util::LogCategory::changeset,
43,256✔
2068
                     "Fetching changeset for upload (client_version=%1, server_version=%2, "
43,256✔
2069
                     "changeset_size=%3, origin_timestamp=%4, origin_file_ident=%5)",
43,256✔
2070
                     uc.progress.client_version, uc.progress.last_integrated_server_version, uc.changeset.size(),
43,256✔
2071
                     uc.origin_timestamp, uc.origin_file_ident); // Throws
43,256✔
2072
        if (logger.would_log(util::Logger::Level::trace)) {
43,256✔
2073
            BinaryData changeset_data = uc.changeset.get_first_chunk();
×
2074
            if (changeset_data.size() < 1024) {
×
2075
                logger.trace(util::LogCategory::changeset, "Changeset: %1",
×
2076
                             _impl::clamped_hex_dump(changeset_data)); // Throws
×
2077
            }
×
2078
            else {
×
2079
                logger.trace(util::LogCategory::changeset, "Changeset(comp): %1 %2", changeset_data.size(),
×
2080
                             protocol.compressed_hex_dump(changeset_data));
×
2081
            }
×
2082

2083
#if REALM_DEBUG
×
2084
            ChunkedBinaryInputStream in{changeset_data};
×
2085
            Changeset log;
×
2086
            try {
×
2087
                parse_changeset(in, log);
×
2088
                std::stringstream ss;
×
2089
                log.print(ss);
×
2090
                logger.trace(util::LogCategory::changeset, "Changeset (parsed):\n%1", ss.str());
×
2091
            }
×
2092
            catch (const BadChangesetError& err) {
×
2093
                logger.error(util::LogCategory::changeset, "Unable to parse changeset: %1", err.what());
×
2094
            }
×
2095
#endif
×
2096
        }
×
2097

2098
#if 0 // Upload log compaction is currently not implemented
2099
        if (!get_client().m_disable_upload_compaction) {
2100
            ChangesetEncoder::Buffer encode_buffer;
2101

2102
            {
2103
                // Upload compaction only takes place within single changesets to
2104
                // avoid another client seeing inconsistent snapshots.
2105
                ChunkedBinaryInputStream stream{uc.changeset};
2106
                Changeset changeset;
2107
                parse_changeset(stream, changeset); // Throws
2108
                // FIXME: What is the point of setting these? How can compaction care about them?
2109
                changeset.version = uc.progress.client_version;
2110
                changeset.last_integrated_remote_version = uc.progress.last_integrated_server_version;
2111
                changeset.origin_timestamp = uc.origin_timestamp;
2112
                changeset.origin_file_ident = uc.origin_file_ident;
2113

2114
                compact_changesets(&changeset, 1);
2115
                encode_changeset(changeset, encode_buffer);
2116

2117
                logger.debug(util::LogCategory::changeset, "Upload compaction: original size = %1, compacted size = %2", uc.changeset.size(),
2118
                             encode_buffer.size()); // Throws
2119
            }
2120

2121
            upload_message_builder.add_changeset(
2122
                uc.progress.client_version, uc.progress.last_integrated_server_version, uc.origin_timestamp,
2123
                uc.origin_file_ident, BinaryData{encode_buffer.data(), encode_buffer.size()}); // Throws
2124
        }
2125
        else
2126
#endif
2127
        {
43,256✔
2128
            upload_message_builder.add_changeset(uc.progress.client_version,
43,256✔
2129
                                                 uc.progress.last_integrated_server_version, uc.origin_timestamp,
43,256✔
2130
                                                 uc.origin_file_ident,
43,256✔
2131
                                                 uc.changeset); // Throws
43,256✔
2132
        }
43,256✔
2133
    }
43,256✔
2134

2135
    int protocol_version = m_conn.get_negotiated_protocol_version();
59,992✔
2136
    OutputBuffer& out = m_conn.get_output_buffer();
59,992✔
2137
    session_ident_type session_ident = get_ident();
59,992✔
2138
    upload_message_builder.make_upload_message(protocol_version, out, session_ident, progress_client_version,
59,992✔
2139
                                               progress_server_version,
59,992✔
2140
                                               locked_server_version); // Throws
59,992✔
2141
    m_conn.initiate_write_message(out, this);                          // Throws
59,992✔
2142

2143
    // Other messages may be waiting to be sent
2144
    enlist_to_send(); // Throws
59,992✔
2145
}
59,992✔
2146

2147

2148
void Session::send_mark_message()
2149
{
17,434✔
2150
    REALM_ASSERT_EX(m_state == Active, m_state);
17,434✔
2151
    REALM_ASSERT(m_ident_message_sent);
17,434✔
2152
    REALM_ASSERT(!m_unbind_message_sent);
17,434✔
2153
    REALM_ASSERT_3(m_target_download_mark, >, m_last_download_mark_sent);
17,434✔
2154

2155
    request_ident_type request_ident = m_target_download_mark;
17,434✔
2156
    logger.debug("Sending: MARK(request_ident=%1)", request_ident); // Throws
17,434✔
2157

2158
    ClientProtocol& protocol = m_conn.get_client_protocol();
17,434✔
2159
    OutputBuffer& out = m_conn.get_output_buffer();
17,434✔
2160
    session_ident_type session_ident = get_ident();
17,434✔
2161
    protocol.make_mark_message(out, session_ident, request_ident); // Throws
17,434✔
2162
    m_conn.initiate_write_message(out, this);                      // Throws
17,434✔
2163

2164
    m_last_download_mark_sent = request_ident;
17,434✔
2165

2166
    // Other messages may be waiting to be sent
2167
    enlist_to_send(); // Throws
17,434✔
2168
}
17,434✔
2169

2170

2171
void Session::send_unbind_message()
2172
{
6,610✔
2173
    REALM_ASSERT_EX(m_state == Deactivating || m_error_message_received || m_suspended, m_state);
6,610✔
2174
    REALM_ASSERT(m_bind_message_sent);
6,610✔
2175
    REALM_ASSERT(!m_unbind_message_sent);
6,610✔
2176

2177
    logger.debug("Sending: UNBIND"); // Throws
6,610✔
2178

2179
    ClientProtocol& protocol = m_conn.get_client_protocol();
6,610✔
2180
    OutputBuffer& out = m_conn.get_output_buffer();
6,610✔
2181
    session_ident_type session_ident = get_ident();
6,610✔
2182
    protocol.make_unbind_message(out, session_ident); // Throws
6,610✔
2183
    m_conn.initiate_write_message(out, this);         // Throws
6,610✔
2184

2185
    m_unbind_message_sent = true;
6,610✔
2186
}
6,610✔
2187

2188

2189
void Session::send_json_error_message()
2190
{
32✔
2191
    REALM_ASSERT_EX(m_state == Active, m_state);
32✔
2192
    REALM_ASSERT(m_ident_message_sent);
32✔
2193
    REALM_ASSERT(!m_unbind_message_sent);
32✔
2194
    REALM_ASSERT(m_error_to_send);
32✔
2195
    REALM_ASSERT(m_client_error);
32✔
2196

2197
    ClientProtocol& protocol = m_conn.get_client_protocol();
32✔
2198
    OutputBuffer& out = m_conn.get_output_buffer();
32✔
2199
    session_ident_type session_ident = get_ident();
32✔
2200
    auto protocol_error = m_client_error->error_for_server;
32✔
2201

2202
    auto message = util::format("%1", m_client_error->to_status());
32✔
2203
    logger.info("Sending: ERROR \"%1\" (error_code=%2, session_ident=%3)", message, static_cast<int>(protocol_error),
32✔
2204
                session_ident); // Throws
32✔
2205

2206
    nlohmann::json error_body_json;
32✔
2207
    error_body_json["message"] = std::move(message);
32✔
2208
    protocol.make_json_error_message(out, session_ident, static_cast<int>(protocol_error),
32✔
2209
                                     error_body_json.dump()); // Throws
32✔
2210
    m_conn.initiate_write_message(out, this);                 // Throws
32✔
2211

2212
    m_error_to_send = false;
32✔
2213
    enlist_to_send(); // Throws
32✔
2214
}
32✔
2215

2216

2217
void Session::send_test_command_message()
2218
{
52✔
2219
    REALM_ASSERT_EX(m_state == Active, m_state);
52✔
2220

2221
    auto it = std::find_if(m_pending_test_commands.begin(), m_pending_test_commands.end(),
52✔
2222
                           [](const PendingTestCommand& command) {
52✔
2223
                               return command.pending;
52✔
2224
                           });
52✔
2225
    REALM_ASSERT(it != m_pending_test_commands.end());
52✔
2226

2227
    ClientProtocol& protocol = m_conn.get_client_protocol();
52✔
2228
    OutputBuffer& out = m_conn.get_output_buffer();
52✔
2229
    auto session_ident = get_ident();
52✔
2230

2231
    logger.info("Sending: TEST_COMMAND \"%1\" (session_ident=%2, request_ident=%3)", it->body, session_ident, it->id);
52✔
2232
    protocol.make_test_command_message(out, session_ident, it->id, it->body);
52✔
2233

2234
    m_conn.initiate_write_message(out, this); // Throws;
52✔
2235
    it->pending = false;
52✔
2236

2237
    enlist_to_send();
52✔
2238
}
52✔
2239

2240
bool Session::client_reset_if_needed()
2241
{
3,670✔
2242
    // Regardless of what happens, once we return from this function we will
2243
    // no longer be in the middle of a client reset
2244
    m_performing_client_reset = false;
3,670✔
2245

2246
    // Even if we end up not actually performing a client reset, consume the
2247
    // config to ensure that the resources it holds are released
2248
    auto client_reset_config = std::exchange(get_client_reset_config(), std::nullopt);
3,670✔
2249
    if (!client_reset_config) {
3,670✔
2250
        return false;
3,302✔
2251
    }
3,302✔
2252

2253
    auto on_flx_version_complete = [this](int64_t version) {
368✔
2254
        this->on_flx_sync_version_complete(version);
292✔
2255
    };
292✔
2256
    bool did_reset = client_reset::perform_client_reset(
368✔
2257
        logger, *get_db(), *client_reset_config->fresh_copy, client_reset_config->mode,
368✔
2258
        std::move(client_reset_config->notify_before_client_reset),
368✔
2259
        std::move(client_reset_config->notify_after_client_reset), m_client_file_ident, get_flx_subscription_store(),
368✔
2260
        on_flx_version_complete, client_reset_config->recovery_is_allowed);
368✔
2261
    if (!did_reset) {
368✔
2262
        return false;
×
2263
    }
×
2264

2265
    // The fresh Realm has been used to reset the state
2266
    logger.debug("Client reset is completed, path=%1", get_realm_path()); // Throws
368✔
2267

2268
    SaltedFileIdent client_file_ident;
368✔
2269
    bool has_pending_client_reset = false;
368✔
2270
    get_history().get_status(m_last_version_available, client_file_ident, m_progress,
368✔
2271
                             &has_pending_client_reset); // Throws
368✔
2272
    REALM_ASSERT_3(m_client_file_ident.ident, ==, client_file_ident.ident);
368✔
2273
    REALM_ASSERT_3(m_client_file_ident.salt, ==, client_file_ident.salt);
368✔
2274
    REALM_ASSERT_EX(m_progress.download.last_integrated_client_version == 0,
368✔
2275
                    m_progress.download.last_integrated_client_version);
368✔
2276
    REALM_ASSERT_EX(m_progress.upload.client_version == 0, m_progress.upload.client_version);
368✔
2277
    logger.trace("last_version_available  = %1", m_last_version_available); // Throws
368✔
2278

2279
    m_upload_progress = m_progress.upload;
368✔
2280
    m_download_progress = m_progress.download;
368✔
2281
    init_progress_handler();
368✔
2282
    // In recovery mode, there may be new changesets to upload and nothing left to download.
2283
    // In FLX DiscardLocal mode, there may be new commits due to subscription handling.
2284
    // For both, we want to allow uploads again without needing external changes to download first.
2285
    m_allow_upload = true;
368✔
2286
    REALM_ASSERT_EX(m_last_version_selected_for_upload == 0, m_last_version_selected_for_upload);
368✔
2287

2288
    if (has_pending_client_reset) {
368✔
2289
        handle_pending_client_reset_acknowledgement();
288✔
2290
    }
288✔
2291

2292
    update_subscription_version_info();
368✔
2293

2294
    // If a migration or rollback is in progress, mark it complete when client reset is completed.
2295
    if (auto migration_store = get_migration_store()) {
368✔
2296
        migration_store->complete_migration_or_rollback();
260✔
2297
    }
260✔
2298

2299
    return true;
368✔
2300
}
368✔
2301

2302
Status Session::receive_ident_message(SaltedFileIdent client_file_ident)
2303
{
3,724✔
2304
    logger.debug("Received: IDENT(client_file_ident=%1, client_file_ident_salt=%2)", client_file_ident.ident,
3,724✔
2305
                 client_file_ident.salt); // Throws
3,724✔
2306

2307
    // Ignore the message if the deactivation process has been initiated,
2308
    // because in that case, the associated Realm and SessionWrapper must
2309
    // not be accessed any longer.
2310
    if (m_state != Active)
3,724✔
2311
        return Status::OK(); // Success
54✔
2312

2313
    bool legal_at_this_time = (m_bind_message_sent && !have_client_file_ident() && !m_error_message_received &&
3,670✔
2314
                               !m_unbound_message_received);
3,670✔
2315
    if (REALM_UNLIKELY(!legal_at_this_time)) {
3,670✔
2316
        return {ErrorCodes::SyncProtocolInvariantFailed, "Received IDENT message when it was not legal"};
×
2317
    }
×
2318
    if (REALM_UNLIKELY(client_file_ident.ident < 1)) {
3,670✔
2319
        return {ErrorCodes::SyncProtocolInvariantFailed, "Bad client file identifier in IDENT message"};
×
2320
    }
×
2321
    if (REALM_UNLIKELY(client_file_ident.salt == 0)) {
3,670✔
2322
        return {ErrorCodes::SyncProtocolInvariantFailed, "Bad client file identifier salt in IDENT message"};
×
2323
    }
×
2324

2325
    m_client_file_ident = client_file_ident;
3,670✔
2326

2327
    if (REALM_UNLIKELY(get_client().is_dry_run())) {
3,670✔
2328
        // Ready to send the IDENT message
2329
        ensure_enlisted_to_send(); // Throws
×
2330
        return Status::OK();       // Success
×
2331
    }
×
2332

2333
    // if a client reset happens, it will take care of setting the file ident
2334
    // and if not, we do it here
2335
    bool did_client_reset = false;
3,670✔
2336
    try {
3,670✔
2337
        did_client_reset = client_reset_if_needed();
3,670✔
2338
    }
3,670✔
2339
    catch (const std::exception& e) {
3,670✔
2340
        auto err_msg = util::format("A fatal error occurred during client reset: '%1'", e.what());
80✔
2341
        logger.error(err_msg.c_str());
80✔
2342
        SessionErrorInfo err_info(Status{ErrorCodes::AutoClientResetFailed, err_msg}, IsFatal{true});
80✔
2343
        suspend(err_info);
80✔
2344
        return Status::OK();
80✔
2345
    }
80✔
2346
    if (!did_client_reset) {
3,590✔
2347
        get_history().set_client_file_ident(client_file_ident,
3,302✔
2348
                                            m_fix_up_object_ids); // Throws
3,302✔
2349
        m_progress.download.last_integrated_client_version = 0;
3,302✔
2350
        m_progress.upload.client_version = 0;
3,302✔
2351
        m_last_version_selected_for_upload = 0;
3,302✔
2352
    }
3,302✔
2353

2354
    // Ready to send the IDENT message
2355
    ensure_enlisted_to_send(); // Throws
3,590✔
2356
    return Status::OK();       // Success
3,590✔
2357
}
3,670✔
2358

2359
Status Session::receive_download_message(const DownloadMessage& message)
2360
{
48,376✔
2361
    // Ignore the message if the deactivation process has been initiated,
2362
    // because in that case, the associated Realm and SessionWrapper must
2363
    // not be accessed any longer.
2364
    if (m_state != Active)
48,376✔
2365
        return Status::OK();
538✔
2366

2367
    bool is_flx = m_conn.is_flx_sync_connection();
47,838✔
2368
    int64_t query_version = is_flx ? *message.query_version : 0;
47,838✔
2369

2370
    if (!is_flx || query_version > 0)
47,838✔
2371
        enable_progress_notifications();
46,206✔
2372

2373
    // If this is a PBS connection, then every download message is its own complete batch.
2374
    bool last_in_batch = is_flx ? *message.last_in_batch : true;
47,838✔
2375
    auto batch_state = last_in_batch ? sync::DownloadBatchState::LastInBatch : sync::DownloadBatchState::MoreToCome;
47,838✔
2376

2377
    auto&& progress = message.progress;
47,838✔
2378
    if (is_flx) {
47,838✔
2379
        logger.debug("Received: DOWNLOAD(download_server_version=%1, download_client_version=%2, "
3,706✔
2380
                     "latest_server_version=%3, latest_server_version_salt=%4, "
3,706✔
2381
                     "upload_client_version=%5, upload_server_version=%6, progress_estimate=%7, "
3,706✔
2382
                     "last_in_batch=%8, query_version=%9, num_changesets=%10, ...)",
3,706✔
2383
                     progress.download.server_version, progress.download.last_integrated_client_version,
3,706✔
2384
                     progress.latest_server_version.version, progress.latest_server_version.salt,
3,706✔
2385
                     progress.upload.client_version, progress.upload.last_integrated_server_version,
3,706✔
2386
                     message.progress_estimate, last_in_batch, query_version, message.changesets.size()); // Throws
3,706✔
2387
    }
3,706✔
2388
    else {
44,132✔
2389
        logger.debug("Received: DOWNLOAD(download_server_version=%1, download_client_version=%2, "
44,132✔
2390
                     "latest_server_version=%3, latest_server_version_salt=%4, "
44,132✔
2391
                     "upload_client_version=%5, upload_server_version=%6, "
44,132✔
2392
                     "downloadable_bytes=%7, num_changesets=%8, ...)",
44,132✔
2393
                     progress.download.server_version, progress.download.last_integrated_client_version,
44,132✔
2394
                     progress.latest_server_version.version, progress.latest_server_version.salt,
44,132✔
2395
                     progress.upload.client_version, progress.upload.last_integrated_server_version,
44,132✔
2396
                     message.downloadable_bytes, message.changesets.size()); // Throws
44,132✔
2397
    }
44,132✔
2398

2399
    // Ignore download messages when the client detects an error. This is to prevent transforming the same bad
2400
    // changeset over and over again.
2401
    if (m_client_error) {
47,838✔
2402
        logger.debug("Ignoring download message because the client detected an integration error");
×
2403
        return Status::OK();
×
2404
    }
×
2405

2406
    bool legal_at_this_time = (m_ident_message_sent && !m_error_message_received && !m_unbound_message_received);
47,838✔
2407
    if (REALM_UNLIKELY(!legal_at_this_time)) {
47,838✔
2408
        return {ErrorCodes::SyncProtocolInvariantFailed, "Received DOWNLOAD message when it was not legal"};
2✔
2409
    }
2✔
2410
    if (auto status = check_received_sync_progress(progress); REALM_UNLIKELY(!status.is_ok())) {
47,836✔
2411
        logger.error("Bad sync progress received (%1)", status);
×
2412
        return status;
×
2413
    }
×
2414

2415
    // Start with the download server version from the last download message, since the changesets in the new
2416
    // download message must have a remote version greater than (or equal to for FLX) this value.
2417
    version_type last_remote_version = m_progress.download.server_version;
47,836✔
2418
    version_type last_integrated_client_version = m_progress.download.last_integrated_client_version;
47,836✔
2419
    // If there are 2 or more changesets in this message, track whether or not the changesets all have the same
2420
    // download_server_version to help with determining if this is a single message server-initiated bootstrap or not.
2421
    bool same_remote_version = last_in_batch && message.changesets.size() > 1;
47,836✔
2422
    bool after_first_changeset = false;
47,836✔
2423

2424
    for (const RemoteChangeset& changeset : message.changesets) {
49,312✔
2425
        // Check that per-changeset server version is strictly increasing since the last download server version,
2426
        // except in FLX sync where the server version must be increasing, but can stay the same during bootstraps.
2427
        bool good_server_version = m_is_flx_sync_session ? (changeset.remote_version >= last_remote_version)
46,328✔
2428
                                                         : (changeset.remote_version > last_remote_version);
46,328✔
2429
        // Each server version cannot be greater than the one in the header of the download message.
2430
        good_server_version = good_server_version && (changeset.remote_version <= progress.download.server_version);
46,328✔
2431
        if (!good_server_version) {
46,328✔
2432
            return {ErrorCodes::SyncProtocolInvariantFailed,
×
2433
                    util::format("Bad server version in changeset header (DOWNLOAD) (%1, %2, %3)",
×
2434
                                 changeset.remote_version, last_remote_version, progress.download.server_version)};
×
2435
        }
×
2436
        // Check to see if all the changesets in this LastInBatch=true message have the same remote_version
2437
        // If so, this is a server-initiated single message bootstrap
2438
        if (same_remote_version && after_first_changeset) {
46,328✔
2439
            // After the first changeset compare the previous changeset's server version to the current changeset
2440
            // server version. If they are different then this is definitely not a bootstrap message
2441
            same_remote_version = changeset.remote_version == last_remote_version;
8,200✔
2442
        }
8,200✔
2443
        // Skip the first changeset, since `server_version` contains the incorrect value for this check
2444
        after_first_changeset = true;
46,328✔
2445
        // Save the remote version for the current changeset to compare against the next changeset
2446
        last_remote_version = changeset.remote_version;
46,328✔
2447

2448
        // Check that per-changeset last integrated client version is "weakly"
2449
        // increasing.
2450
        bool good_client_version =
46,328✔
2451
            (changeset.last_integrated_local_version >= last_integrated_client_version &&
46,328✔
2452
             changeset.last_integrated_local_version <= progress.download.last_integrated_client_version);
46,328✔
2453
        if (!good_client_version) {
46,328✔
2454
            return {ErrorCodes::SyncProtocolInvariantFailed,
×
2455
                    util::format("Bad last integrated client version in changeset header (DOWNLOAD) "
×
2456
                                 "(%1, %2, %3)",
×
2457
                                 changeset.last_integrated_local_version, last_integrated_client_version,
×
2458
                                 progress.download.last_integrated_client_version)};
×
2459
        }
×
2460
        last_integrated_client_version = changeset.last_integrated_local_version;
46,328✔
2461
        // Server shouldn't send our own changes, and zero is not a valid client
2462
        // file identifier.
2463
        bool good_file_ident =
46,328✔
2464
            (changeset.origin_file_ident > 0 && changeset.origin_file_ident != m_client_file_ident.ident);
46,328✔
2465
        if (!good_file_ident) {
46,328✔
2466
            return {ErrorCodes::SyncProtocolInvariantFailed,
×
2467
                    util::format("Bad origin file identifier in changeset header (DOWNLOAD)",
×
2468
                                 changeset.origin_file_ident)};
×
2469
        }
×
2470
    }
46,328✔
2471

2472
    if (is_steady_state_download_message(batch_state, query_version, same_remote_version)) {
47,836✔
2473
        batch_state = DownloadBatchState::SteadyState;
45,544✔
2474
    }
45,544✔
2475
    m_last_download_batch_state = batch_state;
47,836✔
2476

2477
    auto hook_action = call_debug_hook(SyncClientHookEvent::DownloadMessageReceived, progress, query_version,
47,836✔
2478
                                       batch_state, message.changesets.size());
47,836✔
2479
    if (hook_action == SyncClientHookAction::EarlyReturn) {
47,836✔
2480
        return Status::OK();
16✔
2481
    }
16✔
2482
    REALM_ASSERT_EX(hook_action == SyncClientHookAction::NoAction, hook_action);
47,820✔
2483

2484
    if (is_flx)
47,820✔
2485
        update_download_estimate(message.progress_estimate);
3,692✔
2486

2487
    if (process_flx_bootstrap_message(progress, batch_state, query_version, message.changesets)) {
47,820✔
2488
        clear_resumption_delay_state();
2,282✔
2489
        return Status::OK();
2,282✔
2490
    }
2,282✔
2491

2492
    uint64_t downloadable_bytes = is_flx ? 0 : message.downloadable_bytes;
45,538✔
2493
    initiate_integrate_changesets(downloadable_bytes, batch_state, progress, message.changesets); // Throws
45,538✔
2494

2495
    hook_action = call_debug_hook(SyncClientHookEvent::DownloadMessageIntegrated, progress, query_version,
45,538✔
2496
                                  batch_state, message.changesets.size());
45,538✔
2497
    if (hook_action == SyncClientHookAction::EarlyReturn) {
45,538✔
2498
        return Status::OK();
×
2499
    }
×
2500
    REALM_ASSERT_EX(hook_action == SyncClientHookAction::NoAction, hook_action);
45,538✔
2501

2502
    // When we receive a DOWNLOAD message successfully, we can clear the backoff timer value used to reconnect
2503
    // after a retryable session error.
2504
    clear_resumption_delay_state();
45,538✔
2505
    return Status::OK();
45,538✔
2506
}
45,538✔
2507

2508
Status Session::receive_mark_message(request_ident_type request_ident)
2509
{
16,654✔
2510
    logger.debug("Received: MARK(request_ident=%1)", request_ident); // Throws
16,654✔
2511

2512
    // Ignore the message if the deactivation process has been initiated,
2513
    // because in that case, the associated Realm and SessionWrapper must
2514
    // not be accessed any longer.
2515
    if (m_state != Active)
16,654✔
2516
        return Status::OK(); // Success
70✔
2517

2518
    bool legal_at_this_time = (m_ident_message_sent && !m_error_message_received && !m_unbound_message_received);
16,584✔
2519
    if (REALM_UNLIKELY(!legal_at_this_time)) {
16,584✔
2520
        return {ErrorCodes::SyncProtocolInvariantFailed, "Received MARK message when it was not legal"};
10✔
2521
    }
10✔
2522
    bool good_request_ident =
16,574✔
2523
        (request_ident <= m_last_download_mark_sent && request_ident > m_last_download_mark_received);
16,574✔
2524
    if (REALM_UNLIKELY(!good_request_ident)) {
16,574✔
2525
        return {
×
2526
            ErrorCodes::SyncProtocolInvariantFailed,
×
2527
            util::format(
×
2528
                "Received MARK message with invalid request identifer (last mark sent: %1 last mark received: %2)",
×
2529
                m_last_download_mark_sent, m_last_download_mark_received)};
×
2530
    }
×
2531

2532
    m_server_version_at_last_download_mark = m_progress.download.server_version;
16,574✔
2533
    m_last_download_mark_received = request_ident;
16,574✔
2534
    check_for_download_completion(); // Throws
16,574✔
2535

2536
    return Status::OK(); // Success
16,574✔
2537
}
16,574✔
2538

2539

2540
// The caller (Connection) must discard the session if the session has become
2541
// deactivated upon return.
2542
Status Session::receive_unbound_message()
2543
{
4,618✔
2544
    logger.debug("Received: UNBOUND");
4,618✔
2545

2546
    bool legal_at_this_time = (m_unbind_message_sent && !m_error_message_received && !m_unbound_message_received);
4,618✔
2547
    if (REALM_UNLIKELY(!legal_at_this_time)) {
4,618✔
2548
        return {ErrorCodes::SyncProtocolInvariantFailed, "Received UNBOUND message when it was not legal"};
×
2549
    }
×
2550

2551
    // The fact that the UNBIND message has been sent, but an ERROR message has
2552
    // not been received, implies that the deactivation process must have been
2553
    // initiated, so this session must be in the Deactivating state or the session
2554
    // has been suspended because of a client side error.
2555
    REALM_ASSERT_EX(m_state == Deactivating || m_suspended, m_state);
4,618✔
2556

2557
    m_unbound_message_received = true;
4,618✔
2558

2559
    // Detect completion of the unbinding process
2560
    if (m_unbind_message_send_complete && m_state == Deactivating) {
4,618✔
2561
        // The deactivation process completes when the unbinding process
2562
        // completes.
2563
        complete_deactivation(); // Throws
4,618✔
2564
        // Life cycle state is now Deactivated
2565
    }
4,618✔
2566

2567
    return Status::OK(); // Success
4,618✔
2568
}
4,618✔
2569

2570

2571
Status Session::receive_query_error_message(int error_code, std::string_view message, int64_t query_version)
2572
{
20✔
2573
    logger.info("Received QUERY_ERROR \"%1\" (error_code=%2, query_version=%3)", message, error_code, query_version);
20✔
2574
    // Ignore the message if the deactivation process has been initiated,
2575
    // because in that case, the associated Realm and SessionWrapper must
2576
    // not be accessed any longer.
2577
    if (m_state == Active) {
20✔
2578
        on_flx_sync_error(query_version, message); // throws
20✔
2579
    }
20✔
2580
    return Status::OK();
20✔
2581
}
20✔
2582

2583
// The caller (Connection) must discard the session if the session has become
2584
// deactivated upon return.
2585
Status Session::receive_error_message(const ProtocolErrorInfo& info)
2586
{
732✔
2587
    logger.info("Received: ERROR \"%1\" (error_code=%2, is_fatal=%3, error_action=%4)", info.message,
732✔
2588
                info.raw_error_code, info.is_fatal, info.server_requests_action); // Throws
732✔
2589

2590
    bool legal_at_this_time = (m_bind_message_sent && !m_error_message_received && !m_unbound_message_received);
732✔
2591
    if (REALM_UNLIKELY(!legal_at_this_time)) {
732✔
2592
        return {ErrorCodes::SyncProtocolInvariantFailed, "Received ERROR message when it was not legal"};
×
2593
    }
×
2594

2595
    auto protocol_error = static_cast<ProtocolError>(info.raw_error_code);
732✔
2596
    auto status = protocol_error_to_status(protocol_error, info.message);
732✔
2597
    if (status != ErrorCodes::UnknownError && REALM_UNLIKELY(!is_session_level_error(protocol_error))) {
732✔
2598
        return {ErrorCodes::SyncProtocolInvariantFailed,
×
2599
                util::format("Received ERROR message for session with non-session-level error code %1",
×
2600
                             info.raw_error_code)};
×
2601
    }
×
2602

2603
    // Can't process debug hook actions once the Session is undergoing deactivation, since
2604
    // the SessionWrapper may not be available
2605
    if (m_state == Active) {
732✔
2606
        auto debug_action = call_debug_hook(SyncClientHookEvent::ErrorMessageReceived, info);
730✔
2607
        if (debug_action == SyncClientHookAction::EarlyReturn) {
730✔
2608
            return Status::OK();
8✔
2609
        }
8✔
2610
    }
730✔
2611

2612
    // For compensating write errors, we need to defer raising them to the SDK until after the server version
2613
    // containing the compensating write has appeared in a download message.
2614
    if (status == ErrorCodes::SyncCompensatingWrite) {
724✔
2615
        // If the client is not active, the compensating writes will not be processed now, but will be
2616
        // sent again the next time the client connects
2617
        if (m_state == Active) {
44✔
2618
            REALM_ASSERT(info.compensating_write_server_version.has_value());
44✔
2619
            m_pending_compensating_write_errors.push_back(info);
44✔
2620
        }
44✔
2621
        return Status::OK();
44✔
2622
    }
44✔
2623

2624
    if (protocol_error == ProtocolError::schema_version_changed) {
680✔
2625
        // Enable upload immediately if the session is still active.
2626
        if (m_state == Active) {
68✔
2627
            auto wt = get_db()->start_write();
68✔
2628
            _impl::sync_schema_migration::track_sync_schema_migration(*wt, *info.previous_schema_version);
68✔
2629
            wt->commit();
68✔
2630
            // Notify SyncSession a schema migration is required.
2631
            on_connection_state_changed(m_conn.get_state(), SessionErrorInfo{info});
68✔
2632
        }
68✔
2633
        // Keep the session active to upload any unsynced changes.
2634
        return Status::OK();
68✔
2635
    }
68✔
2636

2637
    m_error_message_received = true;
612✔
2638
    suspend(SessionErrorInfo{info, std::move(status)});
612✔
2639
    return Status::OK();
612✔
2640
}
680✔
2641

2642
void Session::suspend(const SessionErrorInfo& info)
2643
{
692✔
2644
    REALM_ASSERT(!m_suspended);
692✔
2645
    REALM_ASSERT_EX(m_state == Active || m_state == Deactivating, m_state);
692✔
2646
    logger.debug("Suspended"); // Throws
692✔
2647

2648
    m_suspended = true;
692✔
2649

2650
    // Detect completion of the unbinding process
2651
    if (m_unbind_message_send_complete && m_error_message_received) {
692✔
2652
        // The fact that the UNBIND message has been sent, but we are not being suspended because
2653
        // we received an ERROR message implies that the deactivation process must
2654
        // have been initiated, so this session must be in the Deactivating state.
2655
        REALM_ASSERT_EX(m_state == Deactivating, m_state);
2✔
2656

2657
        // The deactivation process completes when the unbinding process
2658
        // completes.
2659
        complete_deactivation(); // Throws
2✔
2660
        // Life cycle state is now Deactivated
2661
    }
2✔
2662

2663
    // Notify the application of the suspension of the session if the session is
2664
    // still in the Active state
2665
    if (m_state == Active) {
692✔
2666
        call_debug_hook(SyncClientHookEvent::SessionSuspended, info);
690✔
2667
        m_conn.one_less_active_unsuspended_session(); // Throws
690✔
2668
        on_suspended(info);                           // Throws
690✔
2669
    }
690✔
2670

2671
    if (!info.is_fatal) {
692✔
2672
        begin_resumption_delay(info);
86✔
2673
    }
86✔
2674

2675
    // Ready to send the UNBIND message, if it has not been sent already
2676
    if (!m_unbind_message_sent)
692✔
2677
        ensure_enlisted_to_send(); // Throws
690✔
2678
}
692✔
2679

2680
Status Session::receive_test_command_response(request_ident_type ident, std::string_view body)
2681
{
52✔
2682
    logger.info("Received: TEST_COMMAND \"%1\" (session_ident=%2, request_ident=%3)", body, m_ident, ident);
52✔
2683
    auto it = std::find_if(m_pending_test_commands.begin(), m_pending_test_commands.end(),
52✔
2684
                           [&](const PendingTestCommand& command) {
52✔
2685
                               return command.id == ident;
52✔
2686
                           });
52✔
2687
    if (it == m_pending_test_commands.end()) {
52✔
2688
        return {ErrorCodes::SyncProtocolInvariantFailed,
×
2689
                util::format("Received test command response for a non-existent ident %1", ident)};
×
2690
    }
×
2691

2692
    it->promise.emplace_value(std::string{body});
52✔
2693
    m_pending_test_commands.erase(it);
52✔
2694

2695
    return Status::OK();
52✔
2696
}
52✔
2697

2698
void Session::begin_resumption_delay(const ProtocolErrorInfo& error_info)
2699
{
86✔
2700
    REALM_ASSERT(!m_try_again_activation_timer);
86✔
2701

2702
    m_try_again_delay_info.update(static_cast<sync::ProtocolError>(error_info.raw_error_code),
86✔
2703
                                  error_info.resumption_delay_interval);
86✔
2704
    auto try_again_interval = m_try_again_delay_info.delay_interval();
86✔
2705
    if (ProtocolError(error_info.raw_error_code) == ProtocolError::session_closed) {
86✔
2706
        // FIXME With compensating writes the server sends this error after completing a bootstrap. Doing the
2707
        // normal backoff behavior would result in waiting up to 5 minutes in between each query change which is
2708
        // not acceptable latency. So for this error code alone, we hard-code a 1 second retry interval.
2709
        try_again_interval = std::chrono::milliseconds{1000};
60✔
2710
    }
60✔
2711
    logger.debug("Will attempt to resume session after %1 milliseconds", try_again_interval.count());
86✔
2712
    m_try_again_activation_timer = get_client().create_timer(try_again_interval, [this](Status status) {
86✔
2713
        if (status == ErrorCodes::OperationAborted)
86✔
2714
            return;
12✔
2715
        else if (!status.is_ok())
74✔
2716
            throw Exception(status);
×
2717

2718
        m_try_again_activation_timer.reset();
74✔
2719
        cancel_resumption_delay();
74✔
2720
    });
74✔
2721
}
86✔
2722

2723
void Session::clear_resumption_delay_state()
2724
{
47,820✔
2725
    if (m_try_again_activation_timer) {
47,820✔
2726
        logger.debug("Clearing resumption delay state after successful download");
×
2727
        m_try_again_delay_info.reset();
×
2728
    }
×
2729
}
47,820✔
2730

2731
Status Session::check_received_sync_progress(const SyncProgress& progress) noexcept
2732
{
47,836✔
2733
    const SyncProgress& a = m_progress;
47,836✔
2734
    const SyncProgress& b = progress;
47,836✔
2735
    std::string message;
47,836✔
2736
    if (b.latest_server_version.version < a.latest_server_version.version) {
47,836✔
2737
        message = util::format("Latest server version in download messages must be weakly increasing throughout a "
×
2738
                               "session (current: %1, received: %2)",
×
2739
                               a.latest_server_version.version, b.latest_server_version.version);
×
2740
    }
×
2741
    if (b.upload.client_version < a.upload.client_version) {
47,836✔
2742
        message = util::format("Last integrated client version in download messages must be weakly increasing "
×
2743
                               "throughout a session (current: %1, received: %2)",
×
2744
                               a.upload.client_version, b.upload.client_version);
×
2745
    }
×
2746
    if (b.upload.client_version > m_last_version_available) {
47,836✔
2747
        message = util::format("Last integrated client version on server cannot be greater than the latest client "
×
2748
                               "version in existence (current: %1, received: %2)",
×
2749
                               m_last_version_available, b.upload.client_version);
×
2750
    }
×
2751
    if (b.download.server_version < a.download.server_version) {
47,836✔
2752
        message =
×
2753
            util::format("Download cursor must be weakly increasing throughout a session (current: %1, received: %2)",
×
2754
                         a.download.server_version, b.download.server_version);
×
2755
    }
×
2756
    if (b.download.server_version > b.latest_server_version.version) {
47,836✔
2757
        message = util::format(
×
2758
            "Download cursor cannot be greater than the latest server version in existence (cursor: %1, latest: %2)",
×
2759
            b.download.server_version, b.latest_server_version.version);
×
2760
    }
×
2761
    if (b.download.last_integrated_client_version < a.download.last_integrated_client_version) {
47,836✔
2762
        message = util::format(
×
2763
            "Last integrated client version on the server at the position in the server's history of the download "
×
2764
            "cursor must be weakly increasing throughout a session (current: %1, received: %2)",
×
2765
            a.download.last_integrated_client_version, b.download.last_integrated_client_version);
×
2766
    }
×
2767
    if (b.download.last_integrated_client_version > b.upload.client_version) {
47,836✔
2768
        message = util::format("Last integrated client version on the server in the position at the server's history "
×
2769
                               "of the download cursor cannot be greater than the latest client version integrated "
×
2770
                               "on the server (download: %1, upload: %2)",
×
2771
                               b.download.last_integrated_client_version, b.upload.client_version);
×
2772
    }
×
2773
    if (b.download.server_version < b.upload.last_integrated_server_version) {
47,836✔
2774
        message = util::format(
×
2775
            "The server version of the download cursor cannot be less than the server version integrated in the "
×
2776
            "latest client version acknowledged by the server (download: %1, upload: %2)",
×
2777
            b.download.server_version, b.upload.last_integrated_server_version);
×
2778
    }
×
2779

2780
    if (message.empty()) {
47,836✔
2781
        return Status::OK();
47,836✔
2782
    }
47,836✔
2783
    return {ErrorCodes::SyncProtocolInvariantFailed, std::move(message)};
×
2784
}
47,836✔
2785

2786

2787
void Session::check_for_upload_completion()
2788
{
80,618✔
2789
    REALM_ASSERT_EX(m_state == Active, m_state);
80,618✔
2790
    if (!m_upload_completion_notification_requested) {
80,618✔
2791
        return;
48,638✔
2792
    }
48,638✔
2793

2794
    // during an ongoing client reset operation, we never upload anything
2795
    if (m_performing_client_reset)
31,980✔
2796
        return;
260✔
2797

2798
    // Upload process must have reached end of history
2799
    REALM_ASSERT_3(m_upload_progress.client_version, <=, m_last_version_available);
31,720✔
2800
    bool scan_complete = (m_upload_progress.client_version == m_last_version_available);
31,720✔
2801
    if (!scan_complete)
31,720✔
2802
        return;
5,220✔
2803

2804
    // All uploaded changesets must have been acknowledged by the server
2805
    REALM_ASSERT_3(m_progress.upload.client_version, <=, m_last_version_selected_for_upload);
26,500✔
2806
    bool all_uploads_accepted = (m_progress.upload.client_version == m_last_version_selected_for_upload);
26,500✔
2807
    if (!all_uploads_accepted)
26,500✔
2808
        return;
11,464✔
2809

2810
    m_upload_completion_notification_requested = false;
15,036✔
2811
    on_upload_completion(); // Throws
15,036✔
2812
}
15,036✔
2813

2814

2815
void Session::check_for_download_completion()
2816
{
64,048✔
2817
    REALM_ASSERT_3(m_target_download_mark, >=, m_last_download_mark_received);
64,048✔
2818
    REALM_ASSERT_3(m_last_download_mark_received, >=, m_last_triggering_download_mark);
64,048✔
2819
    if (m_last_download_mark_received == m_last_triggering_download_mark)
64,048✔
2820
        return;
47,228✔
2821
    if (m_last_download_mark_received < m_target_download_mark)
16,820✔
2822
        return;
406✔
2823
    if (m_download_progress.server_version < m_server_version_at_last_download_mark)
16,414✔
2824
        return;
×
2825
    m_last_triggering_download_mark = m_target_download_mark;
16,414✔
2826
    if (REALM_UNLIKELY(!m_allow_upload)) {
16,414✔
2827
        // Activate the upload process now, and enable immediate reactivation
2828
        // after a subsequent fast reconnect.
2829
        m_allow_upload = true;
4,684✔
2830
        ensure_enlisted_to_send(); // Throws
4,684✔
2831
    }
4,684✔
2832
    on_download_completion(); // Throws
16,414✔
2833
}
16,414✔
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

© 2025 Coveralls, Inc