Skip to content

Commit

Permalink
fix: reset dial queue shut down controller on node restart (#2329)
Browse files Browse the repository at this point in the history
When a node is stopped and started we need to reset the dial queue
shutdown controller otherwise every dial attempt after a restart
will be rejected as the shutdown controller signal is still aborted.

Fixes #2188
  • Loading branch information
achingbrain authored Dec 20, 2023
1 parent 6fd681d commit cd8cafc
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
27 changes: 27 additions & 0 deletions packages/integration-tests/test/lifecycle.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { multiaddr } from '@multiformats/multiaddr'
import { createLibp2p } from 'libp2p'
import { createBaseOptions } from './fixtures/base-options.js'
import type { Libp2p } from '@libp2p/interface'

describe('lifecycle', () => {
let peer: Libp2p

afterEach(async () => {
await peer?.stop()
})

it('can dial a node after restarting', async () => {
const ma = multiaddr(process.env.RELAY_MULTIADDR)

peer = await createLibp2p(createBaseOptions())

await peer.dial(ma)

// stop and restart peer
await peer.stop()
await peer.start()

// once started, attempt to dial again
await peer.dial(ma)
})
})
7 changes: 6 additions & 1 deletion packages/libp2p/src/connection-manager/dial-queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export class DialQueue {
private readonly dialTimeout: number
private readonly inProgressDialCount?: Metric
private readonly pendingDialCount?: Metric
private readonly shutDownController: AbortController
private shutDownController: AbortController
private readonly connections: PeerMap<Connection[]>
private readonly log: Logger

Expand Down Expand Up @@ -138,11 +138,16 @@ export class DialQueue {
})
}

start (): void {
this.shutDownController = new AbortController()
}

/**
* Clears any pending dials
*/
stop (): void {
this.shutDownController.abort()
this.queue.clear()
}

/**
Expand Down
1 change: 1 addition & 0 deletions packages/libp2p/src/connection-manager/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,7 @@ export class DefaultConnectionManager implements ConnectionManager, Startable {
}
})

this.dialQueue.start()
this.autoDial.start()

this.started = true
Expand Down

0 comments on commit cd8cafc

Please sign in to comment.