Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add more debug info for "Attempted to connect a connection that is no…
…t disconnected" crash (#2353)
  • Loading branch information
dmoagx committed Jan 13, 2016
1 parent 5efaf6a commit 5243f74
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
2 changes: 2 additions & 0 deletions Frameworks/SPMySQLFramework/Source/SPMySQLConnection.h
Expand Up @@ -128,6 +128,8 @@
BOOL retryQueriesOnConnectionFailure;

SPMySQLClientFlags clientFlags;

NSString *_debugLastConnectedEvent;
}

#pragma mark -
Expand Down
27 changes: 24 additions & 3 deletions Frameworks/SPMySQLFramework/Source/SPMySQLConnection.m
Expand Up @@ -199,6 +199,8 @@ - (id)init
// while running them
retryQueriesOnConnectionFailure = YES;

_debugLastConnectedEvent = nil;

// Start the ping keepalive timer
keepAliveTimer = [[SPMySQLKeepAliveTimer alloc] initWithInterval:10 target:self selector:@selector(_keepAlive)];

Expand Down Expand Up @@ -253,6 +255,8 @@ - (void) dealloc
if (querySqlstate) [querySqlstate release], querySqlstate = nil;
[delegateDecisionLock release];

[_debugLastConnectedEvent release];

[NSObject cancelPreviousPerformRequestsWithTarget:self];

[super dealloc];
Expand Down Expand Up @@ -428,6 +432,14 @@ + (NSString *)findSocketPath
const char *__crashreporter_info__ = NULL;
asm(".desc ___crashreporter_info__, 0x10");

static uint64_t _elapsedMicroSecondsSinceAbsoluteTime(uint64_t comparisonTime)
{
uint64_t elapsedTime_t = mach_absolute_time() - comparisonTime;
Nanoseconds elapsedTime = AbsoluteToNanoseconds(*(AbsoluteTime *)&(elapsedTime_t));

return (UnsignedWideToUInt64(elapsedTime) / 1000ULL);
}

@implementation SPMySQLConnection (PrivateAPI)

/**
Expand All @@ -438,8 +450,11 @@ - (BOOL)_connect

// If a connection is already active in some form, throw an exception
if (state != SPMySQLDisconnected && state != SPMySQLConnectionLostInBackground) {
asprintf(&__crashreporter_info__, "Attempted to connect a connection that is not disconnected (SPMySQLConnectionState=%d).", state);
__builtin_trap();
@synchronized (self) {
uint64_t diff = _elapsedMicroSecondsSinceAbsoluteTime(initialConnectTime);
asprintf(&__crashreporter_info__, "Attempted to connect a connection that is not disconnected (SPMySQLConnectionState=%d).\nIf state==2: Previous connection made %lluµs ago from: %s", state, diff, [_debugLastConnectedEvent cStringUsingEncoding:NSUTF8StringEncoding]);
__builtin_trap();
}
[NSException raise:NSInternalInconsistencyException format:@"Attempted to connect a connection that is not disconnected (SPMySQLConnectionState=%d).", state];
return NO;
}
Expand Down Expand Up @@ -472,7 +487,13 @@ - (BOOL)_connect

// Successfully connected - record connected state and reset tracking variables
state = SPMySQLConnected;
initialConnectTime = mach_absolute_time();

@synchronized (self) {
initialConnectTime = mach_absolute_time();
[_debugLastConnectedEvent release];
_debugLastConnectedEvent = [[NSString alloc] initWithFormat:@"thread=%@ stack=%@",[NSThread currentThread],[NSThread callStackSymbols]];
}

mysqlConnectionThreadId = mySQLConnection->thread_id;
lastConnectionUsedTime = initialConnectTime;

Expand Down

0 comments on commit 5243f74

Please sign in to comment.