Skip to content

Commit a74d22a

Browse files
authored
fix: close stream after sending identify (#1424)
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.
1 parent e10eea2 commit a74d22a

File tree

1 file changed

+5
-12
lines changed

1 file changed

+5
-12
lines changed

src/identify/index.ts

+5-12
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { logger } from '@libp2p/logger'
22
import errCode from 'err-code'
33
import * as lp from 'it-length-prefixed'
44
import { pipe } from 'it-pipe'
5-
import drain from 'it-drain'
65
import first from 'it-first'
76
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
87
import { multiaddr, protocols } from '@multiformats/multiaddr'
@@ -191,16 +190,14 @@ export class IdentifyService implements Startable {
191190
// make stream abortable
192191
const source = abortableDuplex(stream, timeoutController.signal)
193192

194-
await pipe(
193+
await source.sink(pipe(
195194
[Identify.encode({
196195
listenAddrs,
197196
signedPeerRecord,
198197
protocols
199198
})],
200-
lp.encode(),
201-
source,
202-
drain
203-
)
199+
lp.encode()
200+
))
204201
} catch (err: any) {
205202
// Just log errors
206203
log.error('could not push identify update to peer', err)
@@ -430,12 +427,8 @@ export class IdentifyService implements Startable {
430427
// make stream abortable
431428
const source = abortableDuplex(stream, timeoutController.signal)
432429

433-
await pipe(
434-
[message],
435-
lp.encode(),
436-
source,
437-
drain
438-
)
430+
const msgWithLenPrefix = pipe([message], lp.encode())
431+
await source.sink(msgWithLenPrefix)
439432
} catch (err: any) {
440433
log.error('could not respond to identify request', err)
441434
} finally {

0 commit comments

Comments
 (0)