Skip to content

Commit aae91f6

Browse files
committed
bridgev2: split stopping matrix connector
Also fix stopping the websocket in the default Matrix connector
1 parent 95a7e94 commit aae91f6

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

bridgev2/bridge.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ func (br *Bridge) Stop() {
319319
func (br *Bridge) stop(isRunOnce bool) {
320320
br.Log.Info().Msg("Shutting down bridge")
321321
br.stopBackfillQueue.Set()
322-
br.Matrix.Stop()
322+
br.Matrix.PreStop()
323323
if !isRunOnce {
324324
br.cacheLock.Lock()
325325
var wg sync.WaitGroup
@@ -330,6 +330,7 @@ func (br *Bridge) stop(isRunOnce bool) {
330330
br.cacheLock.Unlock()
331331
wg.Wait()
332332
}
333+
br.Matrix.Stop()
333334
if stopNet, ok := br.Network.(StoppableNetwork); ok {
334335
stopNet.Stop()
335336
}

bridgev2/matrix/connector.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,13 +233,37 @@ func (br *Connector) GetCapabilities() *bridgev2.MatrixCapabilities {
233233
return br.Capabilities
234234
}
235235

236-
func (br *Connector) Stop() {
236+
func sendStopSignal(ch chan struct{}) {
237+
if ch != nil {
238+
select {
239+
case ch <- struct{}{}:
240+
default:
241+
}
242+
}
243+
}
244+
245+
func (br *Connector) PreStop() {
237246
br.stopping = true
238247
br.AS.Stop()
248+
if stopWebsocket := br.AS.StopWebsocket; stopWebsocket != nil {
249+
stopWebsocket(appservice.ErrWebsocketManualStop)
250+
}
251+
sendStopSignal(br.wsStopPinger)
252+
sendStopSignal(br.wsShortCircuitReconnectBackoff)
253+
}
254+
255+
func (br *Connector) Stop() {
239256
br.EventProcessor.Stop()
240257
if br.Crypto != nil {
241258
br.Crypto.Stop()
242259
}
260+
if wsStopChan := br.wsStopped; wsStopChan != nil {
261+
select {
262+
case <-wsStopChan:
263+
case <-time.After(4 * time.Second):
264+
br.Log.Warn().Msg("Timed out waiting for websocket to close")
265+
}
266+
}
243267
}
244268

245269
var MinSpecVersion = mautrix.SpecV14

bridgev2/matrixinterface.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ type MatrixCapabilities struct {
3131
type MatrixConnector interface {
3232
Init(*Bridge)
3333
Start(ctx context.Context) error
34+
PreStop()
3435
Stop()
3536

3637
GetCapabilities() *MatrixCapabilities

0 commit comments

Comments
 (0)