automerge / automerge-repo-swift

Extends the Automerge-swift library, providing support for working with multiple Automerge documents at once, with pluggable network and storage providers.
https://swiftpackageindex.com/automerge/automerge-repo-swift/documentation/automergerepo
MIT License
23 stars 4 forks source link

websocket failing and never reconnecting #73

Closed heckj closed 6 months ago

heckj commented 6 months ago

Logging updates in place, and I suspect I messed up something - WebSocket connections are now failing starting to connect, then failing - and toggling the websocket off and on again isn't explicitly resetting it. So there's a miss in there, somewhere.

Logs from MeetingNotes (trace logging enabled on WebSocket):

WEBSOCKET: Activating websocket to wss://sync.automerge.org/
WEBSOCKET: SEND: JOIN[version: 1, sender: 0711B157-AAE4-4A40-8AF5-A97375338435, metadata: [storageId: nil, ephemeral: true]]
WEBSOCKET: Attempt to send a message without a connection or defined remote peer
P2PNET: Sending SYNC[documentId: 3BwAK2fZ3Nrgn9HRBb9yvNx9xXKX, sender: 0711B157-AAE4-4A40-8AF5-A97375338435, target: storage-server-sync-automerge-org, data: 334 bytes] to peer storage-server-sync-automerge-org
P2PNET: Unable to find a connection to peer storage-server-sync-automerge-org
WEBSOCKET: Peered to targetId: storage-server-sync-automerge-org PEER[version: 1, sender: storage-server-sync-automerge-org, target: 0711B157-AAE4-4A40-8AF5-A97375338435, metadata: [storageId: 3760df37-a4c6-4f66-9ecd-732039a9385d, ephemeral: false]]
WEBSOCKET: Error reading websocket: The operation couldn’t be completed. Socket is not connected

this may be an issue with the networking subsystem as well - the error P2PNET: Unable to find a connection to peer storage-server-sync-automerge-org should have found a connection. This is notably happening on iOS, with the simulator, but not on macOS, which seemed to hold a connection without issue and clearly sent sync messages upstream as text content was changed.

heckj commented 6 months ago

WIP to debug: https://github.com/automerge/automerge-repo-swift/pull/74

heckj commented 6 months ago

While in #74, I found an issue where I was presuming all message send() need a PeerID in order to send, which was probably failing the connect. I broke that out into a separate logic path, and only retarget a send() message IF peerId is available, which isn't the case on the initial sends to establish handshaking. The logic is only really needed for broadcast messages from the repository (sync, ephemeral, etc)

heckj commented 6 months ago

My screwed up logic was the issue - that's resolved with #74 and #75 (tracing left in place, but made "opt-in" to keep from flooding someone using this library that doesn't want that level of detail