Skip to content

Commit

Permalink
fix: close stream after sending identify (#1424)
Browse files Browse the repository at this point in the history
Close the stream after we finish writing the identify message. This is behavior is specified https://github.com/libp2p/specs/blob/master/identify/README.md#identify

> The peer being identified responds by returning an Identify message and closes the stream.

This shows up when interacting with a go-libp2p node as go-libp2p waits for the stream to close before finishing reading the messages: https://github.com/libp2p/go-libp2p/blob/master/p2p/protocol/identify/id.go#L455.
  • Loading branch information
MarcoPolo committed Oct 14, 2022
1 parent e10eea2 commit a74d22a
Showing 1 changed file with 5 additions and 12 deletions.
17 changes: 5 additions & 12 deletions src/identify/index.ts
Expand Up @@ -2,7 +2,6 @@ import { logger } from '@libp2p/logger'
import errCode from 'err-code'
import * as lp from 'it-length-prefixed'
import { pipe } from 'it-pipe'
import drain from 'it-drain'
import first from 'it-first'
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
import { multiaddr, protocols } from '@multiformats/multiaddr'
Expand Down Expand Up @@ -191,16 +190,14 @@ export class IdentifyService implements Startable {
// make stream abortable
const source = abortableDuplex(stream, timeoutController.signal)

await pipe(
await source.sink(pipe(
[Identify.encode({
listenAddrs,
signedPeerRecord,
protocols
})],
lp.encode(),
source,
drain
)
lp.encode()
))
} catch (err: any) {
// Just log errors
log.error('could not push identify update to peer', err)
Expand Down Expand Up @@ -430,12 +427,8 @@ export class IdentifyService implements Startable {
// make stream abortable
const source = abortableDuplex(stream, timeoutController.signal)

await pipe(
[message],
lp.encode(),
source,
drain
)
const msgWithLenPrefix = pipe([message], lp.encode())
await source.sink(msgWithLenPrefix)
} catch (err: any) {
log.error('could not respond to identify request', err)
} finally {
Expand Down

0 comments on commit a74d22a

Please sign in to comment.