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

Twitter dont block main on file ops #7298

Merged
merged 4 commits into from
Jan 21, 2021
Merged

Twitter dont block main on file ops #7298

merged 4 commits into from
Jan 21, 2021

Conversation

jhollida24
Copy link
Contributor

Hello. We see the code referenced here blocking the main thread at startup and causing our watchdog system to kill the app. Synchronously waiting for filesystem operations to complete from the main thread can be safely removed.

image (3)

Jeff Holliday added 3 commits January 13, 2021 15:54
On initialization, a promise is created that synchronously
waits on an NSOperationQueue with file ops in it. Re-work
this so that the promise is asynchronously fulfilled with
an operation that is inserted into the queue, and then
dispatched back to the main queue when finished.
@google-cla
Copy link

google-cla bot commented Jan 14, 2021

Thanks for your pull request. It looks like this may be your first contribution to a Google open source project (if not, look below for help). Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

📝 Please visit https://cla.developers.google.com/ to sign.

Once you've signed (or fixed any issues), please reply here with @googlebot I signed it! and we'll verify it.


What to do if you already signed the CLA

Individual signers
Corporate signers

ℹ️ Googlers: Go here for more info.

@google-cla google-cla bot added the cla: no label Jan 14, 2021
@jhollida24
Copy link
Contributor Author

@googlebot I signed it!

@google-cla google-cla bot added cla: yes and removed cla: no labels Jan 14, 2021
@jhollida24
Copy link
Contributor Author

Hi! We'd like to get a build of Firebase into the Twitter app release that cuts its branch this week. We can cherry-pick it if necessary, but I thought I would give a friendly nudge here, first. We have another PR here that the same applies to: #7302

Thanks!

Copy link
Contributor

@maksymmalyhin maksymmalyhin left a comment

Choose a reason for hiding this comment

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

@jhollida24 Thank you for your contribution! The change looks good to me.

@samedson @elenadoty Could you please provide review from your end.

@@ -410,10 +410,18 @@ - (FBLPromise *)deleteUnsentReports {
NSOperationQueue *__weak queue = _operationQueue;
FBLPromise *__weak unsentReportsHandled = _unsentReportsHandled;
promise = [promise then:^id _Nullable(NSNumber *_Nullable value) {
Copy link
Contributor

Choose a reason for hiding this comment

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

optional: I wonder if a version below would be a bit more readable?

promise = [promise onQueue:dispatch_get_global_queue(QOS_CLASS_UTILITY, 0) then:^id _Nullable(NSNumber *_Nullable value) {
  [queue waitUntilAllOperationsAreFinished];
  return value;
}
.then(^id _Nullable(NSNumber *_Nullable value) {
  [unsentReportsHandled fulfill:nil];
  return value;
})

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm happy to make changes that you would like, but I think the GCD folks at Apple would discourage this. Dispatching work to the global concurrent queues that blocks causes dispatch worker threads to be created (for the next block coming through), which is then torn down when it's unblocked. An additional operation on an existing (serial) queue doesn't expand the total concurrency.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I do agree with you it's more readable, and it's nice that it doesn't rely on the automatic flatMap behavior of FBLPromise.

Copy link
Contributor

Choose a reason for hiding this comment

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

Good point on blocking a global queue - agreed it's not the best idea. Current version looks good to me from logic perspective. I'll think how we can improve readability and comment if I have any ideas.

@@ -410,10 +410,18 @@ - (FBLPromise *)deleteUnsentReports {
NSOperationQueue *__weak queue = _operationQueue;
FBLPromise *__weak unsentReportsHandled = _unsentReportsHandled;
promise = [promise then:^id _Nullable(NSNumber *_Nullable value) {
[queue waitUntilAllOperationsAreFinished];
Copy link
Contributor

Choose a reason for hiding this comment

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

@samedson @elenadoty Do you remember if blocking UI thread is intentional here or just an accident?

Copy link
Contributor

Choose a reason for hiding this comment

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

I believe this was an oversight. It shouldn't be a problem to delete in the background

@paulb777 paulb777 merged commit 3ca9dca into firebase:master Jan 21, 2021
@jhollida24 jhollida24 deleted the twitter-dont-block-main-on-file-ops branch January 29, 2021 18:12
@firebase firebase locked and limited conversation to collaborators Feb 21, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants