Skip to content

Commit 24e57fa

Browse files
jboulter11Jim Boulter
andauthored
Fix overlocking on request creation block (#447)
Co-authored-by: Jim Boulter <jboulter@dropbox.com>
1 parent ce29a21 commit 24e57fa

1 file changed

Lines changed: 12 additions & 1 deletion

File tree

Source/SwiftyDropbox/Shared/Handwritten/Request+TokenRefresh.swift

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,8 +241,19 @@ class RequestWithTokenRefresh: ApiRequest {
241241
self.filesAccess = filesAccess
242242

243243
let setTask = {
244+
// It is important to run this block, which may do a lot of work outside the SDK,
245+
// before getting the lock. We have seen deadlock scenarios like the following:
246+
// 1. Enter thread safe code behind a lock in your app
247+
// 2. Start a request from behind that lock in your app
248+
// 3. We would get the access state lock below before executing requestCreation()
249+
// 4. This request creation block eventually goes back out to your app's code to get headers
250+
// 5. Once in your app's code, it needs to go into the code locked in step 1 to get info for headers
251+
// 6. Deadlock
252+
//
253+
// Because we don't have contention between threads to run the requestCreation() block,
254+
// it can be omitted from the locked section.
255+
let task = requestCreation()
244256
self.accessStateWithLock { state in
245-
let task = requestCreation()
246257
state.request = task
247258
state.handleRequestCreation()
248259
}

0 commit comments

Comments
 (0)