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

Finish a transaction never returns on iOS after being in background #2007

Closed
NikHomann opened this issue Apr 22, 2024 · 24 comments · Fixed by #2028
Closed

Finish a transaction never returns on iOS after being in background #2007

NikHomann opened this issue Apr 22, 2024 · 24 comments · Fixed by #2028
Assignees

Comments

@NikHomann
Copy link

Platform

Flutter Mobile

Obfuscation

Disabled

Debug Info

Disabled

Doctor

[√] Flutter (Channel stable, 3.19.4, on Microsoft Windows [Version 10.0.19045.4291], locale de-DE)
• Flutter version 3.19.4 on channel stable at C:\Users\Niklas\fvm\versions\3.19.4
• Upstream repository https://github.com/flutter/flutter.git
• Framework revision 68bfaea224 (5 weeks ago), 2024-03-20 15:36:31 -0700
• Engine revision a5c24f538d
• Dart version 3.3.2
• DevTools version 2.31.1

Version

7.18.0

Steps to Reproduce

How to create a reproducible example:

  1. Create new flutter project
  2. Add sentry packages
  3. Initialize Sentry for instance like here: https://docs.sentry.io/platforms/flutter/#configure
  4. Add a await Sentry.startTransaction("test", "test").finish() somewhere, for instance in the basic flutter example before counter is incremented
  5. Go through the steps below to see the problem

Steps to Reproduce:

  1. Run an app with Sentry in release mode on an iOS device
  2. Optionally, enable "Fast App Termination" in the developer settings to not have to wait for so long
  3. Put the app in the background and wait for some time (~30min with "Fast App Termination")
  4. Open the app and somehow start and finish a transaction

If you downgrade to version 7.10.1 (there might be others, but this is the version I was using before) it works as expected.

Expected Result

The transaction.finish(...) returns after a short amount of time and the transaction is logged in Sentry.

Actual Result

The transaction.finish(...) never returns and the transaction is not logged in Sentry.

Are you willing to submit a PR?

No

@buenaflor
Copy link
Contributor

Hi thanks for raising this issue, before going deeper into this: do you know if this is also reproducible with a shorter background time?

@NikHomann
Copy link
Author

Hi thanks for raising this issue, before going deeper into this: do you know if this is also reproducible with a shorter background time?

I am not sure and do not have exact numbers, but I think sometimes it happened also for shorter background times. However, the ~30min seemed to give me the problem reliably (nearly) every time.

@buenaflor
Copy link
Contributor

Got it, thanks for the issue, we'll try to reproduce. If you have any more info lmk

@kuhnroyal
Copy link
Contributor

kuhnroyal commented May 3, 2024

I believe I found the culprit, the NativeAppStartIntegration or NativeAppStartEventProcessor hangs and the future here never completes because options.autoAppStart is set to false.

I believe the NativeAppStartIntegration is incorrectly added to active integrations in some cases.
If autoAppStart is false and SentryFlutter.setAppStartEnd is never called, then this gets stuck.

@NikHomann Not fully sure that this is the same reason for your case. I am manually starting a new isolate and initialize Sentry with autoAppStart = false.

Workaround:

// NativeAppStartIntegration is not exported
// ignore: implementation_imports
import 'package:sentry_flutter/src/integrations/native_app_start_integration.dart';

 options.removeIntegration(options.integrations.firstWhere((e) => e is NativeAppStartIntegration));

@buenaflor
Copy link
Contributor

we didn't account for setAppStartEnd so we need to modify that, this should be it thx for investigating

@kuhnroyal
Copy link
Contributor

I am still experiencing 30 sec timeouts here.
There should probably be a way to disable appStart tracking completely or the processor needs to just return null if the end time it is not set yet. The 30 secs default timeout are problematic.

@buenaflor
Copy link
Contributor

hey, I'll take another look, sorry about that, I'll try to get to it today or tomorrow since next week will be a bit slower because of droid/fluttercon

@kuhnroyal
Copy link
Contributor

No worries, see you in Berlin 👋

@kuhnroyal
Copy link
Contributor

Not completely sure but I think the addPostFrameCallback is not triggering in my case because it is started from a background isolate (workmanager). I don't really need the appstart information but it is not possible to disable the integration.

@buenaflor
Copy link
Contributor

I think you can do something like

options.integrations.removeWhere((integration) => 
      integration is IntegrationYouWantToRemove);

Which should be the NativeAppStartIntegration, I'm currently on my phone so I can't verify the correctness of the code but afaik it should be possible to remove

@buenaflor
Copy link
Contributor

hey this code will help you remove/disable it:

final integration = options.integrations.firstWhere(
    (element) => element.toString().contains('NativeAppStartIntegration'));
options.removeIntegration(integration);

@kuhnroyal
Copy link
Contributor

Yea, I restored my workaround from #2007 (comment) which I removed when 8.2.0 was released. Obviously I didn't test it  🤣

@buenaflor
Copy link
Contributor

To me it looks like setAppStartEnd just adds more complexity all in all. imo it should be deprecated.

the flutter sdk is also the only SDK that has this API.

@buenaflor
Copy link
Contributor

buenaflor commented Jun 28, 2024

I am still experiencing 30 sec timeouts here.

by the way do have a way to reproduce it reliably? or is it just something that happens once in a while

@buenaflor
Copy link
Contributor

#2140 I've added a fix that will not wait for getAppStartInfo if autoAppstarts is false and setAppStartEnd has not been called

@kuhnroyal
Copy link
Contributor

I am still experiencing 30 sec timeouts here.

by the way do have a way to reproduce it reliably? or is it just something that happens once in a while

I am starting a new isolate via https://pub.dev/packages/flutter_isolate and call SentryFlutter.init. Since there is no UI, my assumption is, that the postFrameCallback is never triggered.

@buenaflor
Copy link
Contributor

ah yeah looks like it

@shinayser
Copy link

I am also experiencing this issue on Android on some of my testings. Is a fix for it available?
The workaround is safe to use or may cause some undesirable issues?

@buenaflor
Copy link
Contributor

@shinayser

the temporary workaround with disabling the app start integration is safe to use. this is only used to instrument the app start times

@shinayser
Copy link

@buenaflor This is exactly what I was trying to do when I figured out the bug 😢

@buenaflor
Copy link
Contributor

@shinayser did you successfully remove the integration?

@shinayser
Copy link

shinayser commented Jul 5, 2024

@buenaflor So, my problem is that I am actually starting sentry in the native side (on Android, Application.kt class) and using the parameter options.autoInitializeNativeSdk=true on the flutter side.

Because of that, when removing the integration and caling the transation.fnish() throws an error on sentry saying that "Sentry sdk isn't started".

In resume, this workaround works but if you are using the native sdk startup you wil be in trouble and sentry will not be able to finish transactions.

@buenaflor
Copy link
Contributor

@shinayser and you're using the SentryNavigatorObserver correct?

@shinayser
Copy link

@shinayser and you're using the SentryNavigatorObserver correct?

Yes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Archived in project
Archived in project
5 participants