Skip to content

Commit cf669fd

Browse files
authored
Merge pull request #10878 from wordpress-mobile/issue/10872-fix-upload-starter-for-release-13-7
Fix crash in UploadStarter on Samsung devices with Android 5
2 parents c2a864e + db5472a commit cf669fd

File tree

2 files changed

+34
-40
lines changed

2 files changed

+34
-40
lines changed

RELEASE-NOTES.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
* Block editor: Images clickable for fullscreen preview
1111
* Fixed time displayed on Post Conflict Detected and Unpublished Revision dialogs
1212
* Block editor: Fix issue when removing image/page break block crashes the app
13+
* Fixed a crash on Samsung devices running Android 5 (Lollipop)
1314

1415
13.6
1516
-----

WordPress/src/main/java/org/wordpress/android/ui/uploads/UploadStarter.kt

Lines changed: 33 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import kotlinx.coroutines.CoroutineScope
1212
import kotlinx.coroutines.Job
1313
import kotlinx.coroutines.coroutineScope
1414
import kotlinx.coroutines.launch
15-
import kotlinx.coroutines.sync.Mutex
1615
import org.wordpress.android.analytics.AnalyticsTracker.Stat
1716
import org.wordpress.android.fluxc.Dispatcher
1817
import org.wordpress.android.fluxc.generated.UploadActionBuilder
@@ -63,13 +62,6 @@ class UploadStarter @Inject constructor(
6362
) : CoroutineScope {
6463
private val job = Job()
6564

66-
/**
67-
* When the app comes to foreground both `queueUploadFromAllSites` and `queueUploadFromSite` are invoked.
68-
* The problem is that they can run in parallel and `uploadServiceFacade.isPostUploadingOrQueued(it)` might return
69-
* out-of-date result and a same post is added twice.
70-
*/
71-
private val mutex = Mutex()
72-
7365
override val coroutineContext: CoroutineContext get() = job + bgDispatcher
7466

7567
/**
@@ -141,40 +133,41 @@ class UploadStarter @Inject constructor(
141133

142134
/**
143135
* This is meant to be used by [checkConnectionAndUpload] only.
136+
*
137+
* The method needs to be synchronized from the following reasons. When the app comes to foreground both
138+
* `queueUploadFromAllSites` and `queueUploadFromSite` are invoked. The problem is that they can run in parallel
139+
* and `uploadServiceFacade.isPostUploadingOrQueued(it)` might return out-of-date result and a same post is added
140+
* twice.
144141
*/
142+
@Synchronized
145143
private suspend fun upload(site: SiteModel) = coroutineScope {
146-
try {
147-
mutex.lock()
148-
postStore.getPostsWithLocalChanges(site)
149-
.asSequence()
150-
.map { post ->
151-
val action = uploadActionUseCase.getAutoUploadAction(post, site)
152-
Pair(post, action)
153-
}
154-
.filter { (_, action) ->
155-
action != DO_NOTHING
156-
}
157-
.toList()
158-
.forEach { (post, action) ->
159-
trackAutoUploadAction(action, post.status)
160-
AppLog.d(
161-
AppLog.T.POSTS,
162-
"UploadStarter for post title: ${post.title}, action: $action"
163-
)
164-
dispatcher.dispatch(
165-
UploadActionBuilder.newIncrementNumberOfAutoUploadAttemptsAction(
166-
post
167-
)
168-
)
169-
uploadServiceFacade.uploadPost(
170-
context = context,
171-
post = post,
172-
trackAnalytics = false
173-
)
174-
}
175-
} finally {
176-
mutex.unlock()
177-
}
144+
postStore.getPostsWithLocalChanges(site)
145+
.asSequence()
146+
.map { post ->
147+
val action = uploadActionUseCase.getAutoUploadAction(post, site)
148+
Pair(post, action)
149+
}
150+
.filter { (_, action) ->
151+
action != DO_NOTHING
152+
}
153+
.toList()
154+
.forEach { (post, action) ->
155+
trackAutoUploadAction(action, post.status)
156+
AppLog.d(
157+
AppLog.T.POSTS,
158+
"UploadStarter for post title: ${post.title}, action: $action"
159+
)
160+
dispatcher.dispatch(
161+
UploadActionBuilder.newIncrementNumberOfAutoUploadAttemptsAction(
162+
post
163+
)
164+
)
165+
uploadServiceFacade.uploadPost(
166+
context = context,
167+
post = post,
168+
trackAnalytics = false
169+
)
170+
}
178171
}
179172

180173
private fun trackAutoUploadAction(action: UploadAction, status: String) {

0 commit comments

Comments
 (0)