-
Notifications
You must be signed in to change notification settings - Fork 33
[Enhancement]Coordinate call leave cleanup through the Call state machine #1124
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
ipavlidakis
merged 2 commits into
develop
from
iliaspavlidakis/introduce-call-leaving-stage
Apr 22, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
Sources/StreamVideo/CallStateMachine/Stages/Call+LeavingStage.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| // | ||
| // Copyright © 2026 Stream.io Inc. All rights reserved. | ||
| // | ||
|
|
||
| import Foundation | ||
|
|
||
| extension Call.StateMachine.Stage { | ||
|
|
||
| /// Creates a leaving stage for the call state machine. | ||
| /// | ||
| /// - Parameters: | ||
| /// - context: The context containing necessary state. | ||
| /// - reason: Optional reason forwarded to the backend leave request. | ||
| /// - Returns: A new `LeavingStage` instance. | ||
| static func leaving( | ||
| _ context: Context, | ||
| reason: String? | ||
| ) -> Call.StateMachine.Stage { | ||
| LeavingStage(context, reason: reason) | ||
| } | ||
| } | ||
|
|
||
| extension Call.StateMachine.Stage { | ||
|
|
||
| /// Represents the leaving stage in the call state machine. | ||
| final class LeavingStage: Call.StateMachine.Stage, @unchecked Sendable { | ||
| private let reason: String? | ||
| private let disposableBag = DisposableBag() | ||
|
|
||
| init( | ||
| _ context: Context, | ||
| reason: String? | ||
| ) { | ||
| self.reason = reason | ||
| super.init(id: .leaving, context: context) | ||
| } | ||
|
|
||
| override func transition( | ||
| from previousStage: Call.StateMachine.Stage | ||
| ) -> Self? { | ||
| switch previousStage.id { | ||
|
Check warning on line 41 in Sources/StreamVideo/CallStateMachine/Stages/Call+LeavingStage.swift
|
||
| case .leaving: | ||
| return nil | ||
| default: | ||
| execute() | ||
| return self | ||
| } | ||
| } | ||
|
|
||
| private func execute() { | ||
| guard | ||
| let call = context.call, | ||
| case let .leaving(input) = context.input | ||
| else { | ||
| return | ||
| } | ||
|
|
||
| input.disposableBag.removeAll() | ||
| input.callController.leave(reason: reason) | ||
| input.closedCaptionsAdapter.stop() | ||
|
|
||
| /// Upon `Call.leave` we remove the call from the cache. Any | ||
| /// further actions that are required to happen on the call object | ||
| /// (e.g. rejoin) will need to fetch a new instance from | ||
| /// `StreamVideo`. | ||
| input.callCache.remove(for: call.cId) | ||
| input.resetOutgoingRingingController() | ||
| input.resetAudioFilter() | ||
|
|
||
| Task(disposableBag: disposableBag) { @MainActor [weak self, call] in | ||
| guard let self else { | ||
| return | ||
| } | ||
|
|
||
| if call.streamVideo.state.ringingCall?.cId == call.cId { | ||
| call.streamVideo.state.ringingCall = nil | ||
| } | ||
| if call.streamVideo.state.activeCall?.cId == call.cId { | ||
| call.streamVideo.state.activeCall = nil | ||
| } | ||
|
|
||
| do { | ||
| try transition?(.idle(.init(call: call))) | ||
| } catch { | ||
| log.error(error) | ||
| } | ||
|
|
||
| postNotification(with: CallNotification.callEnded, object: call) | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Avoid logging expected duplicate leaves as invalid transitions.
Returning
nilhere prevents duplicate cleanup, butStreamStateMachinelogs nil transitions asInvalidStateMachineTransition. Since repeated leave requests are expected, guard before callingtransitionor add a silent idempotent path that preserves the current leaving stage.One possible fix in
Call.leave(reason:)public func leave(reason: String? = nil) { - stateMachine.transition( - .leaving( + stateMachine.withLock { currentStage, transitionHandler in + guard currentStage.id != .leaving else { + return + } + + transitionHandler( + .leaving( .init( call: self, input: .leaving( @@ - reason: reason + reason: reason + ) ) ) - ) + } }🤖 Prompt for AI Agents