Skip to content

fix(realtime): make Push associated to MainActor #721

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

Merged
merged 3 commits into from
May 28, 2025
Merged

Conversation

grdsdev
Copy link
Collaborator

@grdsdev grdsdev commented May 28, 2025

What kind of change does this PR introduce?

Bug fix

What is the current behavior?

Push is currently implemented as an Actor.

What is the new behavior?

Refactor Push to be a class attached to the MainActor, which makes it easier to debug and reason about behavior.

Additional context

Add any other context or screenshots.

@grdsdev grdsdev force-pushed the fix/push-mainactor branch from 8c4dc93 to 469d805 Compare May 28, 2025 08:52
@grdsdev grdsdev marked this pull request as ready for review May 28, 2025 08:57
@@ -81,6 +83,7 @@ public final class RealtimeChannelV2: Sendable {
}

/// Subscribes to the channel
@MainActor
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This isn't a breaking change since the method is async, the call site already have to call this method with an await independent of the running actor.

// let status = await task.value
// XCTAssertEqual(status, .ok)
// }
func testPushWithAck() async {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Since Push is MainActor now, this flaky test passes consistently

@grdsdev grdsdev requested review from dshukertjr and Copilot May 28, 2025 09:01
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR refactors the Push implementation by converting it from an Actor to a MainActor-attached class for improved debuggability and reasoning. Key changes include:

  • Removing the custom invokeTest logic in tests and annotating the test class with @mainactor.
  • Updating the RealtimeChannelV2 to use direct MainActor access for mutable state.
  • Refactoring PushV2 from an actor to a final class annotated with @mainactor.

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
Tests/RealtimeTests/_PushTests.swift Removed custom invokeTest logic and updated async test flow
Sources/Realtime/RealtimeChannelV2.swift Refactored mutable state accesses and updated async patterns
Sources/Realtime/PushV2.swift Changed from an actor to a MainActor-attached final class


let task = Task {
await push.send()
}
Copy link
Preview

Copilot AI May 28, 2025

Choose a reason for hiding this comment

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

[nitpick] Consider adding a comment explaining the use of Task.megaYield() to clarify why it is used instead of the standard Task.yield(), ensuring its necessity for test scheduling.

Suggested change
}
}
// `Task.megaYield()` is used here instead of `Task.yield()` to ensure that all pending tasks
// are fully processed before proceeding. This is necessary for proper test scheduling and
// to simulate realistic asynchronous behavior in the test environment.

Copilot uses AI. Check for mistakes.

Comment on lines +503 to 505
Task { @MainActor in
mutableState.clientChanges.append(config)
}
Copy link
Preview

Copilot AI May 28, 2025

Choose a reason for hiding this comment

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

Since the enclosing context is already on the MainActor, consider appending directly to mutableState.clientChanges instead of dispatching via a Task, to simplify execution and ensure immediate consistency.

Suggested change
Task { @MainActor in
mutableState.clientChanges.append(config)
}
mutableState.clientChanges.append(config)

Copilot uses AI. Check for mistakes.

@coveralls
Copy link

Pull Request Test Coverage Report for Build 15296140878

Details

  • 18 of 18 (100.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.1%) to 75.648%

Totals Coverage Status
Change from base Build 15255576724: 0.1%
Covered Lines: 5222
Relevant Lines: 6903

💛 - Coveralls

@grdsdev grdsdev merged commit 7d6870f into main May 28, 2025
23 checks passed
@grdsdev grdsdev deleted the fix/push-mainactor branch May 28, 2025 13:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants