Skip to content

Commit

Permalink
fix: Flush directly after capturing envelopes (#3915)
Browse files Browse the repository at this point in the history
Calling flush directly after capturing any envelope did not ensure that
the SDK flushes the captured envelope. This is fixed now.
  • Loading branch information
philipphofmann committed May 10, 2024
1 parent 8cbdf25 commit 379ff2a
Show file tree
Hide file tree
Showing 13 changed files with 194 additions and 127 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
### Fixes

- Fix data race when calling reportFullyDisplayed from a background thread (#3926)
- Ensure flushing envelopes directly after capturing them (#3915)

### Improvements

Expand Down
4 changes: 4 additions & 0 deletions Sentry.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@
62E146D02BAAE47600ED34FD /* LocalMetricsAggregator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 62E146CF2BAAE47600ED34FD /* LocalMetricsAggregator.swift */; };
62E146D22BAAF55B00ED34FD /* LocalMetricsAggregatorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 62E146D12BAAF55B00ED34FD /* LocalMetricsAggregatorTests.swift */; };
62F226B729A37C120038080D /* SentryBooleanSerialization.m in Sources */ = {isa = PBXBuildFile; fileRef = 62F226B629A37C120038080D /* SentryBooleanSerialization.m */; };
62FC69362BEDFF18002D3EF2 /* SentryLogExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 62FC69352BEDFF18002D3EF2 /* SentryLogExtensions.swift */; };
630435FE1EBCA9D900C4D3FA /* SentryNSURLRequest.h in Headers */ = {isa = PBXBuildFile; fileRef = 630435FC1EBCA9D900C4D3FA /* SentryNSURLRequest.h */; };
630435FF1EBCA9D900C4D3FA /* SentryNSURLRequest.m in Sources */ = {isa = PBXBuildFile; fileRef = 630435FD1EBCA9D900C4D3FA /* SentryNSURLRequest.m */; };
6304360A1EC0595B00C4D3FA /* SentryNSDataUtils.h in Headers */ = {isa = PBXBuildFile; fileRef = 630436081EC0595B00C4D3FA /* SentryNSDataUtils.h */; settings = {ATTRIBUTES = (Private, ); }; };
Expand Down Expand Up @@ -1094,6 +1095,7 @@
62E146D12BAAF55B00ED34FD /* LocalMetricsAggregatorTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocalMetricsAggregatorTests.swift; sourceTree = "<group>"; };
62F226B629A37C120038080D /* SentryBooleanSerialization.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SentryBooleanSerialization.m; sourceTree = "<group>"; };
62F226B829A37C270038080D /* SentryBooleanSerialization.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SentryBooleanSerialization.h; sourceTree = "<group>"; };
62FC69352BEDFF18002D3EF2 /* SentryLogExtensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentryLogExtensions.swift; sourceTree = "<group>"; };
630435FC1EBCA9D900C4D3FA /* SentryNSURLRequest.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = SentryNSURLRequest.h; path = include/SentryNSURLRequest.h; sourceTree = "<group>"; };
630435FD1EBCA9D900C4D3FA /* SentryNSURLRequest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SentryNSURLRequest.m; sourceTree = "<group>"; };
630436081EC0595B00C4D3FA /* SentryNSDataUtils.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = SentryNSDataUtils.h; path = include/SentryNSDataUtils.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -3452,6 +3454,7 @@
844EDCE72947DCD700C86F34 /* TestSentryNSTimerFactory.swift */,
84B7FA3B29B2866200AD93B1 /* SentryTestUtils-ObjC-BridgingHeader.h */,
62C25C852B075F4900C68CBD /* TestOptions.swift */,
62FC69352BEDFF18002D3EF2 /* SentryLogExtensions.swift */,
);
path = SentryTestUtils;
sourceTree = "<group>";
Expand Down Expand Up @@ -4971,6 +4974,7 @@
8431F01629B2851500D8DC56 /* TestSentryNSProcessInfoWrapper.swift in Sources */,
84B7FA4229B28CDE00AD93B1 /* TestCurrentDateProvider.swift in Sources */,
62C25C862B075F4900C68CBD /* TestOptions.swift in Sources */,
62FC69362BEDFF18002D3EF2 /* SentryLogExtensions.swift in Sources */,
84B7FA3F29B28BAD00AD93B1 /* TestTransport.swift in Sources */,
84A5D75B29D5170700388BFA /* TimeInterval+Sentry.swift in Sources */,
84AC61D929F7643B009EEF61 /* TestDispatchFactory.swift in Sources */,
Expand Down
9 changes: 0 additions & 9 deletions Sentry.xcodeproj/xcshareddata/xcschemes/Sentry.xcscheme
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,6 @@
<Test
Identifier = "SentryCrashReportStore_Tests/testStoresLoadsMultipleReports">
</Test>
<Test
Identifier = "SentryHttpTransportTests/testFlush_BlocksCallingThread_FinishesFlushingWhenSent()">
</Test>
<Test
Identifier = "SentryHttpTransportTests/testFlush_CalledSequentially_BlocksTwice()">
</Test>
<Test
Identifier = "SentryHttpTransportTests/testFlush_WhenNoInternet_BlocksAndFinishes()">
</Test>
<Test
Identifier = "SentryNetworkTrackerIntegrationTests/testGetCaptureFailedRequestsEnabled()">
</Test>
Expand Down
6 changes: 1 addition & 5 deletions SentryTestUtils/ClearTestState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ public func clearTestState() {
TestCleanup.clearTestState()
}

public func setTestDefaultLogLevel() {
SentryLog.configure(true, diagnosticLevel: .debug)
}

@objcMembers
class TestCleanup: NSObject {
static func clearTestState() {
Expand All @@ -24,7 +20,7 @@ class TestCleanup: NSObject {
PrivateSentrySDKOnly.appStartMeasurementHybridSDKMode = false
SentryNetworkTracker.sharedInstance.disable()

setTestDefaultLogLevel()
SentryLog.setTestDefaultLogLevel()

#if os(iOS) || os(tvOS) || targetEnvironment(macCatalyst)

Expand Down
20 changes: 20 additions & 0 deletions SentryTestUtils/SentryLogExtensions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import Foundation

extension SentryLog {
public static func setTestDefaultLogLevel() {
SentryLog.configure(true, diagnosticLevel: .debug)
}

public static func disable() {
SentryLog.configure(false, diagnosticLevel: .none)
}

/// SentryLog uses NSLog internally, which can significantly slow down code because it requires
/// synchronization. Tests that need code to run fast should can turn off logs to avoid flakiness.
public static func withOutLogs<T>(_ closure: () throws -> T) rethrows -> T {
defer { setTestDefaultLogLevel() }
disable()
return try closure()
}

}
30 changes: 19 additions & 11 deletions Sources/Sentry/SentryHttpTransport.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,13 @@
# import "SentryReachability.h"
#endif // !TARGET_OS_WATCH

static NSTimeInterval const cachedEnvelopeSendDelay = 0.1;

@interface
SentryHttpTransport ()
#if SENTRY_HAS_REACHABILITY
<SentryReachabilityObserver>
#endif // !TARGET_OS_WATCH

@property (nonatomic, readonly) NSTimeInterval cachedEnvelopeSendDelay;
@property (nonatomic, strong) SentryFileManager *fileManager;
@property (nonatomic, strong) id<SentryRequestManager> requestManager;
@property (nonatomic, strong) SentryNSURLRequestBuilder *requestBuilder;
Expand Down Expand Up @@ -68,15 +67,17 @@
@implementation SentryHttpTransport

- (id)initWithOptions:(SentryOptions *)options
fileManager:(SentryFileManager *)fileManager
requestManager:(id<SentryRequestManager>)requestManager
requestBuilder:(SentryNSURLRequestBuilder *)requestBuilder
rateLimits:(id<SentryRateLimits>)rateLimits
envelopeRateLimit:(SentryEnvelopeRateLimit *)envelopeRateLimit
dispatchQueueWrapper:(SentryDispatchQueueWrapper *)dispatchQueueWrapper
cachedEnvelopeSendDelay:(NSTimeInterval)cachedEnvelopeSendDelay
fileManager:(SentryFileManager *)fileManager
requestManager:(id<SentryRequestManager>)requestManager
requestBuilder:(SentryNSURLRequestBuilder *)requestBuilder
rateLimits:(id<SentryRateLimits>)rateLimits
envelopeRateLimit:(SentryEnvelopeRateLimit *)envelopeRateLimit
dispatchQueueWrapper:(SentryDispatchQueueWrapper *)dispatchQueueWrapper
{
if (self = [super init]) {
self.options = options;
_cachedEnvelopeSendDelay = cachedEnvelopeSendDelay;
self.requestManager = requestManager;
self.requestBuilder = requestBuilder;
self.fileManager = fileManager;
Expand Down Expand Up @@ -199,7 +200,12 @@ - (SentryFlushResult)flush:(NSTimeInterval)timeout
#endif // defined(TEST) || defined(TESTCI) || defined(DEBUG)
}

[self sendAllCachedEnvelopes];
// We are waiting for the dispatch group below, which we leave in finished sending. As
// sendAllCachedEnvelopes does some IO, it could block the calling thread longer than the
// desired flush duration. Therefore, we dispatch the sendAllCachedEnvelopes async. Furthermore,
// when calling flush directly after captureEnvelope, it could happen that SDK doesn't store the
// envelope to disk, which happens async, before starting to flush.
[self.dispatchQueue dispatchAsyncWithBlock:^{ [self sendAllCachedEnvelopes]; }];

intptr_t result = dispatch_group_wait(self.dispatchGroup, dispatchTimeout);

Expand Down Expand Up @@ -317,10 +323,12 @@ - (void)deleteEnvelopeAndSendNext:(NSString *)envelopePath
{
SENTRY_LOG_DEBUG(@"Deleting envelope and sending next.");
[self.fileManager removeFileAtPath:envelopePath];
self.isSending = NO;
@synchronized(self) {
self.isSending = NO;
}

__weak SentryHttpTransport *weakSelf = self;
[self.dispatchQueue dispatchAfter:cachedEnvelopeSendDelay
[self.dispatchQueue dispatchAfter:self.cachedEnvelopeSendDelay
block:^{
if (weakSelf == nil) {
return;
Expand Down
1 change: 1 addition & 0 deletions Sources/Sentry/SentryTransportFactory.m
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ @implementation SentryTransportFactory

SentryHttpTransport *httpTransport =
[[SentryHttpTransport alloc] initWithOptions:options
cachedEnvelopeSendDelay:0.1
fileManager:sentryFileManager
requestManager:requestManager
requestBuilder:requestBuilder
Expand Down
13 changes: 7 additions & 6 deletions Sources/Sentry/include/SentryHttpTransport.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ NS_ASSUME_NONNULL_BEGIN
SENTRY_NO_INIT

- (id)initWithOptions:(SentryOptions *)options
fileManager:(SentryFileManager *)fileManager
requestManager:(id<SentryRequestManager>)requestManager
requestBuilder:(SentryNSURLRequestBuilder *)requestBuilder
rateLimits:(id<SentryRateLimits>)rateLimits
envelopeRateLimit:(SentryEnvelopeRateLimit *)envelopeRateLimit
dispatchQueueWrapper:(SentryDispatchQueueWrapper *)dispatchQueueWrapper;
cachedEnvelopeSendDelay:(NSTimeInterval)cachedEnvelopeSendDelay
fileManager:(SentryFileManager *)fileManager
requestManager:(id<SentryRequestManager>)requestManager
requestBuilder:(SentryNSURLRequestBuilder *)requestBuilder
rateLimits:(id<SentryRateLimits>)rateLimits
envelopeRateLimit:(SentryEnvelopeRateLimit *)envelopeRateLimit
dispatchQueueWrapper:(SentryDispatchQueueWrapper *)dispatchQueueWrapper;

@end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ class SentryANRTrackerTests: XCTestCase, SentryANRTrackerDelegate {

wait(for: [anrDetectedExpectation, anrStoppedExpectation], timeout: waitTimeout)

setTestDefaultLogLevel()
SentryLog.setTestDefaultLogLevel()
}

func testRemoveListener_StopsReportingANRs() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ class SentryPerformanceTrackerTests: XCTestCase {
sut.finishSpan(span.key)
}

setTestDefaultLogLevel()
SentryLog.setTestDefaultLogLevel()
}

func testStackAsync() {
Expand Down

0 comments on commit 379ff2a

Please sign in to comment.