Even though queue is assigned to FoundationConnection it's never used for write results like it's used for reads.
In app I work on we've observed quite significant number of crashes coming from WriteItem.scheduledWrites.remove() which is builtin method of Set.
After bit of analysis (and usage of Thread Sanitiser) I discovered that these crashes were caused by concurrent modification of this Set inside of CocoaMQTTWebSocket.write() method.
First part, where newWrite is inserted to Set is executed on internalQueue which is fine. However, when connection.write gives results back it does that on URLSession internal queue -> it is possible that remove will be called at the same time as insert if there's multiple writes scheduled.
important I've never tested this case but I have a feeling that if someone won't use FoundationConnection but instead StarscreamConnection it can happen as well because provided queue isn't used for write too.
Even though queue is assigned to FoundationConnection it's never used for write results like it's used for reads.
In app I work on we've observed quite significant number of crashes coming from
WriteItem.scheduledWrites.remove()
which is builtin method ofSet
.After bit of analysis (and usage of Thread Sanitiser) I discovered that these crashes were caused by concurrent modification of this Set inside of
CocoaMQTTWebSocket.write()
method.First part, where
newWrite
is inserted to Set is executed oninternalQueue
which is fine. However, whenconnection.write
gives results back it does that onURLSession
internal queue -> it is possible thatremove
will be called at the same time asinsert
if there's multiple writes scheduled.important I've never tested this case but I have a feeling that if someone won't use
FoundationConnection
but insteadStarscreamConnection
it can happen as well because provided queue isn't used forwrite
too.