matrix-org / matrix-org/matrix-js-sdk
Client (or sync) wont stop, even with .stopClient()
Nobody has claimed this yet.
- Dominant language
- TypeScript
- Stars
- 2.2k
- Forks
- 704
- Avg merge
- 1d 20h
- Merged PRs (30d)
- 40
Description
Hello,
I am trying to use this module to send a notification at the end of a script. It works, except that the client only stops several minutes after calling the `.stopClient()` function. So my program is still awake during this time.
The program blocks after
```
stopping OutgoingRoomKeyRequestManager
stopping MatrixClient
SyncApi.stop
```
And finally after many minutes I get `Sync no longer running: exiting` and the program finally stops.
How can I force the client (and the synchronization) to stop immediately?
Here is the code i'm using:
```javascript
import * as sdk from 'matrix-js-sdk'
import { logger } from 'matrix-js-sdk/lib/logger.js'
import { ClientEvent } from 'matrix-js-sdk'
import Olm from "olm/olm_legacy.js"
import { LocalStorage } from 'node-localstorage'
import { LocalStorageCryptoStore } from 'matrix-js-sdk/lib/crypto/store/localStorage-crypto-store.js'
// temp fix : https://github.com/matrix-org/matrix-js-sdk/issues/2415#issuecomment-1141246410
import request from "request"
///
global.Olm = Olm
const localStorage = new LocalStorage('./store/matrix')
export class Matrix {
matrixClient
connected
constructor(url, user, token) {
this.connected = false
// temp fix : https://github.com/matrix-org/matrix-js-sdk/issues/2415#issuecomment-1141246410
sdk.request(request)
///
this.matrixClient = sdk.createClient({
deviceId: "Streaks Server",
baseUrl: url,
accessToken: token,
userId: user,
sessionStore: new sdk.MemoryStore({ localStorage }),
cryptoStore: new LocalStorageCryptoStore(localStorage)
})
}
async connect() {
if (this.connected)
return
//logger.setLevel(logger.levels.ERROR)
await this.matrixClient.initCrypto()
await this.matrixClient.startClient()
await new Promise((resolve, _reject) => {
this.matrixClient.once(ClientEvent.Sync, () => {
// Send encrypted message, even if member isn't trusted
this.matrixClient.setGlobalErrorOnUnknownDevices(false)
this.connected = true
resolve()
})
})
}
disconnect() {
if (!this.connected)
return
this.matrixClient.stopClient()
}
async sendMessage(roomID, message) {
await this.matrixClient.joinRoom(roomID)
await this.matrixClient.uploadKeys()
await this.matrixClient.sendTextMessage(roomID, message)
}
}
const config = {
matrix: {
url: process.env['MATRIX_URL'],
user: process.env['MATRIX_USER'],
token: process.env['MATRIX_TOKEN'],
roomID: process.env['MATRIX_ROOM']
}
}
let matrix = new Matrix(config.matrix.url, config.matrix.user, config.matrix.token)
await matrix.connect()
await matrix.sendMessage(config.matrix.roomID, "Hello world!")
matrix.disconnect()
```
[Full logs](https://github.com/matrix-org/matrix-js-sdk/files/8993427/logs.txt)
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by tracing MatrixClient.stopClient through the SyncApi.stop entry point shown in the logs, then compare it with the connect and startClient flow in the provided example. Reproduce the delayed exit and identify which active synchronization or request operation remains open. Done means stopping the client also stops synchronization promptly and the script exits without waiting several minutes.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript
- Domain
- api, networking
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100