Skip to content

Commit d34642d

Browse files
authored
fix: only update pending incoming connections if connection accepted (#2790)
* fix: only update pending incoming connections if connection accepted The `afterUpgradeInbound` function decrements the count of pending (e.g. not upgraded yet) incoming connections, so only invoke that method if we actually accepted the connection, otherwise we decrement a counter that was never incremented. * chore: helpful to include implementation
1 parent a4b2db1 commit d34642d

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

packages/libp2p/src/upgrader.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -182,30 +182,32 @@ export class DefaultUpgrader implements Upgrader {
182182
* Upgrades an inbound connection
183183
*/
184184
async upgradeInbound (maConn: MultiaddrConnection, opts: UpgraderOptions = {}): Promise<Connection> {
185+
let accepted = false
186+
185187
try {
186188
this.metrics.dials?.increment({
187189
inbound: true
188190
})
189191

190-
const accept = await this.components.connectionManager.acceptIncomingConnection(maConn)
192+
accepted = await this.components.connectionManager.acceptIncomingConnection(maConn)
191193

192-
if (!accept) {
194+
if (!accepted) {
193195
throw new ConnectionDeniedError('connection denied')
194196
}
195197

196198
await this.shouldBlockConnection('denyInboundConnection', maConn)
197199

198-
const conn = await this._performUpgrade(maConn, 'inbound', opts)
199-
200-
return conn
200+
return await this._performUpgrade(maConn, 'inbound', opts)
201201
} catch (err) {
202202
this.metrics.errors?.increment({
203203
inbound: true
204204
})
205205

206206
throw err
207207
} finally {
208-
this.components.connectionManager.afterUpgradeInbound()
208+
if (accepted) {
209+
this.components.connectionManager.afterUpgradeInbound()
210+
}
209211
}
210212
}
211213

packages/libp2p/test/upgrading/upgrader.spec.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ import { type Components, defaultComponents } from '../../src/components.js'
2929
import { createLibp2p } from '../../src/index.js'
3030
import { DEFAULT_MAX_OUTBOUND_STREAMS } from '../../src/registrar.js'
3131
import { DefaultUpgrader } from '../../src/upgrader.js'
32-
import type { Libp2p, Connection, ConnectionProtector, Stream, ConnectionEncrypter, SecuredConnection, PeerId, StreamMuxer, StreamMuxerFactory, StreamMuxerInit, Upgrader, PrivateKey } from '@libp2p/interface'
32+
import type { Libp2p, Connection, ConnectionProtector, Stream, ConnectionEncrypter, SecuredConnection, PeerId, StreamMuxer, StreamMuxerFactory, StreamMuxerInit, Upgrader, PrivateKey, MultiaddrConnection } from '@libp2p/interface'
33+
import type { ConnectionManager } from '@libp2p/interface-internal'
3334

3435
const addrs = [
3536
multiaddr('/ip4/127.0.0.1/tcp/0'),
@@ -596,6 +597,22 @@ describe('Upgrader', () => {
596597
expect(localConnectionProtector.protect.callCount).to.equal(0, 'used local connection protector')
597598
expect(remoteConnectionProtector.protect.callCount).to.equal(0, 'used remote connection protector')
598599
})
600+
601+
it('should not decrement inbound pending connection count if the connection is denied', async () => {
602+
const connectionManager = stubInterface<ConnectionManager>()
603+
604+
// @ts-expect-error private field
605+
localUpgrader.components.connectionManager = connectionManager
606+
607+
const maConn = stubInterface<MultiaddrConnection>()
608+
609+
connectionManager.acceptIncomingConnection.resolves(false)
610+
611+
await expect(localUpgrader.upgradeInbound(maConn)).to.eventually.be.rejected
612+
.with.property('name', 'ConnectionDeniedError')
613+
614+
expect(connectionManager.afterUpgradeInbound.called).to.be.false()
615+
})
599616
})
600617

601618
describe('libp2p.upgrader', () => {

0 commit comments

Comments
 (0)