Skip to content
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

Fix PubSub subscribe on connection race condition #809

Merged
merged 3 commits into from
Jan 10, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions libp2p/protocols/pubsub/pubsubpeer.nim
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ type
onEvent*: OnEvent # Connectivity updates for peer
codec*: string # the protocol that this peer joined from
sendConn*: Connection # cached send connection
connectedFut: Future[void]
address*: Option[MultiAddress]
peerId*: PeerId
handler*: RPCHandler
Expand Down Expand Up @@ -120,6 +121,8 @@ proc handle*(p: PubSubPeer, conn: Connection) {.async.} =
conn, peer = p, closed = conn.closed
try:
try:
# wait for bidirectional connection
await p.connectedFut
while not conn.atEof:
trace "waiting for data", conn, peer = p, closed = conn.closed

Expand Down Expand Up @@ -174,6 +177,7 @@ proc connectOnce(p: PubSubPeer): Future[void] {.async.} =
# stop working so we make an effort to only keep a single channel alive

trace "Get new send connection", p, newConn
p.connectedFut.complete()
p.sendConn = newConn
p.address = if p.sendConn.observedAddr.isSome: some(p.sendConn.observedAddr.get) else: none(MultiAddress)

Expand All @@ -182,6 +186,8 @@ proc connectOnce(p: PubSubPeer): Future[void] {.async.} =

await handle(p, newConn)
finally:
if not p.connectedFut.finished():
p.connectedFut.fail(newException(LPError, "can't establish conn"))
Menduist marked this conversation as resolved.
Show resolved Hide resolved
if p.sendConn != nil:
trace "Removing send connection", p, conn = p.sendConn
await p.sendConn.close()
Expand Down Expand Up @@ -292,5 +298,6 @@ proc new*(
onEvent: onEvent,
codec: codec,
peerId: peerId,
connectedFut: newFuture[void](),
maxMessageSize: maxMessageSize
)