Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Re-work thread safety mechanisms for flush process #611

Merged
merged 10 commits into from
Jul 19, 2023

Conversation

jaredmixpanel
Copy link
Contributor

@jaredmixpanel jaredmixpanel commented Jul 18, 2023

Hopefully a fix for #578

if let requestData = requestData {
let semaphore = DispatchSemaphore(value: 0)
#if os(iOS)
flushBatchQueue.sync {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

put all calls to flushQueueInBatches into sync queue in case it's being called from multiple threads

#endif // os(iOS)
let success = flushRequest.sendRequest(requestData,
type: type,
useIP: useIPAddressForGeoLocation)
Copy link
Contributor Author

@jaredmixpanel jaredmixpanel Jul 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move the semaphore into sendRequest and make it synchronous from the perspective of flushQueueInBatches and return a boolean indicating success.

})
_ = semaphore.wait(timeout: DispatchTime.distantFuture)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe, we should change this timeout to some number of minutes?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, agreed.

@zihejia zihejia self-requested a review July 18, 2023 23:13
if let requestData = requestData {
let semaphore = DispatchSemaphore(value: 0)
#if os(iOS)
flushBatchQueue.sync {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

calling .sync could potentially cause the deadlock.

Copy link
Contributor

@zihejia zihejia left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@jaredmixpanel jaredmixpanel changed the title [WIP] Re-work thread safety mechanisms for flush process Re-work thread safety mechanisms for flush process Jul 19, 2023
@jaredmixpanel jaredmixpanel merged commit 48e54e1 into master Jul 19, 2023
@RamblinWreck77
Copy link
Contributor

Sorry to comment on an already-merged MR, but are you guys open to a Swift Concurrency/async-await refactor?

I have a bunch of concerns with Flush.swift:

  • flushIntervalReadWriteLock is on a .utility QoS concurrent queue

  • DispatchQueue.main.async is forcing work to the main thread, when mixpanel IMO should pretty much never touch the main thread

  • I suspect there's a priority inversion there locking with a lower QoS and doing work on main

  • calling out to objc #selectors inside a DispatchQueue.main block that weakly calls a delegate function sets off my spidey-sense

It seems like a lot of what you want to accomplish could be better handled in pure-swift with:

  • Swift Concurrency Tasks

  • async-await

  • Swift Actors for mutable state isolation

@zihejia @jaredmixpanel I can see you guys support back to iOS 11 (!!) in the Package.swift

Would you consider raising the minimum OS to iOS 13.0, or even 14.0? Apple back-ported Swift Concurrency to 13.0, and that unlocks a whole world of feature improvements.

If some PM gripes about legacy users, you could always fork this repo (as it is fairly stable) as mixpanel_legacy, and leave it up who insist on using Paleolithic-era OSes while the main repo forges ahead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants