Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make explicitly pausing the sync session a separate state #6183

Merged
merged 12 commits into from
Jan 11, 2023

Conversation

jbreams
Copy link
Contributor

@jbreams jbreams commented Jan 9, 2023

What, How & Why?

This makes the pausing of SyncSession's a separate state than being Inactive that is functionally the same except for being "sticky" until the user explicitly resumes the sync session. There are a bunch of places/rules where we revive a sync session besides the user explicitly calling resume(), and it can be confusing to users to pause a sync session and then have it resume unexpectedly because of some seemingly unrelated action (like freezing a realm).

This also renames SyncSession::log_out() to SyncSession::force_close() since the function's behavior doesn't actually have anything to do with authentication and is really just the behavior of close() if close ignored the StopPolicy of the realm's SyncConfig

☑️ ToDos

  • 📝 Changelog update
  • 🚦 Tests (or not relevant)
  • C-API, if public C++ API changed. (the C API already had a pause/resume function, and they've been updated to use the explicit pause/resume function instead of log_out()/revive_if_needed())

@cla-bot cla-bot bot added the cla: yes label Jan 9, 2023
@jbreams jbreams linked an issue Jan 10, 2023 that may be closed by this pull request
@jbreams jbreams marked this pull request as ready for review January 10, 2023 19:40
@jbreams jbreams requested review from michael-wb and tgoyne January 10, 2023 19:50
Copy link
Member

@tgoyne tgoyne left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a nice improvement. There's some trickiness around actually adopting it SDK-side since it's arguably a breaking change, but at worst it'll be good to have it already available for when we do a major version bump.

// Changing the state releases the lock, which means that by the
// time we reacquire the lock the state may have changed again
// (either due to one of the callbacks being invoked or another
// thread coincidentally doing something). We just attempt to keep
// switching it to inactive until it stays there.
become_inactive(std::move(state_lock));
if (m_state != State::Paused) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check can never be false.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💯

case State::WaitingForAccessToken:
become_paused(std::move(lock));
break;
case State::Inactive:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it correct to stay in Inactive here? This'd mean that pause() -> revive_if_needed() does sometimes result in an active session.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it's not correct. become_paused() needs to check if the old state was inactive and early return in this case.

Copy link
Contributor Author

@jbreams jbreams left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this breaking enough that I shouldn't merge it? Do we need to wait for a bigger change before adopting this? If so, I can pursue an alternate interim fix for #6085.

case State::WaitingForAccessToken:
become_paused(std::move(lock));
break;
case State::Inactive:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it's not correct. become_paused() needs to check if the old state was inactive and early return in this case.

// Changing the state releases the lock, which means that by the
// time we reacquire the lock the state may have changed again
// (either due to one of the callbacks being invoked or another
// thread coincidentally doing something). We just attempt to keep
// switching it to inactive until it stays there.
become_inactive(std::move(state_lock));
if (m_state != State::Paused) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💯

@tgoyne
Copy link
Member

tgoyne commented Jan 10, 2023

If I understand the changes correctly it's completely fine to merge; we can preserve the old behavior by continuing to call force_close()/revive_if_needed() in suspend() and resume() while figuring out how handle the switch. I think maybe we'd call resume() automatically when the user does an async open, since that's the one place I could see reasonable code relying on the old behavior.

Copy link
Contributor

@michael-wb michael-wb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM - walked through the state transitions and verified the paused state was "sticky"

@@ -970,6 +1028,9 @@ void SyncSession::close(util::CheckedUniqueLock lock)
case State::Dying:
m_state_mutex.unlock(lock);
break;
case State::Paused:
m_state_mutex.unlock(lock);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you mind adding a comment here that mentions paused is supposed to be sticky and that state should not be changed to inactive.

@jbreams jbreams merged commit c0a0b5b into master Jan 11, 2023
@jbreams jbreams deleted the jbr/allow_explicitly_pausing_resuming_sync branch January 11, 2023 15:53
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 21, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Sync session is resumed without explicitly calling resume
3 participants