@@ -13,8 +13,13 @@ import androidx.work.WorkManager
13
13
import androidx.work.WorkerParameters
14
14
import dagger.assisted.Assisted
15
15
import dagger.assisted.AssistedInject
16
+ import kotlinx.coroutines.CancellationException
16
17
import kotlinx.coroutines.Deferred
18
+ import kotlinx.coroutines.GlobalScope
19
+ import kotlinx.coroutines.SupervisorJob
17
20
import kotlinx.coroutines.async
21
+ import kotlinx.coroutines.cancelAndJoin
22
+ import kotlinx.coroutines.launch
18
23
import kotlinx.coroutines.supervisorScope
19
24
import org.session.libsession.database.StorageProtocol
20
25
import org.session.libsession.database.userAuth
@@ -102,7 +107,10 @@ class BackgroundPollWorker @AssistedInject constructor(
102
107
103
108
try {
104
109
Log .v(TAG , " Performing background poll for ${requestTargets.joinToString { it.name }} ." )
105
- supervisorScope {
110
+ // The polling process is independent from the worker's lifecycle, once it's started
111
+ // it can't be safely cancelled.
112
+ // This is fixed on dev and we can discard the workaround.
113
+ GlobalScope .async(SupervisorJob ()) {
106
114
val tasks = mutableListOf<Deferred <* >>()
107
115
108
116
// DMs
@@ -115,6 +123,7 @@ class BackgroundPollWorker @AssistedInject constructor(
115
123
116
124
// FIXME: Using a job here seems like a bad idea...
117
125
BatchMessageReceiveJob (params).executeAsync(" background" )
126
+ Log .d(TAG , " Polling messages finished." )
118
127
}
119
128
}
120
129
@@ -127,6 +136,7 @@ class BackgroundPollWorker @AssistedInject constructor(
127
136
async {
128
137
Log .d(TAG , " Polling legacy group ${key.substring(0 , 8 )} ..." )
129
138
poller.poll(key)
139
+ Log .d(TAG , " Polling legacy group finished." )
130
140
}
131
141
}
132
142
}
@@ -142,6 +152,7 @@ class BackgroundPollWorker @AssistedInject constructor(
142
152
.apply { hasStarted = true }
143
153
.poll()
144
154
.await()
155
+ Log .d(TAG , " Polling Open group finished." )
145
156
}
146
157
}
147
158
}
@@ -151,6 +162,7 @@ class BackgroundPollWorker @AssistedInject constructor(
151
162
tasks + = async {
152
163
Log .d(TAG , " Polling all groups." )
153
164
groupPollerManager.pollAllGroupsOnce()
165
+ Log .d(TAG , " Polling groups finished." )
154
166
}
155
167
}
156
168
@@ -168,10 +180,14 @@ class BackgroundPollWorker @AssistedInject constructor(
168
180
if (caughtException != null ) {
169
181
throw caughtException
170
182
}
171
- }
183
+ }.await()
172
184
173
185
return Result .success()
174
- } catch (exception: Exception ) {
186
+ } catch (c: CancellationException ) {
187
+ Log .v(TAG , " Background poll cancelled" )
188
+ throw c
189
+ }
190
+ catch (exception: Exception ) {
175
191
Log .e(TAG , " Background poll failed due to error: ${exception.message} ." , exception)
176
192
return Result .retry()
177
193
}
0 commit comments