Skip to content

Commit

Permalink
fix(ping): gracefully handle remote stream closure.
Browse files Browse the repository at this point in the history
  • Loading branch information
privacyguard committed Nov 13, 2024
1 parent 7dcabb8 commit a1b0132
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions packages/protocol-ping/src/ping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,26 @@ export class PingService implements Startable, PingServiceInterface {

const buf = await bytes.read(PING_LENGTH, {
signal
}).catch((err: Error) => {
if (stream.readStatus === 'ready') throw err
return null
})

if (buf === null) break

await bytes.write(buf, {
signal
})
}
// close the stream
if (stream.status === 'open') {
const signal = AbortSignal.timeout(this.timeout)
signal.addEventListener('abort', () => {
stream?.abort(new TimeoutError('close timeout'))
})

await stream.close({ signal })
}
})
.catch(err => {
this.log.error('incoming ping from %p failed with error', data.connection.remotePeer, err)
Expand Down

0 comments on commit a1b0132

Please sign in to comment.