Skip to content

Commit

Permalink
Fix incorrect app.duration after calling notify()
Browse files Browse the repository at this point in the history
  • Loading branch information
nickdowell committed Feb 26, 2021
1 parent 298b8dd commit 17f619e
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Bugsnag/KSCrash/Source/KSCrash/Recording/BSG_KSCrashC.c
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ void bsg_kscrash_reinstall(const char *const crashReportFilePath,
BSG_KSLOG_ERROR("Failed to initialize persistent crash state");
}
context->state.appLaunchTime = mach_absolute_time();
context->state.appStateTransitionTime = mach_absolute_time();
context->state.lastUpdateDurationsTime = mach_absolute_time();
}

BSG_KSCrashType bsg_kscrash_setHandlingCrashTypes(BSG_KSCrashType crashTypes) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ typedef struct {

/** Timestamp for when the app state was last changed
* (background<->foreground) (mach_absolute_time()) */
uint64_t appStateTransitionTime;
uint64_t lastUpdateDurationsTime;

/** If true, the application is currently in the foreground. */
bool applicationIsInForeground;
Expand Down
16 changes: 11 additions & 5 deletions Bugsnag/KSCrash/Source/KSCrash/Recording/BSG_KSCrashState.m
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ bool bsg_kscrashstate_i_loadState(BSG_KSCrash_State *const context,
context->foregroundDurationSinceLastCrash = [objectContext[@"foregroundDurationSinceLastCrash"] doubleValue];
context->foregroundDurationSinceLaunch = [objectContext[@"foregroundDurationSinceLaunch"] doubleValue];
context->appLaunchTime = [objectContext[@"appLaunchTime"] unsignedLongLongValue];
context->appStateTransitionTime = [objectContext[@"appStateTransitionTime"] unsignedLongLongValue];
context->lastUpdateDurationsTime = [objectContext[@"appStateTransitionTime"] unsignedLongLongValue];
context->launchesSinceLastCrash = [objectContext[@"launchesSinceLastCrash"] intValue];
context->sessionsSinceLastCrash = [objectContext[@"sessionsSinceLastCrash"] intValue];
context->sessionsSinceLaunch = [objectContext[@"sessionsSinceLaunch"] intValue];
Expand Down Expand Up @@ -322,7 +322,7 @@ void bsg_kscrashstate_notifyAppInForeground(const bool isInForeground) {
state->applicationIsInForeground = isInForeground;
uint64_t timeNow = mach_absolute_time();
double duration = bsg_ksmachtimeDifferenceInSeconds(
timeNow, state->appStateTransitionTime);
timeNow, state->lastUpdateDurationsTime);
if (isInForeground) {
state->backgroundDurationSinceLaunch += duration;
state->backgroundDurationSinceLastCrash += duration;
Expand All @@ -333,15 +333,15 @@ void bsg_kscrashstate_notifyAppInForeground(const bool isInForeground) {
state->foregroundDurationSinceLastCrash += duration;
bsg_kscrashstate_i_saveState(state, stateFilePath);
}
state->appStateTransitionTime = timeNow;
state->lastUpdateDurationsTime = timeNow;
}

void bsg_kscrashstate_notifyAppTerminate(void) {
BSG_KSCrash_State *const state = bsg_g_state;
const char *const stateFilePath = bsg_g_stateFilePath;

const double duration = bsg_ksmachtimeDifferenceInSeconds(
mach_absolute_time(), state->appStateTransitionTime);
mach_absolute_time(), state->lastUpdateDurationsTime);
state->backgroundDurationSinceLastCrash += duration;
bsg_kscrashstate_i_saveState(state, stateFilePath);
}
Expand All @@ -358,15 +358,21 @@ void bsg_kscrashstate_notifyAppCrash(BSG_KSCrashType type) {
}

void bsg_kscrashstate_updateDurationStats(BSG_KSCrash_State *const state) {
if (!state->lastUpdateDurationsTime) {
// BSG_KSCrash_State has not yet been initialized.
return;
}
uint64_t timeNow = mach_absolute_time();
const double duration = bsg_ksmachtimeDifferenceInSeconds(
mach_absolute_time(), state->appStateTransitionTime);
timeNow, state->lastUpdateDurationsTime);
if (state->applicationIsInForeground) {
state->foregroundDurationSinceLaunch += duration;
state->foregroundDurationSinceLastCrash += duration;
} else {
state->backgroundDurationSinceLaunch += duration;
state->backgroundDurationSinceLastCrash += duration;
}
state->lastUpdateDurationsTime = timeNow;
}

const BSG_KSCrash_State *bsg_kscrashstate_currentState(void) {
Expand Down
4 changes: 2 additions & 2 deletions Bugsnag/Payload/BugsnagAppWithState.m
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ + (BugsnagAppWithState *)appWithDictionary:(NSDictionary *)event
NSDictionary *stats = system[@"application_stats"];

// convert from seconds to milliseconds
NSNumber *activeTimeSinceLaunch = @([stats[@"active_time_since_launch"] longValue] * 1000);
NSNumber *backgroundTimeSinceLaunch = @([stats[@"background_time_since_launch"] longValue] * 1000);
NSNumber *activeTimeSinceLaunch = @((int)([stats[@"active_time_since_launch"] doubleValue] * 1000.0));
NSNumber *backgroundTimeSinceLaunch = @((int)([stats[@"background_time_since_launch"] doubleValue] * 1000.0));

app.durationInForeground = activeTimeSinceLaunch;
app.duration = @([activeTimeSinceLaunch longValue] + [backgroundTimeSinceLaunch longValue]);
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ Changelog

### Bug fixes

* Fix inaccurate app.duration values after calling `notify()` multiple times.
[#1014](https://github.com/bugsnag/bugsnag-cocoa/pull/1014)

* Fix a possible deadlock when writing crash reports for unhandled errors.
[#1013](https://github.com/bugsnag/bugsnag-cocoa/pull/1013)

Expand Down

0 comments on commit 17f619e

Please sign in to comment.