Skip to content

Commit

Permalink
fix: upgrader should abort stream on close based on inbound limits (l…
Browse files Browse the repository at this point in the history
  • Loading branch information
maschad committed Apr 26, 2023
1 parent b5a1219 commit 79e82d5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
6 changes: 3 additions & 3 deletions src/upgrader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ function findIncomingStreamLimit (protocol: string, registrar: Registrar): numbe
function findOutgoingStreamLimit (protocol: string, registrar: Registrar): number | undefined {
try {
const { options } = registrar.getHandler(protocol)

return options.maxOutboundStreams
} catch (err: any) {
if (err.code !== codes.ERR_NO_HANDLER_FOR_PROTOCOL) {
Expand Down Expand Up @@ -392,9 +391,10 @@ export class DefaultUpgrader implements Upgrader {
const streamCount = countStreams(protocol, 'inbound', connection)

if (streamCount === incomingLimit) {
muxedStream.abort(new CodeError(`Too many inbound protocol streams for protocol "${protocol}" - limit ${incomingLimit}`, codes.ERR_TOO_MANY_INBOUND_PROTOCOL_STREAMS))
const err = new CodeError(`Too many inbound protocol streams for protocol "${protocol}" - limit ${incomingLimit}`, codes.ERR_TOO_MANY_INBOUND_PROTOCOL_STREAMS)
muxedStream.abort(err)

return
throw err
}

// after the handshake the returned stream can have early data so override
Expand Down
17 changes: 11 additions & 6 deletions test/upgrading/upgrader.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import { DefaultComponents } from '../../src/components.js'
import { StubbedInstance, stubInterface } from 'sinon-ts'
import { yamux } from '@chainsafe/libp2p-yamux'
import { EventEmitter } from '@libp2p/interfaces/events'
import { logger } from '@libp2p/logger'

const addrs = [
multiaddr('/ip4/127.0.0.1/tcp/0'),
Expand Down Expand Up @@ -664,7 +665,10 @@ describe('libp2p.upgrader', () => {
webSockets()
],
streamMuxers: [
yamux()
yamux({
log: logger('yamux:local')
})
// mplex()
],
connectionEncryption: [
plaintext()
Expand All @@ -678,7 +682,10 @@ describe('libp2p.upgrader', () => {
webSockets()
],
streamMuxers: [
yamux()
yamux({
log: logger('yamux:remote')
})
// mplex()
],
connectionEncryption: [
plaintext()
Expand Down Expand Up @@ -726,8 +733,7 @@ describe('libp2p.upgrader', () => {
webSockets()
],
streamMuxers: [
yamux(),
mplex()
yamux()
],
connectionEncryption: [
plaintext()
Expand All @@ -741,8 +747,7 @@ describe('libp2p.upgrader', () => {
webSockets()
],
streamMuxers: [
yamux(),
mplex()
yamux()
],
connectionEncryption: [
plaintext()
Expand Down

0 comments on commit 79e82d5

Please sign in to comment.