@@ -27,29 +27,13 @@ class MXBackgroundCryptoV2: MXBackgroundCrypto {
27
27
case missingCredentials
28
28
}
29
29
30
- private let machine : MXCryptoMachine
30
+ private let credentials : MXCredentials
31
+ private let restClient : MXRestClient
31
32
private let log = MXNamedLog ( name: " MXBackgroundCryptoV2 " )
32
33
33
- init ( credentials: MXCredentials , restClient: MXRestClient ) throws {
34
- guard
35
- let userId = credentials. userId,
36
- let deviceId = credentials. deviceId
37
- else {
38
- throw Error . missingCredentials
39
- }
40
-
41
- // `MXCryptoMachine` will load the same store as the main application meaning that background and foreground
42
- // sync services have access to the same data / keys. Possible race conditions are handled internally.
43
- machine = try MXCryptoMachine (
44
- userId: userId,
45
- deviceId: deviceId,
46
- restClient: restClient,
47
- getRoomAction: { [ log] _ in
48
- log. error ( " The background crypto should not be accessing rooms " )
49
- return nil
50
- }
51
- )
52
-
34
+ init ( credentials: MXCredentials , restClient: MXRestClient ) {
35
+ self . credentials = credentials
36
+ self . restClient = restClient
53
37
log. debug ( " Initialized background crypto module " )
54
38
}
55
39
@@ -66,6 +50,7 @@ class MXBackgroundCryptoV2: MXBackgroundCrypto {
66
50
log. debug ( details)
67
51
68
52
do {
53
+ let machine = try createMachine ( )
69
54
_ = try await machine. handleSyncResponse (
70
55
toDevice: syncResponse. toDevice,
71
56
deviceLists: syncResponse. deviceLists,
@@ -100,6 +85,7 @@ class MXBackgroundCryptoV2: MXBackgroundCrypto {
100
85
do {
101
86
// Rust-sdk does not expose api to see if we have a given session key yet (will be added in the future)
102
87
// so for the time being to find out if we can decrypt we simply perform the (more expensive) decryption
88
+ let machine = try createMachine ( )
103
89
_ = try machine. decryptRoomEvent ( event)
104
90
log. debug ( " Event ` \( eventId) ` can be decrypted with session ` \( sessionId) ` " )
105
91
return true
@@ -117,6 +103,7 @@ class MXBackgroundCryptoV2: MXBackgroundCrypto {
117
103
log. debug ( " Decrypting event ` \( eventId) ` " )
118
104
119
105
do {
106
+ let machine = try createMachine ( )
120
107
let decrypted = try machine. decryptRoomEvent ( event)
121
108
let result = try MXEventDecryptionResult ( event: decrypted)
122
109
event. setClearData ( result)
@@ -127,6 +114,30 @@ class MXBackgroundCryptoV2: MXBackgroundCrypto {
127
114
throw error
128
115
}
129
116
}
117
+
118
+ // `MXCryptoMachine` will load the same store as the main application meaning that background and foreground
119
+ // sync services have access to the same data / keys. The machine is not fully multi-thread and multi-process
120
+ // safe, and until this is resolved we open a new instance of `MXCryptoMachine` on each background operation
121
+ // to ensure we are always up-to-date with whatever has been written by the foreground process in the meanwhile.
122
+ // See https://github.com/matrix-org/matrix-rust-sdk/issues/1415 for more details.
123
+ private func createMachine( ) throws -> MXCryptoMachine {
124
+ guard
125
+ let userId = credentials. userId,
126
+ let deviceId = credentials. deviceId
127
+ else {
128
+ throw Error . missingCredentials
129
+ }
130
+
131
+ return try MXCryptoMachine (
132
+ userId: userId,
133
+ deviceId: deviceId,
134
+ restClient: restClient,
135
+ getRoomAction: { [ log] _ in
136
+ log. error ( " The background crypto should not be accessing rooms " )
137
+ return nil
138
+ }
139
+ )
140
+ }
130
141
}
131
142
132
143
#endif
0 commit comments