centrifugal / centrifuge-swift

Swift client SDK for bidirectional real-time communication with Centrifugo and Centrifuge-based server over WebSocket
MIT License
47 stars 41 forks source link

Thread Sanitizer detected multiple runtime issues in WebSocket implementation #81

Open antonselyanin opened 1 year ago

antonselyanin commented 1 year ago

Noticed several data races on 0.5.5 release. This doesn't look good :( (Thread Sanitizer can be used only while running code on simulator ATM). I will try to investigate this issue when I have time.

WebSocket.fragBuffer field

<project path>/centrifuge-swift/Sources/SwiftCentrifuge/WebSocket.swift:769 Data race in SwiftCentrifuge.WebSocket.fragBuffer.getter : Swift.Optional<Foundation.Data> at 0x125a19e00

Location is a 672-byte heap object at 0x125a19e00
Write of size 8 by thread 314
#0  0x000000010449cce0 in WebSocket.fragBuffer.setter ()
#1  0x00000001044a62bc in WebSocket.cleanupStream() at <project path>/centrifuge-swift/Sources/SwiftCentrifuge/WebSocket.swift:742
#2  0x00000001044a6120 in WebSocket.disconnectStream(_:runDelegate:) at <project path>/centrifuge-swift/Sources/SwiftCentrifuge/WebSocket.swift:729
#3  0x00000001044a4824 in WebSocket.initStreamsWithData(_:_:) at <project path>/centrifuge-swift/Sources/SwiftCentrifuge/WebSocket.swift:649
#4  0x00000001044a3744 in WebSocket.createHTTPRequest() at <project path>/centrifuge-swift/Sources/SwiftCentrifuge/WebSocket.swift:619
#5  0x000000010449fbfc in WebSocket.connect() at <project path>/centrifuge-swift/Sources/SwiftCentrifuge/WebSocket.swift:499
#6  0x00000001044372d4 in closure #1 in closure #1 in closure #1 in CentrifugeClient.scheduleReconnect() at <project path>/centrifuge-swift/Sources/SwiftCentrifuge/Client.swift:761
#7  0x0000000104445dc4 in partial apply for closure #1 in closure #1 in closure #1 in CentrifugeClient.scheduleReconnect() ()
#8  0x000000010441bce0 in thunk for @escaping @callee_guaranteed () -> () ()
#9  0x000000011054b694 in __tsan::invoke_and_release_block(void*) ()
#10 0x00000001154a5d5c in _dispatch_client_callout ()
Read of size 8 by thread 309
#0  0x000000010449cbc8 in WebSocket.fragBuffer.getter ()
#1  0x00000001044a68c8 in closure #1 in WebSocket.dequeueInput() at <project path>/centrifuge-swift/Sources/SwiftCentrifuge/WebSocket.swift:769
#2  0x00000001044bbaa8 in partial apply for closure #1 in WebSocket.dequeueInput() ()
#3  0x00000001a1b31b24 in autoreleasepool<τ_0_0>(invoking:) ()
#4  0x00000001044a6560 in WebSocket.processInputStream() at <project path>/centrifuge-swift/Sources/SwiftCentrifuge/WebSocket.swift:757
#5  0x00000001044a5e30 in WebSocket.newBytesInStream() at <project path>/centrifuge-swift/Sources/SwiftCentrifuge/WebSocket.swift:711
#6  0x00000001044b87f0 in protocol witness for WSStreamDelegate.newBytesInStream() in conformance WebSocket ()
#7  0x00000001044905cc in FoundationStream.stream(_:handle:) at <project path>/centrifuge-swift/Sources/SwiftCentrifuge/WebSocket.swift:305
#8  0x0000000104490978 in @objc FoundationStream.stream(_:handle:) ()
#9  0x0000000180396438 in _signalEventSync ()
#10 0x00000001154a5d5c in _dispatch_client_callout ()

WebSocket.connected field

<project path>/centrifuge-swift/Sources/SwiftCentrifuge/WebSocket.swift:777 Data race in SwiftCentrifuge.WebSocket.connected.getter : Swift.Bool at 0x125a19e00

Location is a 672-byte heap object at 0x125a19e00
Read of size 1 by thread 309
#0  0x000000010449b7cc in WebSocket.connected.getter ()
#1  0x00000001044a6b3c in closure #1 in WebSocket.dequeueInput() at <project path>/centrifuge-swift/Sources/SwiftCentrifuge/WebSocket.swift:777
#2  0x00000001044bbaa8 in partial apply for closure #1 in WebSocket.dequeueInput() ()
#3  0x00000001a1b31b24 in autoreleasepool<τ_0_0>(invoking:) ()
#4  0x00000001044a6560 in WebSocket.processInputStream() at <project path>/centrifuge-swift/Sources/SwiftCentrifuge/WebSocket.swift:757
#5  0x00000001044a5e30 in WebSocket.newBytesInStream() at <project path>/centrifuge-swift/Sources/SwiftCentrifuge/WebSocket.swift:711
#6  0x00000001044b87f0 in protocol witness for WSStreamDelegate.newBytesInStream() in conformance WebSocket ()
#7  0x00000001044905cc in FoundationStream.stream(_:handle:) at <project path>/centrifuge-swift/Sources/SwiftCentrifuge/WebSocket.swift:305
#8  0x0000000104490978 in @objc FoundationStream.stream(_:handle:) ()
#9  0x0000000180396438 in _signalEventSync ()
#10 0x00000001154a5d5c in _dispatch_client_callout ()
Heap block allocated by thread 297
Write of size 1 by thread 314
#0  0x000000010449b8b4 in WebSocket.connected.setter ()
#1  0x00000001044a6134 in WebSocket.disconnectStream(_:runDelegate:) at <project path>/centrifuge-swift/Sources/SwiftCentrifuge/WebSocket.swift:730
#2  0x00000001044a4824 in WebSocket.initStreamsWithData(_:_:) at <project path>/centrifuge-swift/Sources/SwiftCentrifuge/WebSocket.swift:649
#3  0x00000001044a3744 in WebSocket.createHTTPRequest() at <project path>/centrifuge-swift/Sources/SwiftCentrifuge/WebSocket.swift:619
#4  0x000000010449fbfc in WebSocket.connect() at <project path>/centrifuge-swift/Sources/SwiftCentrifuge/WebSocket.swift:499
#5  0x00000001044372d4 in closure #1 in closure #1 in closure #1 in CentrifugeClient.scheduleReconnect() at <project path>/centrifuge-swift/Sources/SwiftCentrifuge/Client.swift:761
#6  0x0000000104445dc4 in partial apply for closure #1 in closure #1 in closure #1 in CentrifugeClient.scheduleReconnect() ()
#7  0x000000010441bce0 in thunk for @escaping @callee_guaranteed () -> () ()
#8  0x000000011054b694 in __tsan::invoke_and_release_block(void*) ()
#9  0x00000001154a5d5c in _dispatch_client_callout ()