kentcb / azure_application_insights

Dart client for Azure's Application Insights service

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Bad state: Cannot fire new event. Controller is already firing an event:

jonsamwell opened this issue · comments

Hi, thanks for this great library.

I am getting the below error quite a lot.

I/flutter (18167): ══╡ EXCEPTION CAUGHT BY COM.LOGITBOX.APP ╞══════════════════════════════════════════════════════════
I/flutter (18167): The following _Exception was thrown Zone error: Bad state: Cannot fire new event. Controller is
I/flutter (18167): already firing an event:
I/flutter (18167): Exception: Bad state: Cannot fire new event. Controller is already firing an event
I/flutter (18167):
I/flutter (18167): When the exception was thrown, this was the stack:
I/flutter (18167): #1      Logger._publish
package:logging/src/logger.dart:276
I/flutter (18167): #2      Logger.log
package:logging/src/logger.dart:200
I/flutter (18167): #3      Logger.fine
package:logging/src/logger.dart:230
I/flutter (18167): #4      TransmissionProcessor._transmit
package:azure_application_insights/src/processing.dart:192
I/flutter (18167): #5      TransmissionProcessor.process
package:azure_application_insights/src/processing.dart:166
I/flutter (18167): #6      BufferedProcessor.flush
package:azure_application_insights/src/processing.dart:109
I/flutter (18167): #7      BufferedProcessor.process
package:azure_application_insights/src/processing.dart:85
I/flutter (18167): #8      TelemetryClient._track
package:azure_application_insights/src/client.dart:156
I/flutter (18167): #9      TelemetryClient.trackTrace
package:azure_application_insights/src/client.dart:140
I/flutter (18167): #10     AzureAppInsightsTelemetryService.trackWarning
package:logitbox/…/services/azure_app_insights_telemetry_service.dart:97

It seems to happen when tracking warnings and a few are getting fired quite close together

Thanks for the report @jonsamwell. From the stack trace, it looks like an issue with the logging package. Can you confirm what version of it you are using?

Hi! I am also getting the same error when using the 3.0.0 version:

image

@kentcb I am using version 1.0.2 of logging and version 3.0.0 of azure_application_insights

Some notes from looking at this:

  1. This is the line of code that is throwing the exception (although it can also happen when an error is added to the stream, but I'm assuming that's not the case here - it's just trying to add another log event)
  2. Disabling logging is one way to avoid the problem (e.g. Logger.root.level = Level.OFF;), though obviously not a solution
  3. Dart's stream controllers do not allow re-entrancy, and that appears to be what's triggering the issue. That is, the handler for the log stream is itself logging, which causes the re-entrancy.
  4. I created a minimal repro here

@jonsamwell @Yagorundy Can you please check and confirm whether you have triggered re-entrancy by logging from your logging handler as per my repro?

Thinking about the problem over lunch, I suspect you're forwarding logs to AppInsights as trace telemetry? Doing so will, by default, log details on that process. This causes re-entrancy since you're doing this from your log handler. The easiest way to avoid this is to provide a custom logger to TransmissionProcessor:

// You need to enable hierarchical logging to allow loggers to have different log levels. Do this on app startup.
hierarchicalLoggingEnabled = true;

// Create a logger that will never log anything.
final nullLogger = Logger('Null');
nullLogger.level = Level.OFF;

// Pass that into TransmissionProcessor.
final processor = BufferedProcessor(
  next: TransmissionProcessor(
    instrumentationKey: apiKey,
    httpClient: client,
    timeout: const Duration(seconds: 10),
    logger: nullLogger,
  ),
);

Please let me know if that helps.

@kentcb this solution seems to work really well! I haven't encountered this issue since! Thank you for figuring it out!