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

Sentry.close must be called in an isolate otherwise it won't shut down #2625

Closed
buenaflor opened this issue Jan 28, 2025 · 7 comments · Fixed by getsentry/sentry-docs#13218
Closed

Comments

@buenaflor
Copy link
Contributor

Description

"I'm calling Sentry.init in my isolate (the non-flutter variant) and found my isolate wouldn't shutdown until I added Sentry.close as the last line in the isolate."

Let's verify this first and then document it

@github-project-automation github-project-automation bot moved this to Needs Discussion in Mobile SDKs Jan 28, 2025
@buenaflor buenaflor moved this from Needs Discussion to Backlog in Mobile SDKs Jan 30, 2025
@denrase denrase moved this from Backlog to In Investigation in Mobile SDKs Feb 4, 2025
@denrase
Copy link
Collaborator

denrase commented Feb 4, 2025

I tested with the following code.

import 'dart:isolate';
import 'package:sentry/sentry.dart';

Future<void> main(List<String> arguments) async {
  print('before sentry isolate');
  await Isolate.run(_sentryIsolate);
  print('after sentry isolate');
}

Future<void> _sentryIsolate() async {
  print('sentry isolate start');
  await Sentry.init((options) {
    options.dsn = 'https://[email protected]/5428562';
  });
  // await Sentry.close();
  print('sentry isolate end');
}

It makes no difference if we close sentry or not, the output is the same in both cases.

denis@MacBook-Pro metrics % ./perf_test_console_sentry.bin
before sentry isolate
sentry isolate start
sentry isolate end
after sentry isolate

@buenaflor Can we reach the user somehow to get a sample where this is occurring?

@denrase denrase moved this from In Investigation to Needs More Information in Mobile SDKs Feb 4, 2025
@buenaflor buenaflor moved this from Needs More Information to Needs Investigation in Mobile SDKs Feb 11, 2025
@buenaflor buenaflor moved this from Needs Investigation to Needs More Information in Mobile SDKs Feb 11, 2025
@buenaflor
Copy link
Contributor Author

cc @bsutton

@bsutton
Copy link

bsutton commented Feb 11, 2025

here is a link to the source file that had the problem.

https://github.com/bsutton/hmb/blob/wip/lib/database/management/backup_providers/zip_isolate.dart

@getsantry getsantry bot moved this from Waiting for: Community to Waiting for: Product Owner in GitHub Issues with 👀 3 Feb 11, 2025
@denrase
Copy link
Collaborator

denrase commented Mar 5, 2025

@bsutton Could reproduce with this minimal code:

import 'dart:async';
import 'dart:isolate';

import 'package:sentry_flutter/sentry_flutter.dart';

Future<void> sentryInIsolate() async {
  final receivePort = ReceivePort();
  final errorPort = ReceivePort();
  final exitPort = ReceivePort();

  await Isolate.spawn<SendPort>(
    _runSentry,
    receivePort.sendPort,
    onError: errorPort.sendPort,
    onExit: exitPort.sendPort,
  );

  final completer = Completer<void>();

  receivePort.listen((message) {
    print("receivePort $message");
  });

  errorPort.listen((error) {
    print("onError");
    if (!completer.isCompleted) {
      completer.completeError(error as Object);
    }
  });

  exitPort.listen((message) {
    print("onExit");
    if (!completer.isCompleted) {
      completer.complete();
    }
  });

  await completer.future;

  receivePort.close();
  errorPort.close();
  exitPort.close();
}

Future<void> _runSentry(SendPort sendPort) async {
  sendPort.send("before sentry init"); 
  await Sentry.init((options) {
    options
      ..dsn =
          'https://17bb41df4a5343530bfcb92553f4c5a7@o4507706035994624.ingest.us.sentry.io/4507706038157312'
      ..tracesSampleRate = 1.0;
  });
  sendPort.send("after sentry init"); 

  sendPort.send("before sentry close"); 
  await Sentry.close(); // If not done, the isolate will not close.
  sendPort.send("after sentry close"); 
}

Since sentry is holding onto resources (http, connections, etc.) the correct usage in isolates is to close it at the end. We will update this in our documentation.

@denrase
Copy link
Collaborator

denrase commented Mar 5, 2025

Found out that this works if we do not add the IsolateErrorIntegration, so maybe there is something we can do here after all, will investigate further.

@denrase
Copy link
Collaborator

denrase commented Mar 10, 2025

@bsutton Just tested and the isolate is closing with sentry 8.13.2. Please upgrade and report back if the issue still persists.

@denrase
Copy link
Collaborator

denrase commented Mar 26, 2025

I added a test and we indeed need to close sentry to make the isolate complete. We should document this behaviour.

@denrase denrase moved this from Needs More Information to Needs Review in Mobile SDKs Mar 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Archived in project
Status: Done
Development

Successfully merging a pull request may close this issue.

5 participants