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

use core background fetch, use .default qos #2071

Merged
merged 4 commits into from
Feb 9, 2024

Conversation

r10s
Copy link
Member

@r10s r10s commented Feb 8, 2024

this PR aims to improve the existing background fetch further by:

  • use the new dc_accounts_background_fetch() api which was explicitly made for this purpose

  • changing qos ("quality of service") of background task to .default instead of .background - while the latter sounds as if it is correct, it was probably never the intention as .background may be delayed by a loooong time according to stackoverflow - but also by our own observations (in theory, there is also .utility, however, finer granulation is probably not needed, i assume anyways that there is some additional per-app-weight)

the "one second sleep" is untouched for now, in fact, you'll see some core activity between "finishing fetch" and "fetch done", at least in the simulator, maybe in "real background" where re-start IO is not needed, it is really superfluous. also, in theory, swift could have started short-living threads from events need to finish. so, let's do not have too many moving targets and leave that as is for now

.background is probably not what we want - this qos may have long delays,
apple is a bit opaque in its description,
but stackoverflow is pretty concrete (https://stackoverflow.com/questions/53141829/performance-of-background-queue-in-swift)
and we even have a comment in our code ("may be delayed by tens of minutes").

this also fixes the warning
"Thread running at User-initiated quality-of-service class waiting on a lower QoS thread"
as before the background was .default while the event handler was .backgound

potentially, this change could explain several issues :)
@r10s r10s mentioned this pull request Feb 8, 2024
@r10s r10s added the notify label Feb 8, 2024
Copy link
Member

@Simon-Laux Simon-Laux left a comment

Choose a reason for hiding this comment

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

Looks great.

Can't say anything about the qos stuff, because it's outside of my area of expertise, so maybe wait for zeitschlag's opinion or we learn by trying it out.


// wait for DC_EVENT_ACCOUNTS_BACKGROUND_FETCH_DONE,
// marking the end of events needed to being processed.
// as IO was not started, random events caused by other reasons are not added.
Copy link
Member

Choose a reason for hiding this comment

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

maybe also mention that this event is also emitted in case of timeout, so it is clearer that we can not just do an early exit when the fetch timed-out (because the event is still emitted and we still might have a partial fetch that has some of the new message events)

Copy link
Member Author

Choose a reason for hiding this comment

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

i made the comment more precise, putting it more in code context and not just repeating documentation

main purpose of the comment is anyway to know that you need to grep for DC_EVENT_ACCOUNTS_BACKGROUND_FETCH_DONE :)

Copy link
Collaborator

@zeitschlag zeitschlag left a comment

Choose a reason for hiding this comment

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

looks reasonable to me. I added a few remarks and questions here and there, but nothing blocking 👍

DcCore/DcCore/DC/events.swift Show resolved Hide resolved
@@ -484,35 +484,33 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
pushToDebugArray("1")

// move work to non-main thread to not block UI (otherwise, in case we get suspended, the app is blocked totally)
// (we are using `qos: default` as `qos: .background` or `main.asyncAfter` may be delayed by tens of minutes)
// (we are using `qos: default` as `qos: .background` may be delayed by tens of minutes)
Copy link
Collaborator

Choose a reason for hiding this comment

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

From what I know, .background-work only gets done when the app is in background and stuff gets done with a very low priority. So it's not a background thread (as in "not main") while the app is running in foreground. So it's not only a question of time.

When we switch to iOS 13+, we could start using Task and async/await for background-stuff.

Copy link
Member

Choose a reason for hiding this comment

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

I would also like async/await because it would make working with jsonrpc nicer.
Also iOS 13 brings swiftUI but that seems to be not mature enough to be used from what I heard.
But on the other hand we are a messenger and people appreciate that we support older devices as well at least on android, for iPhone users it might be more common to upgrade to a new phone regularly - I suggest we look at the usage percentage take that into account.

// backgroundFetch() pauses IO as needed
if !self.dcAccounts.backgroundFetch(timeout: 20) {
logger.error("backgroundFetch failed")
self.pushToDebugArray("ERR2")
Copy link
Collaborator

Choose a reason for hiding this comment

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

What does ERR2 stand for?

Copy link
Member Author

Choose a reason for hiding this comment

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

it js just a random short string added to "Settings/Advaned/View Log". it makes sense only if you look at the code. once things are settled, we can remove this DebugArray completely

@r10s
Copy link
Member Author

r10s commented Feb 9, 2024

thanks for review and thoughts! i am also using the PR already since a day on my phone, and it seems to work in general :)

@r10s r10s merged commit 93c8ca5 into main Feb 9, 2024
1 check passed
@r10s r10s deleted the r10s/use-core-background-fetch branch February 9, 2024 09:35
This was referenced Feb 13, 2024
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.

4 participants