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

fix: read free_memory when the event is captured, not only at SDK startup #1962

Merged
merged 14 commits into from
Jul 18, 2022
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

- Properly sanitize the event context and SDK information (#1943)
- Don't send error 429 as `network_error` (#1957)
- Read free_memory when the event is captured, not only at SDK startup (#1962)
- Sanitize Span data (#1963)
- Deprecate not needed option `sdkInfo` (#1960)
- Crash in profiling logger (#1964)
Expand Down
28 changes: 24 additions & 4 deletions Sources/Sentry/SentryClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#import "SentryClient+Private.h"
#import "SentryCrashDefaultMachineContextWrapper.h"
#import "SentryCrashIntegration.h"
#import "SentryCrashMonitor_System.h"
#import "SentryCrashStackEntryMapper.h"
#import "SentryDebugImageProvider.h"
#import "SentryDefaultCurrentDateProvider.h"
Expand Down Expand Up @@ -487,10 +488,13 @@ - (SentryEvent *_Nullable)prepareEvent:(SentryEvent *)event

event = [scope applyToEvent:event maxBreadcrumb:self.options.maxBreadcrumbs];

// Remove free_memory if OOM as free_memory stems from the current run and not of the time of
// the OOM.
if ([self isOOM:event isCrashEvent:isCrashEvent]) {
// Remove free_memory if OOM as free_memory stems from the current run and not of
// the time of the OOM.
[self removeFreeMemoryFromDeviceContext:event];
} else {
// Store the actual free memory at the time of this event
[self storeFreeMemoryToDeviceContext:event];
kevinrenskers marked this conversation as resolved.
Show resolved Hide resolved
}

// With scope applied, before running callbacks run:
Expand Down Expand Up @@ -638,7 +642,24 @@ - (BOOL)isOOM:(SentryEvent *)event isCrashEvent:(BOOL)isCrashEvent
[exception.mechanism.type isEqualToString:SentryOutOfMemoryMechanismType];
}

- (void)storeFreeMemoryToDeviceContext:(SentryEvent *)event
kevinrenskers marked this conversation as resolved.
Show resolved Hide resolved
{
[self modifyDeviceContext:event
block:^(NSMutableDictionary *device) {
device[SentryDeviceContextFreeMemoryKey] =
@(sentrycrashcm_system_freememory());
}];
}

- (void)removeFreeMemoryFromDeviceContext:(SentryEvent *)event
{
[self modifyDeviceContext:event
block:^(NSMutableDictionary *device) {
[device removeObjectForKey:SentryDeviceContextFreeMemoryKey];
}];
}

- (void)modifyDeviceContext:(SentryEvent *)event block:(void (^)(NSMutableDictionary *))block
{
if (nil == event.context || event.context.count == 0 || nil == event.context[@"device"]) {
return;
Expand All @@ -647,9 +668,8 @@ - (void)removeFreeMemoryFromDeviceContext:(SentryEvent *)event
NSMutableDictionary *context = [[NSMutableDictionary alloc] initWithDictionary:event.context];
NSMutableDictionary *device =
[[NSMutableDictionary alloc] initWithDictionary:context[@"device"]];
[device removeObjectForKey:SentryDeviceContextFreeMemoryKey];
block(device);
context[@"device"] = device;

event.context = context;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ extern "C" {
*/
SentryCrashMonitorAPI *sentrycrashcm_system_getAPI(void);

uint64_t sentrycrashcm_system_freememory(void);

#ifdef __cplusplus
}
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,12 @@
return 0;
}

uint64_t
sentrycrashcm_system_freememory(void)
{
return freeMemory();
}

static uint64_t
usableMemory(void)
{
Expand Down