@@ -511,9 +511,6 @@ import Logging
511511 var messageWithNewline = message
512512 messageWithNewline. append ( UInt8 ( ascii: " \n " ) )
513513
514- // Use a local actor-isolated variable to track continuation state
515- var sendContinuationResumed = false
516-
517514 try await withCheckedThrowingContinuation {
518515 [ weak self] ( continuation: CheckedContinuation < Void , Swift . Error > ) in
519516 guard let self = self else {
@@ -525,50 +522,43 @@ import Logging
525522 content: messageWithNewline,
526523 contentContext: . defaultMessage,
527524 isComplete: true ,
528- completion: . contentProcessed { [ weak self] error in
529- guard let self = self else { return }
530-
531- Task { @MainActor in
532- if !sendContinuationResumed {
533- sendContinuationResumed = true
534- if let error = error {
535- self . logger. error ( " Send error: \( error) " )
536-
537- // Check if we should attempt to reconnect on send failure
538- let isStopping = await self . isStopping // Await actor-isolated property
539- if !isStopping && self . reconnectionConfig. enabled {
540- let isConnected = await self . isConnected
541- if isConnected {
542- if error. isConnectionLost {
543- self . logger. warning (
544- " Connection appears broken, will attempt to reconnect... "
545- )
546-
547- // Schedule connection restart
548- Task { [ weak self] in // Operate on self's executor
549- guard let self = self else { return }
550-
551- await self . setIsConnected ( false )
552-
553- try ? await Task . sleep ( for: . milliseconds( 500 ) )
554-
555- let currentIsStopping = await self . isStopping
556- if !currentIsStopping {
557- // Cancel the connection, then attempt to reconnect fully.
558- self . connection. cancel ( )
559- try ? await self . connect ( )
560- }
561- }
562- }
525+ completion: . contentProcessed { [ weak self, continuation] error in
526+ guard let self = self else {
527+ continuation. resume (
528+ throwing: MCPError . internalError (
529+ " Transport deallocated during send " ) )
530+ return
531+ }
532+
533+ if let error = error {
534+ self . logger. error ( " Send error: \( error) " )
535+
536+ // Schedule reconnection check on a separate task
537+ Task { [ weak self] in
538+ guard let self = self else { return }
539+ let isStopping = await self . isStopping
540+ if !isStopping && self . reconnectionConfig. enabled {
541+ let isConnected = await self . isConnected
542+ if isConnected && error. isConnectionLost {
543+ self . logger. warning (
544+ " Connection appears broken, will attempt to reconnect... "
545+ )
546+ await self . setIsConnected ( false )
547+ try ? await Task . sleep ( for: . milliseconds( 500 ) )
548+
549+ let currentIsStopping = await self . isStopping
550+ if !currentIsStopping {
551+ self . connection. cancel ( )
552+ try ? await self . connect ( )
563553 }
564554 }
565-
566- continuation. resume (
567- throwing: MCPError . internalError ( " Send error: \( error) " ) )
568- } else {
569- continuation. resume ( )
570555 }
571556 }
557+
558+ continuation. resume (
559+ throwing: MCPError . internalError ( " Send error: \( error) " ) )
560+ } else {
561+ continuation. resume ( )
572562 }
573563 } )
574564 }
@@ -747,8 +737,6 @@ import Logging
747737 /// - Returns: The received data chunk
748738 /// - Throws: Network errors or transport failures
749739 private func receiveData( ) async throws -> Data {
750- var receiveContinuationResumed = false
751-
752740 return try await withCheckedThrowingContinuation {
753741 [ weak self] ( continuation: CheckedContinuation < Data , Swift . Error > ) in
754742 guard let self = self else {
@@ -758,22 +746,16 @@ import Logging
758746
759747 let maxLength = bufferConfig. maxReceiveBufferSize ?? Int . max
760748 connection. receive ( minimumIncompleteLength: 1 , maximumLength: maxLength) {
761- content, _, isComplete, error in
762- Task { @MainActor in
763- if !receiveContinuationResumed {
764- receiveContinuationResumed = true
765- if let error = error {
766- continuation. resume ( throwing: MCPError . transportError ( error) )
767- } else if let content = content {
768- continuation. resume ( returning: content)
769- } else if isComplete {
770- self . logger. trace ( " Connection completed by peer " )
771- continuation. resume ( throwing: MCPError . connectionClosed)
772- } else {
773- // EOF: Resume with empty data instead of throwing an error
774- continuation. resume ( returning: Data ( ) )
775- }
776- }
749+ [ weak self, continuation] content, _, isComplete, error in
750+ if let error = error {
751+ continuation. resume ( throwing: MCPError . transportError ( error) )
752+ } else if let content = content {
753+ continuation. resume ( returning: content)
754+ } else if isComplete {
755+ self ? . logger. trace ( " Connection completed by peer " )
756+ continuation. resume ( throwing: MCPError . connectionClosed)
757+ } else {
758+ continuation. resume ( returning: Data ( ) )
777759 }
778760 }
779761 }
0 commit comments